Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. class EventForm(forms.ModelForm):
  2. dt = '%Y-%m-%d %H:%M'
  3.  
  4. start = forms.DateTimeField(
  5. label="Start time (in event's local time)",
  6. required=False,
  7. input_formats=[dt]
  8. )
  9.  
  10. end = forms.DateTimeField(
  11. label="End time (in event's local time)",
  12. required=False,
  13. input_formats=[dt]
  14. )
  15.  
  16. class Meta:
  17. model = Event
  18. fields = [
  19. 'name', 'flexibility', 'timezone',
  20. 'date_start', 'date_end', 'last_field',
  21. ]
  22.  
  23. def __init__(self, *args, **kwargs):
  24. super(EventForm, self).__init__(*args, **kwargs)
  25.  
  26. self.fields['date_start'].widget = forms.HiddenInput()
  27. self.fields['date_end'].widget = forms.HiddenInput()
  28.  
  29. i = kwargs.get('instance')
  30. tz = pytz.timezone(i.timezone) if i and i.timezone else None
  31. if i and i.flexibility == Event.FLEXIBILITY_ONTIME and tz:
  32. self.fields['start'].initial = i.date_start.astimezone(tz).strftime(self.dt) if i.date_start else None
  33. self.fields['end'].initial = i.date_end.astimezone(tz).strftime(self.dt) if i.date_end else None
  34.  
  35. else:
  36. self.fields['start'].initial = None
  37. self.fields['end'].initial = None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement