Renamed PeriodFields to SecondsField
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 13 Nov 2010 13:51:46 +0000 (13:51 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 13 Nov 2010 13:51:46 +0000 (13:51 +0000)
Added "week" period
Clean up SecondsWidget.compress()

bin/djais/views.py

index a2eec223359eb6458f1181279eca7a3f8c7ec38d..1a3f8506d3d47c05b363b74ea01e6daf69dd869c 100644 (file)
@@ -115,15 +115,17 @@ def vessel_search(request):
 
     return render_to_response('vessel_index.html', {'form': form}, RequestContext(request))
 
-class PeriodWidget(forms.MultiWidget):
+class SecondsWidget(forms.MultiWidget):
     '''
     A widget that splits period number and period type in a integer and a choice widgets
+    Does not work well for more than a week, as month lengthes do vary.
     '''
     __periods = (
         (u'1', u'second(s)'),
         (u'60', u'minute(s)'),
         (u'3600', u'hour(s)'),
         (u'86400', u'day(s)'),
+        (u'604800', u'week(s)'),
         (u'2592000', u'month(es)'))
 
     def __init__(self, attrs=None):
@@ -132,24 +134,18 @@ class PeriodWidget(forms.MultiWidget):
             textattrs.update(attrs)
         widgets = (
             forms.TextInput(attrs=textattrs),
-            forms.Select(choices=(self.__periods), attrs=attrs))
-        super(PeriodWidget, self).__init__(widgets, attrs)
+            forms.Select(choices=self.__periods, attrs=attrs))
+        super(SecondsWidget, self).__init__(widgets, attrs)
 
     def decompress(self, value):
         if value:
-            if value >= 2592000 and not value % 2592000:
-                return [ unicode(value // 2592000), u'2592000' ]
-            if value >= 86400 and not value % 86400:
-                return [ unicode(value // 86400), u'86400' ]
-            if value >= 3600 and not value % 3600:
-                return [ unicode(value // 3600), u'3600' ]
-            if value >= 60 and not value % 60:
-                return [ unicode(value // 60), u'60' ]
-            return [unicode(value), u'1']
+            for period_second in reversed([int(sec_txt[0]) for sec_txt in self.__periods]):
+                if value >= period_second and not value % period_second:
+                    return [ unicode(value // period_second), unicode(period_second) ]
         return [None, None]
 
-class PeriodField(forms.MultiValueField):
-    widget = PeriodWidget
+class SecondsField(forms.MultiValueField):
+    widget = SecondsWidget
 
     def __init__(self, *args, **kargs):
         fields = (
@@ -167,14 +163,13 @@ class PeriodField(forms.MultiValueField):
                 pass
         return None
 
-
 class HistoryForm(forms.Form):
     format = forms.ChoiceField(choices=(('animation', 'Animation (Google Earth v5)'), ('track', 'Track line (Google Earth)'), ('csv', 'Coma separated values (SpreadSheet)')), widget=forms.Select(attrs={'onchange': mark_safe("if (this.value=='csv') $('#csvhint').show(); else $('#csvhint').hide();")}))
     period_type = forms.ChoiceField(choices=(('duration', 'Duration until now'), ('date_date','Between two dates'), ('start_duration', 'Start date and duration')), widget=forms.RadioSelect(attrs={'onchange': mark_safe("show_hide_start_end_time(this.value);")}))
     start_date = forms.DateTimeField(required=False, widget=AisCalendarWidget(attrs={'class':'vDateField'}))
-    duration = PeriodField(required=False, label='Period length', initial=7*86400)
+    duration = SecondsField(required=False, label='Period length', initial=7*86400)
     end_date = forms.DateTimeField(required=False, widget=AisCalendarWidget(attrs={'class':'vDateField'}))
-    grain = PeriodField(label='One position every', initial=3600)
+    grain = SecondsField(label='One position every', initial=3600)
 
     def clean_start_date(self):
         period_type = self.cleaned_data.get('period_type', None)
@@ -498,8 +493,6 @@ def vessel_history(request, strmmsi):
                 date_start = data['start_date']
                 date_end = date_start + timedelta(0, data['duration'])
 
-            grain = data['grain']
-            
             format = data['format']
 
             if format == u'track':
@@ -521,7 +514,10 @@ def vessel_history(request, strmmsi):
             command += u' --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\''
             if date_end:
                 command += u' --end=\'' + date_end.strftime('%Y%m%d %H%M%S') + u'\''
+            
+            grain = data['grain']
             command += u' --granularity=' + unicode(grain)
+
             command += u' ' + strmmsi
 
             job = Job()