Skip to content

Commit

Permalink
feature: date/datetime fields in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Apr 14, 2021
1 parent 56abfcf commit 281724d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_requirements(fname='requirements.txt'):

setup(
name="unicms",
version='0.8.7',
version='0.8.8',
description="uniCMS is a Django Web Content Management System",
author='Giuseppe De Marco, Francesco Filicetti',
author_email='giuseppe.demarco@unical.it, francesco.filicetti@unical.it',
Expand Down
12 changes: 10 additions & 2 deletions src/cms/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,22 @@ def _get_choices(choices):
field_type = getattr(field.widget.__class__,
'input_type',
'textarea')
print(field.widget.__class__)

field_dict = {}
field_dict['id'] = field_name
field_dict['label'] = field.label
field_dict['required'] = 1 if field.required else 0
field_dict['type'] = field_type
field_dict['help_text'] = field.help_text
field_dict['options'] = []
field_dict['multiple'] = 0

if field_type == "select":
if field.widget.__class__.__name__ == 'DateInput':
field_dict['type'] = 'date'
elif field.widget.__class__.__name__ == 'DateTimeInput':
field_dict['type'] = 'datetime'
elif field.widget.__class__.__name__ == 'Select':
field_dict['type'] = 'select'
if field.widget.__class__.__dict__.get('allow_multiple_selected'):
field_dict['multiple'] = 1

Expand All @@ -63,6 +69,8 @@ def _get_choices(choices):
"value": item.pk})
elif hasattr(field, '_choices'):
field_dict['options'].extend(_get_choices(field._choices))
else:
field_dict['type'] = field_type
form_fields.append(field_dict)
return form_fields

Expand Down
1 change: 0 additions & 1 deletion src/cms/contexts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Meta:
class PublicationContextForm(ModelForm):

def __init__(self, *args, **kwargs):
print(kwargs)
site_id = kwargs.pop('site_id', None)
webpath_id = kwargs.pop('webpath_id', None)
super().__init__(*args, **kwargs)
Expand Down
2 changes: 2 additions & 0 deletions src/cms/pages/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def to_representation(self, instance):
data['base_template'] = base_template.data
data['webpath'] = webpath.data
data['preview_url'] = preview_url
data['date_start'] = instance.date_start.strftime("%Y-%m-%d %H:%M:%S")
data['date_end'] = instance.date_end.strftime("%Y-%m-%d %H:%M:%S") if instance.date_end else None
return data

class Meta:
Expand Down
4 changes: 4 additions & 0 deletions src/cms/publications/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def to_representation(self, instance):
data = super().to_representation(instance)
publication = PublicationSerializer(instance.publication)
data['publication'] = publication.data
data['date_start'] = instance.date_start.strftime("%Y-%m-%d %H:%M:%S")
data['date_end'] = instance.date_end.strftime("%Y-%m-%d %H:%M:%S")
data['in_evidence_start'] = instance.in_evidence_start.strftime("%Y-%m-%d %H:%M:%S") if instance.in_evidence_start else None
data['in_evidence_end'] = instance.in_evidence_end.strftime("%Y-%m-%d %H:%M:%S") if instance.in_evidence_end else None
return data

class Meta:
Expand Down

0 comments on commit 281724d

Please sign in to comment.