Permalink
Browse files

* Add models.UpdateInfoBaseModel

* Update decorators.render_to
  • Loading branch information...
1 parent 3c40150 commit 7ceb3c982c74a563a8a8c660d41b7b0da1f89882 @jedie committed Jun 14, 2011
Showing with 85 additions and 7 deletions.
  1. +17 −0 .project
  2. +3 −0 README.textile
  3. +1 −1 django_tools/__init__.py
  4. +9 −6 django_tools/decorators.py
  5. +55 −0 django_tools/models.py
View
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>django-tools</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.python.pydev.PyDevBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.python.pydev.pythonNature</nature>
+ </natures>
+</projectDescription>
View
@@ -31,6 +31,9 @@ to:
h2. history
+* v0.17
+** Add models.UpdateInfoBaseModel
+** Update decorators.render_to
* v0.16.4
** Bugfix: """get_db_prep_save() got an unexpected keyword argument 'connection'""" when save a SignSeparatedModelField()
* v0.16.3
View
@@ -6,7 +6,7 @@
import subprocess
-__version__ = (0, 16, 4)
+__version__ = (0, 17, 0, "pre")
VERSION_STRING = '.'.join(str(part) for part in __version__)
View
@@ -18,6 +18,7 @@
import sys
import warnings
+from django.contrib import messages
try:
from functools import wraps
except ImportError:
@@ -96,8 +97,6 @@ def PyLucidPluginFoo(request):
def PyLucidPluginFoo(request):
bar = Bar.object.all()
return {'bar': bar, 'template_name': 'foo/template.html'}
-
- TODO: merge render_to() and render_pylucid_response()
"""
def renderer(function):
@wraps(function)
@@ -106,7 +105,11 @@ def wrapper(request, *args, **kwargs):
if not isinstance(context, dict):
if debug:
- print function.__name__, template_name, type(context), function.func_code
+ msg = (
+ "renter_to info: %s (template: %r)"
+ " has not return a dict, has return: %r (%r)"
+ ) % (function.__name__, template_name, type(context), function.func_code)
+ messages.info(request, msg)
return context
template_name2 = context.pop('template_name', template_name)
@@ -117,9 +120,9 @@ def wrapper(request, *args, **kwargs):
response = render_to_response(template_name2, context, context_instance=RequestContext(request))
if debug:
- print "render debug for %r (template: %r):" % (function.__name__, template_name2)
- print "local view context:", context
- print "response:", response.content
+ messages.info(request, "render debug for %r (template: %r):" % (function.__name__, template_name2))
+ messages.info(request, "local view context:", context)
+ messages.info(request, "response:", response.content)
return response
return wrapper
View
@@ -0,0 +1,55 @@
+# coding: utf-8
+
+"""
+ models stuff
+ ~~~~~~~~~~~~
+
+ :copyleft: 2011 by the django-tools team, see AUTHORS for more details.
+ :license: GNU GPL v3 or above, see LICENSE for more details.
+"""
+
+
+from django.db import models
+from django.contrib.auth.models import User
+
+
+from django_tools.middlewares import ThreadLocal
+
+
+
+
+class UpdateInfoBaseModel(models.Model):
+ """
+ Base model with update info attributes, used by many models.
+ The createby and lastupdateby ForeignKey would be automaticly updated. This needs the
+ request object as the first argument in the save method.
+ """
+ objects = models.Manager()
+
+ createtime = models.DateTimeField(auto_now_add=True, help_text="Create time")
+ lastupdatetime = models.DateTimeField(auto_now=True, help_text="Time of the last change.")
+
+ createby = models.ForeignKey(User, editable=False, related_name="%(class)s_createby",
+ null=True, blank=True, # <- If the model used outsite a real request (e.g. unittest, db shell)
+ help_text="User how create this entry.")
+ lastupdateby = models.ForeignKey(User, editable=False, related_name="%(class)s_lastupdateby",
+ null=True, blank=True, # <- If the model used outsite a real request (e.g. unittest, db shell)
+ help_text="User as last edit this entry.")
+
+ def save(self, *args, **kwargs):
+ """
+ Automatic update createby and lastupdateby attributes with the request object witch must be
+ the first argument.
+ """
+ current_user = ThreadLocal.get_current_user()
+
+ if current_user and isinstance(current_user, User):
+ if self.pk == None or kwargs.get("force_insert", False): # New model entry
+ self.createby = current_user
+ self.lastupdateby = current_user
+
+ return super(UpdateInfoBaseModel, self).save(*args, **kwargs)
+
+ class Meta:
+ abstract = True
+

0 comments on commit 7ceb3c9

Please sign in to comment.