Re: FloatField, localize and Django settings

2013-01-23 Thread Michael Anckaert
Thanks for your feedback. For reference, ticket is #19656 (
https://code.djangoproject.com/ticket/19656#ticket)


2013/1/22 Wim Feijen <w...@go2people.nl>

> Hi Michael, sounds very reasonable to me. Could you please file a bug
> ticket for that at https://code.djangoproject.com/newticket ?
>
>
> On Tuesday, 22 January 2013 21:30:09 UTC+1, Michael Anckaert wrote:
>
>> Hello everyone
>>
>> I came across an issue where my CBV and an automatic ModelForm gave me
>> some headaches with localization.
>>
>> The project is localized so a float is 3,2 instead of 3.2 (comma instead
>> of dot) but the CBV UpdateView didn't handle the localization of the float.
>> After tracking through the sources I noticed that the attribute localize
>> of FloatField has to be set manually.
>>
>> Wouldn't it be sensible to localize the FloatField according to the
>> project settings instead of having to manually set it in a custom form?
>>
>> Kind regards
>> Michael Anckaert
>> michael@sinax.be
>> http://www.sinax.be
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django developers" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-developers/-/BFrsjaJD0hMJ.
> To post to this group, send email to django-developers@googlegroups.com.
> To unsubscribe from this group, send email to
> django-developers+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-developers?hl=en.
>



-- 
Kind regards
Michael Anckaert <michael.ancka...@sinax.be>
http://www.sinax.be

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



FloatField, localize and Django settings

2013-01-22 Thread Michael Anckaert
Hello everyone

I came across an issue where my CBV and an automatic ModelForm gave me some
headaches with localization.

The project is localized so a float is 3,2 instead of 3.2 (comma instead of
dot) but the CBV UpdateView didn't handle the localization of the float.
After tracking through the sources I noticed that the attribute localize of
FloatField has to be set manually.

Wouldn't it be sensible to localize the FloatField according to the project
settings instead of having to manually set it in a custom form?

Kind regards
Michael Anckaert
michael.ancka...@sinax.be
http://www.sinax.be

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Where is the authentication backend used?

2012-12-16 Thread Michael Anckaert
Hello everyone

I've tried getting my question answered by the folks over django-users but 
when searching the archives I didn't feel that I would get a sufficient 
response there. 

On my current project I need a mix of builtin Django authentication and 
oAuth2 sources. But when writing a custom authentication backend, when does 
it get used? The docs say: 

"""Once a user has authenticated, Django stores which backend was used to 
authenticate the user in the user's session, and re-uses the same backend 
for the duration of that session whenever access to the currently 
authenticated user is needed."""

The code of ModelBackend only seems to deal with permissions. Assuming an 
oAuth2 source deals with authentication my backend should only handle 
permission checks? 

Thanks for taking the time to answer this. 

Kind regards
Michael

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-developers/-/UeWpZ7m5kQYJ.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Date parsing in DateField

2009-06-16 Thread Michael Anckaert
Hello everyone,

I recently stumbled on a problem (at least for me) that you can't pass any
string to a datefield and have it parsed as a correct date. The current
implementation in SVN expects a string in the format of -MM-DD.
There is however a wonderful option in settings.py called DATE_FORMAT. The
attached patch changes the django/db/models/fields/__init__.py file so that
the DateField accepts strings in the format defined in settings.DATE_FORMAT.


Currently the code is messy to say the least, currently supported
DATE_FORMAT:
%d-%m-%Y  16-06-2009
%Y-%m-%d  2009-06-16
And all variants where there are one or two day/month digits and/or the -
delimiter is a /

I intend to make it compatible with all possible DATE_FORMAT options.

-- 
Met vriendelijke groeten,
Kind regards,
Michael Anckaert
Sinax Consultant
+32 473 779 135
http://www.sinax.be

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~--~~~~--~~--~--~---

Index: django/db/models/fields/__init__.py
===
--- django/db/models/fields/__init__.py	(revision 11012)
+++ django/db/models/fields/__init__.py	(working copy)
@@ -446,8 +446,37 @@
 defaults.update(kwargs)
 return super(CommaSeparatedIntegerField, self).formfield(**defaults)
 
-ansi_date_re = re.compile(r'^\d{4}-\d{1,2}-\d{1,2}$')
+def parse_date(value):
+	"""
+		Parses a string to a date according to the Django setting DATE_FORMAT
+		Currently supported DATE_FORMAT:
+			%d-%m-%Y	16-06-2009
+			%Y-%m-%d	2009-06-16
+			And all variants where there is one or two day/month digits and/or the - delimiter is a /
+	"""
+	DATE_FORMAT = settings.DATE_FORMAT
+	if DATE_FORMAT == "%d-%m-%Y":
+		if re.compile(r'^\d{1,2}-\d{1,2}-\d{4}$').match(value):
+			day, month, year = map(int, value.split("-"))
+			return datetime.date(year, month, day)
 
+	if DATE_FORMAT == "%d/%m/%Y":
+		if re.compile(r'^\d{1,2}/\d{1,2}/\d{4}$').match(value):
+			day, month, year = map(int, value.split("/"))
+			return datetime.date(year, month, day)
+
+	if DATE_FORMAT == "%Y-%m-%d":
+		if re.compile(r'^\d{4}-\d{1,2}-\d{1,2}$').match(value):
+			year, month, day = map(int, value.split("-"))
+			return datetime.date(year, month, day)
+
+	if DATE_FORMAT == "%Y/%m/%d":
+		if re.compile(r'^\d{4}/\d{1,2}/\d{1,2}$').match(value):
+			year, month, day = map(int, value.split("/"))
+			return datetime.date(year, month, day)
+
+	return False
+
 class DateField(Field):
 empty_strings_allowed = False
 def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs):
@@ -462,27 +491,20 @@
 return "DateField"
 
 def to_python(self, value):
-if value is None:
-return value
-if isinstance(value, datetime.datetime):
-return value.date()
-if isinstance(value, datetime.date):
-return value
+if value is None:
+		return value
+if isinstance(value, datetime.datetime):
+		return value.date()
+if isinstance(value, datetime.date):
+		return value
 
-if not ansi_date_re.search(value):
-raise exceptions.ValidationError(
-_('Enter a valid date in -MM-DD format.'))
-# Now that we have the date string in -MM-DD format, check to make
-# sure it's a valid date.
-# We could use time.strptime here and catch errors, but datetime.date
-# produces much friendlier error messages.
-year, month, day = map(int, value.split('-'))
-try:
-return datetime.date(year, month, day)
-except ValueError, e:
-msg = _('Invalid date: %s') % _(str(e))
-raise exceptions.ValidationError(msg)
+d = parse_date(value)
+if not d:
+		raise exceptions.ValidationError(
+_('Enter a valid date in the format you defined in settings.py.'))
 
+return d
+
 def pre_save(self, model_instance, add):
 if self.auto_now or (self.auto_now_add and add):
 value = datetime.datetime.now()