diff options
| author | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-23 10:18:03 +0000 |
|---|---|---|
| committer | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-23 10:18:03 +0000 |
| commit | 174c8a0295c7e3f4782b5a0769de92e3f94bc801 (patch) | |
| tree | f3904f5b731f6314c55341bd5f859484d8cd8ec1 /django | |
| parent | 15f1da053220394061d0f37447e545597cee6cd9 (diff) | |
| parent | 17f62269c2168549b76ee6204a1423a6e6f4e5f5 (diff) | |
i18n: merged to [992] from trunk
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@993 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/management.py | 2 | ||||
| -rw-r--r-- | django/core/meta/__init__.py | 5 | ||||
| -rw-r--r-- | django/core/template/loaders/app_directories.py | 14 | ||||
| -rw-r--r-- | django/utils/dateformat.py | 37 | ||||
| -rw-r--r-- | django/utils/timesince.py | 18 | ||||
| -rw-r--r-- | django/utils/tzinfo.py | 52 | ||||
| -rw-r--r-- | django/views/decorators/auth.py | 19 |
7 files changed, 122 insertions, 25 deletions
diff --git a/django/core/management.py b/django/core/management.py index 564697776d..3b973c28da 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -299,7 +299,7 @@ def init(): cursor = db.db.cursor() for sql in get_sql_create(core) + get_sql_create(auth) + get_sql_initial_data(core) + get_sql_initial_data(auth): cursor.execute(sql) - cursor.execute("INSERT INTO %s (domain, name) VALUES ('mysite.com', 'My Django site')" % core.Site._meta.db_table) + cursor.execute("INSERT INTO %s (domain, name) VALUES ('example.com', 'Example site')" % core.Site._meta.db_table) except Exception, e: sys.stderr.write("Error: The database couldn't be initialized.\n%s\n" % e) try: diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index f934f9dd6c..2e39f72e95 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -1360,9 +1360,10 @@ def function_get_sql_clause(opts, **kwargs): def function_get_in_bulk(opts, klass, *args, **kwargs): id_list = args and args[0] or kwargs['id_list'] assert id_list != [], "get_in_bulk() cannot be passed an empty list." - kwargs['where'] = ["%s.id IN (%s)" % (opts.db_table, ",".join(map(str, id_list)))] + kwargs['where'] = ["%s.%s IN (%s)" % (opts.db_table, opts.pk.column, ",".join(['%s'] * len(id_list)))] + kwargs['params'] = id_list obj_list = function_get_list(opts, klass, **kwargs) - return dict([(o.id, o) for o in obj_list]) + return dict([(getattr(o, opts.pk.column), o) for o in obj_list]) def function_get_latest(opts, klass, does_not_exist_exception, **kwargs): kwargs['order_by'] = ('-' + opts.get_latest_by,) diff --git a/django/core/template/loaders/app_directories.py b/django/core/template/loaders/app_directories.py index 5afb18e2f5..b8bd0d6169 100644 --- a/django/core/template/loaders/app_directories.py +++ b/django/core/template/loaders/app_directories.py @@ -1,6 +1,7 @@ # Wrapper for loading templates from "template" directories in installed app packages. from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION +from django.core.exceptions import ImproperlyConfigured from django.core.template import TemplateDoesNotExist import os @@ -8,8 +9,17 @@ import os app_template_dirs = [] for app in INSTALLED_APPS: i = app.rfind('.') - m, a = app[:i], app[i+1:] - mod = getattr(__import__(m, '', '', [a]), a) + if i == -1: + m, a = app, None + else: + m, a = app[:i], app[i+1:] + try: + if a is None: + mod = __import__(m, '', '', []) + else: + mod = getattr(__import__(m, '', '', [a]), a) + except ImportError, e: + raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0]) template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') if os.path.isdir(template_dir): app_template_dirs.append(template_dir) diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index 3620558f09..9913c3f8c4 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -12,8 +12,9 @@ Usage: """ from django.utils.dates import MONTHS, MONTHS_AP, WEEKDAYS +from django.utils.tzinfo import LocalTimezone from calendar import isleap -import re +import re, time re_formatchars = re.compile(r'(?<!\\)([aABdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])') re_escaped = re.compile(r'\\(.)') @@ -40,7 +41,9 @@ class TimeFormat(Formatter): def A(self): "'AM' or 'PM'" - return self.a().upper() + if self.data.hour > 11: + return 'PM' + return 'AM' def B(self): "Swatch Internet time" @@ -100,8 +103,12 @@ class TimeFormat(Formatter): class DateFormat(TimeFormat): year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] - def __init__(self, d): - self.data = d + def __init__(self, dt): + # Accepts either a datetime or date object. + self.data = dt + self.timezone = getattr(dt, 'tzinfo', None) + if hasattr(self.data, 'hour') and not self.timezone: + self.timezone = LocalTimezone(dt) def d(self): "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'" @@ -119,6 +126,13 @@ class DateFormat(TimeFormat): "'1' if Daylight Savings Time, '0' otherwise." raise NotImplementedError + def I(self): + "'1' if Daylight Savings Time, '0' otherwise." + if self.timezone.dst(self.data): + return '1' + else: + return '0' + def j(self): "Day of the month without leading zeros; i.e. '1' to '31'" return self.data.day @@ -149,11 +163,12 @@ class DateFormat(TimeFormat): def O(self): "Difference to Greenwich time in hours; e.g. '+0200'" - raise NotImplementedError + tz = self.timezone.utcoffset(self.data) + return "%+03d%02d" % (tz.seconds // 3600, (tz.seconds // 60) % 60) def r(self): "RFC 822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" - raise NotImplementedError + return self.format('D, j M Y H:i:s O') def S(self): "English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'" @@ -174,11 +189,15 @@ class DateFormat(TimeFormat): def T(self): "Time zone of this machine; e.g. 'EST' or 'MDT'" - raise NotImplementedError + name = self.timezone.tzname(self.data) + if name is None: + name = self.format('O') + return name def U(self): "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)" - raise NotImplementedError + off = self.timezone.utcoffset(self.data) + return int(time.mktime(self.data.timetuple())) + off.seconds * 60 def w(self): "Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)" @@ -229,7 +248,7 @@ class DateFormat(TimeFormat): """Time zone offset in seconds (i.e. '-43200' to '43200'). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.""" - raise NotImplementedError + return self.timezone.utcoffset(self.data).seconds def format(value, format_string): "Convenience function" diff --git a/django/utils/timesince.py b/django/utils/timesince.py index c11cef0342..5b22fde58c 100644 --- a/django/utils/timesince.py +++ b/django/utils/timesince.py @@ -1,4 +1,5 @@ -import time, math, datetime +import datetime, math, time +from django.utils.tzinfo import LocalTimezone def timesince(d, now=None): """ @@ -6,7 +7,6 @@ def timesince(d, now=None): as a nicely formatted string, e.g "10 minutes" Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since """ - original = time.mktime(d.timetuple()) chunks = ( (60 * 60 * 24 * 365, 'year'), (60 * 60 * 24 * 30, 'month'), @@ -14,9 +14,17 @@ def timesince(d, now=None): (60 * 60, 'hour'), (60, 'minute') ) - if not now: - now = time.time() - since = now - original + if now: + t = time.mktime(now) + else: + t = time.localtime() + if d.tzinfo: + tz = LocalTimezone() + else: + tz = None + now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) + delta = now - d + since = delta.days * 24 * 60 * 60 + delta.seconds # Crazy iteration syntax because we need i to be current index for i, (seconds, name) in zip(range(len(chunks)), chunks): count = math.floor(since / seconds) diff --git a/django/utils/tzinfo.py b/django/utils/tzinfo.py new file mode 100644 index 0000000000..cc9f028e91 --- /dev/null +++ b/django/utils/tzinfo.py @@ -0,0 +1,52 @@ +"Implementation of tzinfo classes for use with datetime.datetime." + +import time +from datetime import timedelta, tzinfo + +class FixedOffset(tzinfo): + "Fixed offset in minutes east from UTC." + def __init__(self, offset): + self.__offset = timedelta(minutes=offset) + self.__name = "%+03d%02d" % (offset // 60, offset % 60) + + def __repr__(self): + return self.__name + + def utcoffset(self, dt): + return self.__offset + + def tzname(self, dt): + return self.__name + + def dst(self, dt): + return timedelta(0) + +class LocalTimezone(tzinfo): + "Proxy timezone information from time module." + def __init__(self, dt): + tzinfo.__init__(self, dt) + self._tzname = time.tzname[self._isdst(dt)] + + def __repr__(self): + return self._tzname + + def utcoffset(self, dt): + if self._isdst(dt): + return timedelta(seconds=-time.altzone) + else: + return timedelta(seconds=-time.timezone) + + def dst(self, dt): + if self._isdst(dt): + return timedelta(seconds=-time.altzone) - timedelta(seconds=-time.timezone) + else: + return timedelta(0) + + def tzname(self, dt): + return time.tzname[self._isdst(dt)] + + def _isdst(self, dt): + tt = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1) + stamp = time.mktime(tt) + tt = time.localtime(stamp) + return tt.tm_isdst > 0 diff --git a/django/views/decorators/auth.py b/django/views/decorators/auth.py index ae27fe33a1..f543a6aa48 100644 --- a/django/views/decorators/auth.py +++ b/django/views/decorators/auth.py @@ -1,12 +1,19 @@ -def login_required(view_func): +def user_passes_test(view_func, test_func): """ - Decorator for views that checks that the user is logged in, redirecting - to the log-in page if necessary. + Decorator for views that checks that the user passes the given test, + redirecting to the log-in page if necessary. The test should be a callable + that takes the user object and returns True if the user passes. """ from django.views.auth.login import redirect_to_login def _checklogin(request, *args, **kwargs): - if request.user.is_anonymous(): - return redirect_to_login(request.path) - else: + if test_func(request.user): return view_func(request, *args, **kwargs) + return redirect_to_login(request.path) return _checklogin + +def login_required(view_func): + """ + Decorator for views that checks that the user is logged in, redirecting + to the log-in page if necessary. + """ + return user_passes_test(lambda u: not u.is_anonymous()) |
