Skip to content

Commit

Permalink
Implemented result tag formatting, worked on translation, implemented…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
sorinmarti committed Dec 13, 2023
1 parent f51e026 commit cd86d62
Show file tree
Hide file tree
Showing 33 changed files with 566 additions and 274 deletions.
2 changes: 1 addition & 1 deletion ndr_core/admin_forms/result_card_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, *args, **kwargs):
if result_field_conf_row == 0:
required = True

result_field = forms.ModelChoiceField(queryset=NdrCoreResultField.objects.all(),
result_field = forms.ModelChoiceField(queryset=NdrCoreResultField.objects.all().order_by('label'),
required=required, help_text="")
row_field = forms.IntegerField(required=required,
help_text="")
Expand Down
8 changes: 0 additions & 8 deletions ndr_core/admin_forms/result_field_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ def helper(self):
)
layout.append(form_row)

form_row = Row(
Column('display_border', css_class='form-group col-4'),
Column('html_display', css_class='form-group col-4'),
Column('md_display', css_class='form-group col-4'),
css_class='form-row'
)
layout.append(form_row)

form_row = Row(
Column(
get_info_box('Access your variables in the following form', 'xxx_info'),
Expand Down
10 changes: 9 additions & 1 deletion ndr_core/admin_forms/search_config_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Meta:
'api_type', 'api_connection_url',
'api_user_name', 'api_password', 'api_auth_key',
'search_id_field', 'sort_field', 'sort_order',
'search_has_compact_result', 'page_size', 'compact_page_size', 'repository_url',
'search_has_compact_result', 'compact_result_is_default', 'page_size',
'compact_page_size', 'citation_expression', 'repository_url',
'has_simple_search', 'simple_search_first', 'simple_query_main_field',
'simple_query_label', 'simple_query_help_text',]

Expand Down Expand Up @@ -107,6 +108,13 @@ def helper(self):
)
layout.append(form_row)

form_row = Row(
Column('compact_result_is_default', css_class='col-3'),
Column('citation_expression', css_class='col-9'),
css_class='form-row'
)
layout.append(form_row)

form_row = Row(
Column(Div(HTML('''
<br/>
Expand Down
29 changes: 25 additions & 4 deletions ndr_core/admin_forms/translation_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@ def init_form(self):
initial_values = {}
for item in self.items:
for field in self.translatable_fields:
values = {}
if isinstance(field, dict):
values = field
field = values['field']

if 'condition' in values:
if item.__getattribute__(values['condition']['field']) not in values['condition']['values']:
continue

self.fields[f"{field}_{item.pk}"] = forms.CharField(label=f"Translate: '{field}' for '{item.__getattribute__(field)}'",
required=False,
max_length=1000,
help_text='')
if field.startswith('rich_'):
if 'widget' in values and values['widget'] == 'textarea':
self.fields[f"{field}_{item.pk}"].widget = forms.Textarea(attrs={'rows': 3})

initial_values[f"{field}_{item.pk}"] = self.get_initial_value(field, str(item.pk))
Expand All @@ -54,6 +63,15 @@ def do_helper(self):

cols = []
for field in self.translatable_fields:
values = {}
if isinstance(field, dict):
values = field
field = values['field']

if 'condition' in values:
if item.__getattribute__(values['condition']['field']) not in values['condition']['values']:
continue

cols.append(Column(f"{field}_{item.pk}", css_class=f'form-group col-{int(12/len(self.translatable_fields))}'),)

form_row = Row(
Expand Down Expand Up @@ -81,7 +99,6 @@ def save_translations(self):
"""Saves the translations to the database. """
self.is_valid()

print(self.cleaned_data)
for item in self.items:
for field in self.translatable_fields:
self.save_translation(str(item.pk), field, self.cleaned_data[f"{field}_{item.pk}"])
Expand Down Expand Up @@ -117,7 +134,11 @@ class TranslateFieldForm(TranslateForm):
"""Form to translate form field values """

items = NdrCoreSearchField.objects.all()
translatable_fields = ['field_label', 'help_text']
translatable_fields = ['field_label', 'help_text',
{'field': 'list_choices',
'widget': 'textarea',
'condition': {'field': 'field_type',
'values': [NdrCoreSearchField.FieldType.INFO_TEXT]}}]
table_name = 'NdrCoreSearchField'

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -170,7 +191,7 @@ class TranslateResultForm(TranslateForm):
"""Form to translate settings values. """

items = NdrCoreResultField.objects.all()
translatable_fields = ['rich_expression']
translatable_fields = [{'field': 'rich_expression', 'widget': 'textarea'}]
table_name = 'NdrCoreResultField'

def __init__(self, *args, **kwargs):
Expand Down
3 changes: 1 addition & 2 deletions ndr_core/admin_views/color_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ def form_valid(self, form):
my_string = f.read().decode('utf-8')
deserialized_object = serializers.deserialize("json", "["+my_string+"]")
for obj in deserialized_object:
if NdrCoreColorScheme.objects.filter(scheme_name=obj.object.scheme_name).count()>0:
if NdrCoreColorScheme.objects.filter(scheme_name=obj.object.scheme_name).count() > 0:
messages.info(self.request, f'The scheme "{obj.object.scheme_name}" was updated')
print(obj.save())
except DeserializationError:
messages.error(self.request, 'Could not deserialize object.')

Expand Down
14 changes: 8 additions & 6 deletions ndr_core/admin_views/result_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ndr_core.admin_forms.result_card_forms import SearchConfigurationResultEditForm
from ndr_core.admin_views.admin_views import AdminViewMixin
from ndr_core.form_preview import get_search_form_image_from_raw_data
from ndr_core.form_preview import PreviewImage
from ndr_core.admin_forms.result_field_forms import ResultFieldCreateForm, ResultFieldEditForm
from ndr_core.models import (
NdrCoreResultField,
Expand Down Expand Up @@ -73,8 +73,8 @@ def get_form(self, form_class=None):
"""Returns the form for this view. """
form = super().get_form(form_class=form_class)
all_fields = NdrCoreSearchConfiguration.objects.get(pk=self.kwargs['pk']).result_card_fields.all()
normal_fields = all_fields.filter(result_card_group='normal')
compact_fields = all_fields.filter(result_card_group='compact')
normal_fields = all_fields.filter(result_card_group='normal').order_by('field_column').order_by('field_row')
compact_fields = all_fields.filter(result_card_group='compact').order_by('field_column').order_by('field_row')

form_row = 0
for field in normal_fields:
Expand All @@ -96,6 +96,8 @@ def form_valid(self, form):
"""Creates or updates the result card configuration for a search configuration. """
response = super().form_valid(form)
conf_object = NdrCoreSearchConfiguration.objects.get(pk=self.kwargs['pk'])
all_fields = conf_object.result_card_fields.all()
all_fields.delete()

for row in range(20):
fields = self.get_row_fields(row)
Expand Down Expand Up @@ -142,6 +144,7 @@ def form_valid(self, form):

def preview_result_card_image(request, img_config):
"""Creates a result card preview image of a result form configuration. """

data = []
config_rows = img_config.split(",")
for row in config_rows:
Expand All @@ -152,7 +155,6 @@ def preview_result_card_image(request, img_config):
'row': int(config_row[0]),
'col': int(config_row[1]),
'size': int(config_row[2]),
'text': '',
'type': field.field_type})
image_data = get_search_form_image_from_raw_data(data)
'text': field.label})
image_data = PreviewImage().create_result_card_image_from_raw_data(data)
return HttpResponse(image_data, content_type="image/png")
4 changes: 2 additions & 2 deletions ndr_core/admin_views/search_field_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.views.generic import CreateView, UpdateView, DeleteView

from ndr_core.admin_views.admin_views import AdminViewMixin
from ndr_core.form_preview import get_search_form_image_from_raw_data
from ndr_core.form_preview import PreviewImage
from ndr_core.admin_forms.search_field_forms import SearchFieldCreateForm, SearchFieldEditForm

from ndr_core.models import NdrCoreSearchField
Expand Down Expand Up @@ -52,5 +52,5 @@ def preview_search_form_image(request, img_config):
'size': int(config_row[2]),
'text': field.field_label,
'type': field.field_type})
image_data = get_search_form_image_from_raw_data(data)
image_data = PreviewImage().create_search_form_image_from_raw_data(data)
return HttpResponse(image_data, content_type="image/png")
3 changes: 2 additions & 1 deletion ndr_core/admin_views/search_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class SearchConfigurationFormEditView(AdminViewMixin, LoginRequiredMixin, FormVi
def get_form(self, form_class=None):
"""Returns the form for this view. """
form = super().get_form(form_class=form_class)
fields = NdrCoreSearchConfiguration.objects.get(pk=self.kwargs['pk']).search_form_fields.all()
fields = (NdrCoreSearchConfiguration.objects.get(pk=self.kwargs['pk']).search_form_fields.all().
order_by('field_column').order_by('field_row'))

form_row = 0
for field in fields:
Expand Down
1 change: 0 additions & 1 deletion ndr_core/admin_views/settings_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ def form_valid(self, form):
for obj in deserialized_object:
if NdrCoreValue.objects.filter(value_name=obj.object.value_name).count() > 0:
messages.info(self.request, f'The setting "{obj.object.value_name}" was updated')
print(obj.save())
except DeserializationError:
messages.error(self.request, 'Could not deserialize object.')

Expand Down
1 change: 0 additions & 1 deletion ndr_core/api/mongodb/mongodb_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def get_advanced_query(self, *kwargs):
except NdrCoreSearchField.DoesNotExist:
pass

print(query)
return query

def get_list_query(self, list_name, add_page_and_size=True, search_term=None, tags=None):
Expand Down
2 changes: 0 additions & 2 deletions ndr_core/api/mongodb/mongodb_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def download_result(self):
try:
# Get the connection string and collection from the configuration
connection_string_arr = self.search_configuration.api_connection_url.split('/')
print(connection_string_arr)
connection_string = '/'.join(connection_string_arr[:-1])
print(connection_string)
db_client = pymongo.MongoClient(connection_string, serverSelectionTimeoutMS=2000)
collection = db_client[connection_string_arr[-2]][connection_string_arr[-1]]

Expand Down
11 changes: 1 addition & 10 deletions ndr_core/form_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,8 @@ def create_result_card_image_from_raw_data(self, data):
coords_offset = self.get_coordinates(data_point['row'], data_point['col'], data_point['size'], offset=3)
draw.rounded_rectangle(coords_offset, 5, fill=self.shadow_color, outline="#333333")
draw.rounded_rectangle(coords, 5, fill=self.field_color, outline="#36454F")
draw.text((coords[0][0] + 10, coords[0][1] + 5), data_point['text'], (0, 0, 0))

output = io.BytesIO()
img.save(output, "PNG")
return output.getvalue()


def get_search_form_image_from_raw_data(data):
"""This function gets called as view. It returns a preview image of a form."""
return PreviewImage().create_search_form_image_from_raw_data(data)


def get_result_card_image_from_raw_data(data):
"""This function gets called as view. It returns a preview image of a result card."""
return PreviewImage().create_result_card_image_from_raw_data(data)
1 change: 0 additions & 1 deletion ndr_core/forms/forms_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Meta:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# print(NdrCoreValue.get_or_initialize(value_name='contact_form_default_subject').translated_value())
self.fields['message_subject'].initial = NdrCoreValue.get_or_initialize(
value_name='contact_form_default_subject').translated_value()
self.fields['message_subject'].label = _('Message Subject')
Expand Down
6 changes: 3 additions & 3 deletions ndr_core/forms/forms_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def __init__(self, *args, **kwargs):
"""Initializes all needed form fields for the configured search based on
the page's search configuration. """

if self.ndr_page is not None:
self.search_configs = self.ndr_page.search_configs.all()
elif 'ndr_page' in kwargs:
if 'ndr_page' in kwargs:
self.ndr_page = kwargs.pop('ndr_page')

if self.ndr_page is not None:
self.search_configs = self.ndr_page.search_configs.all()
elif 'search_config' in kwargs:
self.search_configs = [kwargs.pop('search_config')]
Expand Down
2 changes: 1 addition & 1 deletion ndr_core/management/commands/ndr_import_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ def handle(self, *args, **options):
order_value_1=year,
order_value_2=issue,
order_value_3=issue_id)
print(f"Created: {year}/{issue}: {title}")
self.stdout.write(self.style.SUCCESS(f"Created: {year}/{issue}: {title}"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.7 on 2023-12-07 21:53

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("ndr_core", "0008_ndrcoreresultfield_label"),
]

operations = [
migrations.AddField(
model_name="ndrcoresearchconfiguration",
name="citation_expression",
field=models.CharField(
blank=True,
default=None,
help_text="Expression to generate a citation for a result.",
max_length=512,
null=True,
verbose_name="Citation Expression",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.7 on 2023-12-07 22:03

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("ndr_core", "0009_ndrcoresearchconfiguration_citation_expression"),
]

operations = [
migrations.AddField(
model_name="ndrcoresearchconfiguration",
name="compact_result_is_default",
field=models.BooleanField(
default=False,
help_text="If the compact result view is the default, check this box.",
),
),
]
25 changes: 23 additions & 2 deletions ndr_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def get_list_choices(self):
"""Returns the list choices as a list of tuples. This is used to render the dropdowns
in the search form and result template lists."""
if not self.field_type == self.FieldType.LIST and not self.field_type == self.FieldType.MULTI_LIST:
return {}
return []

file_handle = StringIO(self.list_choices)
reader = csv.reader(file_handle, delimiter=',')
Expand All @@ -256,12 +256,25 @@ def get_list_choices(self):
for row in reader:
if row_number == 0:
header = row
print(row)
else:

try:
val = row[header.index(f'value_{get_language()}')]
except ValueError:
val = row[header.index('value')]
result_list.append((row[header.index('key')], val))

try:
searchable = header.index('is_searchable')
searchable = row[searchable]
except ValueError:
searchable = 'true'
except IndexError:
searchable = 'true'

if searchable.lower() == 'true':
result_list.append((row[0], val))


row_number += 1

Expand Down Expand Up @@ -468,6 +481,10 @@ class NdrCoreSearchConfiguration(TranslatableMixin, models.Model):
"check this box.")
"""If the result has a normal and a compact view, check this box."""

compact_result_is_default = models.BooleanField(default=False,
help_text="If the compact result view is the default, "
"check this box.")

page_size = models.IntegerField(default=10,
verbose_name="Page Size",
help_text="Size of the result page (e.g. 'How many results at once')")
Expand All @@ -483,6 +500,10 @@ class NdrCoreSearchConfiguration(TranslatableMixin, models.Model):
help_text="URL to the data repository where this data is stored.")
"""URL to the repository's website."""

citation_expression = models.CharField(max_length=512, default=None, null=True, blank=True,
verbose_name="Citation Expression",
help_text="Expression to generate a citation for a result.")

def __str__(self):
return self.conf_name

Expand Down
2 changes: 0 additions & 2 deletions ndr_core/ndr_template_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,8 @@ def get_element(self, template, element_id):
else:
kw = {'pk': element_id}
element = element_class.objects.get(**kw)
# print(f"Element found {element_class} / {element_id}: {element}")
return element
except element_class.DoesNotExist:
# print(f"Element not found {element_class} / {element_id}")
return None

def get_pre_rendered_text(self):
Expand Down
Loading

0 comments on commit cd86d62

Please sign in to comment.