Merge with 1762d071100840e538d715593197965db9075680
authorcavaliet
Thu, 04 Oct 2012 12:17:41 +0200
changeset 820 f1a218ea85c3
parent 819 d6b64df81b35 (current diff)
parent 818 1762d0711008 (diff)
child 821 ca8127a4cdd5
Merge with 1762d071100840e538d715593197965db9075680
--- a/src/ldt/ldt/forms/__init__.py	Thu Oct 04 12:17:09 2012 +0200
+++ b/src/ldt/ldt/forms/__init__.py	Thu Oct 04 12:17:41 2012 +0200
@@ -1,1 +1,7 @@
+from __future__ import absolute_import
 
+from django.core.exceptions import ValidationError
+from django.forms.fields import *
+from django.forms.forms import *
+from django.forms.models import *
+from django.forms.widgets import *
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/forms/fields.py	Thu Oct 04 12:17:41 2012 +0200
@@ -0,0 +1,34 @@
+"""
+Field classes.
+"""
+
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+import logging
+import time
+import math
+
+__all__ = (
+    'LdtDurationField'
+)
+
+class LdtDurationField (forms.TimeField):
+    default_error_messages = {
+        'invalid': _(u'Enter a valid duration format'),
+    }
+    
+    def __init__(self, formats, *args, **kwargs):
+        self.formats = formats
+        super(LdtDurationField, self).__init__(*args, **kwargs)
+    
+    def clean(self, data):
+        dur = data
+        for format in self.formats:
+            try:
+                dur = time.strptime(dur, format)
+                dur = dur.tm_hour*3600 + dur.tm_min*60 + dur.tm_sec
+                dur = dur*1000
+                break
+            except:
+                logging.debug("trying next format")
+        return dur
\ No newline at end of file
--- a/src/ldt/ldt/ldt_utils/forms.py	Thu Oct 04 12:17:09 2012 +0200
+++ b/src/ldt/ldt/ldt_utils/forms.py	Thu Oct 04 12:17:41 2012 +0200
@@ -1,7 +1,7 @@
 from django import forms
 from django.utils.translation import ugettext_lazy as _
 from django.contrib.auth.models import Group
-from ldt.forms import widgets as ldt_widgets
+from ldt.forms import widgets as ldt_widgets, fields as ldt_fields
 from ldt.security.forms import ShareForm
 from models import Project, Content, Media
 from utils import generate_uuid
@@ -54,7 +54,7 @@
     groups = forms.ModelMultipleChoiceField(Group.objects.all(), required=False)
     is_public = forms.BooleanField(required=False)
     front_project = forms.ModelChoiceField(queryset=Project.objects.none(), required=False, label=_("content.front_project"))
-    duration = forms.IntegerField(required=True, label=_("content.duration")+" (Ms, H:M, H:M:S, HhM, Ss, Ssec)", widget=ldt_widgets.LdtParseVideoDuration(format=["%H:%M", "%H:%M:%S", "%Hh%M", "%Ss", "%Ssec"]))
+    duration = ldt_fields.LdtDurationField(required=True, label=_("content.duration")+" (Ms, M:S, H:M:S, HhM, Ss, Ssec)", formats=["%M:%S", "%H:%M:%S", "%Hh%M", "%Ss", "%Ssec"])
         
     def clean_iri_id(self):
         data = self.cleaned_data.get('iri_id')