slashmili / django-jalali

Jalali DateField support for Django model
http://pypi.python.org/pypi/django_jalali
BSD 3-Clause "New" or "Revised" License
258 stars 52 forks source link

Specific date not parsing #97

Closed ajahansh closed 5 years ago

ajahansh commented 5 years ago
$ from foo.models import BarTime
$ BarTime.objects.create(name="Test", datetime="1398-04-30 12:12:12")
<BarTime: Test, 1398-04-30 12:12:12>
 $BarTime.objects.create(name="Test", datetime="1398-04-31 12:12:12")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django_jalali\db\models.py in parse_date(self, datetime_obj)
    251         try:  # Seconds are optional, so try converting seconds first.
--> 252             t = time.strptime(datetime_obj, '%Y-%m-%d %H:%M:%S')
    253             if t.tm_year > 1700:

c:\tools\miniconda3\envs\django_jalali\lib\_strptime.py in _strptime_time(data_string, format)
    570     format string."""
--> 571     tt = _strptime(data_string, format)[0]
    572     return time.struct_time(tt[:time._STRUCT_TM_ITEMS])

c:\tools\miniconda3\envs\django_jalali\lib\_strptime.py in _strptime(data_string, format)
    543         julian = datetime_date(year, month, day).toordinal() - \
--> 544                   datetime_date(year, 1, 1).toordinal() + 1
    545     else:  # Assume that if they bothered to include Julian day (or if it was

ValueError: day is out of range for month

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django_jalali\db\models.py in parse_date(self, datetime_obj)
    263             try:  # Try without seconds.
--> 264                 t = time.strptime(datetime_obj, '%Y-%m-%d %H:%M')
    265                 if t.tm_year > 1700:

c:\tools\miniconda3\envs\django_jalali\lib\_strptime.py in _strptime_time(data_string, format)
    570     format string."""
--> 571     tt = _strptime(data_string, format)[0]
    572     return time.struct_time(tt[:time._STRUCT_TM_ITEMS])

c:\tools\miniconda3\envs\django_jalali\lib\_strptime.py in _strptime(data_string, format)
    361         raise ValueError("unconverted data remains: %s" %
--> 362                           data_string[found.end():])
    363

ValueError: unconverted data remains: :12

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django_jalali\db\models.py in parse_date(self, datetime_obj)
    275                 try:
--> 276                     t = time.strptime(datetime_obj, '%Y-%m-%d')[:3]
    277                     if t[0] > 1700:

c:\tools\miniconda3\envs\django_jalali\lib\_strptime.py in _strptime_time(data_string, format)
    570     format string."""
--> 571     tt = _strptime(data_string, format)[0]
    572     return time.struct_time(tt[:time._STRUCT_TM_ITEMS])

c:\tools\miniconda3\envs\django_jalali\lib\_strptime.py in _strptime(data_string, format)
    361         raise ValueError("unconverted data remains: %s" %
--> 362                           data_string[found.end():])
    363

ValueError: unconverted data remains:  12:12:12

During handling of the above exception, another exception occurred:

ValidationError                           Traceback (most recent call last)
<ipython-input-6-8128a45adbce> in <module>
----> 1 BarTime.objects.create(name="Test", datetime="1398-04-31 12:12:12")

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\manager.py in manager_method(self, *args, **kwargs)
     80         def create_method(name, method):
     81             def manager_method(self, *args, **kwargs):
---> 82                 return getattr(self.get_queryset(), name)(*args, **kwargs)
     83             manager_method.__name__ = method.__name__
     84             manager_method.__doc__ = method.__doc__

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\query.py in create(self, **kwargs)
    420         obj = self.model(**kwargs)
    421         self._for_write = True
--> 422         obj.save(force_insert=True, using=self.db)
    423         return obj
    424

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\base.py in save(self, force_insert, force_update, using, update_fields)
    739
    740         self.save_base(using=using, force_insert=force_insert,
--> 741                        force_update=force_update, update_fields=update_fields)
    742     save.alters_data = True
    743

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\base.py in save_base(self, raw, force_insert, force_update, using, update_fields)
    777             updated = self._save_table(
    778                 raw, cls, force_insert or parent_inserted,
--> 779                 force_update, using, update_fields,
    780             )
    781         # Store the database on which the object was saved

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\base.py in _save_table(self, raw, cls, force_insert, force_update, using, update_fields)
    868
    869             update_pk = meta.auto_field and not pk_set
--> 870             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
    871             if update_pk:
    872                 setattr(self, meta.pk.attname, result)

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\base.py in _do_insert(self, manager, using, fields, update_pk, raw)
    906         """
    907         return manager._insert([self], fields=fields, return_id=update_pk,
--> 908                                using=using, raw=raw)
    909
    910     def delete(self, using=None, keep_parents=False):

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\manager.py in manager_method(self, *args, **kwargs)
     80         def create_method(name, method):
     81             def manager_method(self, *args, **kwargs):
---> 82                 return getattr(self.get_queryset(), name)(*args, **kwargs)
     83             manager_method.__name__ = method.__name__
     84             manager_method.__doc__ = method.__doc__

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\query.py in _insert(self, objs, fields, return_id, raw, using, ignore_conflicts)
   1184         query = sql.InsertQuery(self.model, ignore_conflicts=ignore_conflicts)
   1185         query.insert_values(fields, objs, raw=raw)
-> 1186         return query.get_compiler(using=using).execute_sql(return_id)
   1187     _insert.alters_data = True
   1188     _insert.queryset_only = False

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\sql\compiler.py in execute_sql(self, return_id)
   1332         self.return_id = return_id
   1333         with self.connection.cursor() as cursor:
-> 1334             for sql, params in self.as_sql():
   1335                 cursor.execute(sql, params)
   1336             if not return_id:

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\sql\compiler.py in as_sql(self)
   1276             value_rows = [
   1277                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
-> 1278                 for obj in self.query.objs
   1279             ]
   1280         else:

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\sql\compiler.py in <listcomp>(.0)
   1276             value_rows = [
   1277                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
-> 1278                 for obj in self.query.objs
   1279             ]
   1280         else:

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\sql\compiler.py in <listcomp>(.0)
   1275         if self.query.fields:
   1276             value_rows = [
-> 1277                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
   1278                 for obj in self.query.objs
   1279             ]

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\sql\compiler.py in prepare_value(self, field, value)
   1216                 raise FieldError('Window expressions are not allowed in this query.')
   1217         else:
-> 1218             value = field.get_db_prep_save(value, connection=self.connection)
   1219         return value
   1220

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django\db\models\fields\__init__.py in get_db_prep_save(self, value, connection)
    787     def get_db_prep_save(self, value, connection):
    788         """Return field's value prepared for saving into a database."""
--> 789         return self.get_db_prep_value(value, connection=connection, prepared=False)
    790
    791     def has_default(self):

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django_jalali\db\models.py in get_db_prep_value(self, value, connection, prepared)
    332         # Casts dates into the format expected by the backend
    333         if not prepared:
--> 334             value = self.get_prep_value(value)
    335
    336         if isinstance(value, jdatetime.datetime):

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django_jalali\db\models.py in get_prep_value(self, value)
    315
    316     def get_prep_value(self, value):
--> 317         value = self.to_python(value)
    318         if value is not None and settings.USE_TZ and timezone.is_naive(value):
    319             try:

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django_jalali\db\models.py in to_python(self, value)
    304                 raise exceptions.ValidationError(
    305                     self.error_messages['invalid'])
--> 306         return self.parse_date(value)
    307
    308     def pre_save(self, model_instance, add):

c:\tools\miniconda3\envs\django_jalali\lib\site-packages\django_jalali\db\models.py in parse_date(self, datetime_obj)
    285                 except ValueError:
    286                     raise exceptions.ValidationError(
--> 287                         self.error_messages['invalid'])
    288
    289     def from_db_value(self, value, expression, connection, context):

ValidationError: ['Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.']
ajahansh commented 5 years ago

The problem is that jalali parsing is not available.

ajahansh commented 5 years ago

parse_date function needs to parse gregorian from database and jalali from input stream which is not doing correctly at the moment, will fail for days of month not in gregorian calendar. Function needs to be rewritten, Unfortunately no time to do so on my behalf. Will use DateTimeField for the time being and write custom fields in the form to show time in jalali.