1- # coding: utf-8
2-
31"""
42 models stuff
53 ~~~~~~~~~~~~
64
7- :copyleft: 2011-2017 by the django-tools team, see AUTHORS for more details.
5+ :copyleft: 2011-2019 by the django-tools team, see AUTHORS for more details.
86 :license: GNU GPL v3 or above, see LICENSE for more details.
97"""
108
11- from __future__ import absolute_import , division , print_function
129
1310from django .conf import settings
1411from django .contrib .auth import get_user_model
1512from django .db import models
16- from django .utils .encoding import python_2_unicode_compatible
13+ from django .utils .timezone import now
1714
1815# https://github.com/jedie/django-tools
1916from django_tools .middlewares import ThreadLocal
2017
21- try :
22- from django .utils .timezone import now
23- except ImportError :
24- from datetime import datetime
25- now = datetime .now
26-
2718
28- @python_2_unicode_compatible
2919class UpdateTimeBaseModel (models .Model ):
3020 """
3121 Base model to automatically set:
@@ -36,10 +26,11 @@ class UpdateTimeBaseModel(models.Model):
3626 see also:
3727 https://github.com/jezdez/django-dbtemplates/commit/2f27327bebe7f2e7b33e5cfb0db517f53a1b9701#commitcomment-1396126
3828 """
29+
3930 createtime = models .DateTimeField (default = now , editable = False , help_text = "Create time" )
4031 lastupdatetime = models .DateTimeField (default = now , editable = False , help_text = "Time of the last change." )
4132
42- def __str__ (self ): # to be overwritten
33+ def __str__ (self ): # to be overwritten
4334 return "model instance ID:%s" % self .pk
4435
4536 def save (self , * args , ** kwargs ):
@@ -50,7 +41,6 @@ class Meta:
5041 abstract = True
5142
5243
53- @python_2_unicode_compatible
5444class UpdateUserBaseModel (models .Model ):
5545 """
5646 Base model to automatically set:
@@ -59,18 +49,27 @@ class UpdateUserBaseModel(models.Model):
5949
6050 Important: "threadlocals middleware" must be used!
6151 """
62- createby = models .ForeignKey (settings .AUTH_USER_MODEL , editable = False , related_name = "%(class)s_createby" ,
63- null = True , blank = True , # <- If the model used outside a real request (e.g. unittest, db shell)
52+
53+ createby = models .ForeignKey (
54+ settings .AUTH_USER_MODEL ,
55+ editable = False ,
56+ related_name = "%(class)s_createby" ,
57+ null = True ,
58+ blank = True , # <- If the model used outside a real request (e.g. unittest, db shell)
6459 help_text = "User how create this entry." ,
65- on_delete = models .SET_NULL
60+ on_delete = models .SET_NULL ,
6661 )
67- lastupdateby = models .ForeignKey (settings .AUTH_USER_MODEL , editable = False , related_name = "%(class)s_lastupdateby" ,
68- null = True , blank = True , # <- If the model used outside a real request (e.g. unittest, db shell)
62+ lastupdateby = models .ForeignKey (
63+ settings .AUTH_USER_MODEL ,
64+ editable = False ,
65+ related_name = "%(class)s_lastupdateby" ,
66+ null = True ,
67+ blank = True , # <- If the model used outside a real request (e.g. unittest, db shell)
6968 help_text = "User as last edit this entry." ,
70- on_delete = models .SET_NULL
69+ on_delete = models .SET_NULL ,
7170 )
7271
73- def __str__ (self ): # to be overwritten
72+ def __str__ (self ): # to be overwritten
7473 return "model instance ID:%s" % self .pk
7574
7675 def save (self , * args , ** kwargs ):
@@ -79,7 +78,7 @@ def save(self, *args, **kwargs):
7978 if current_user :
8079 User = get_user_model ()
8180 if isinstance (current_user , User ):
82- if self .pk is None or kwargs .get ("force_insert" , False ): # New model entry
81+ if self .pk is None or kwargs .get ("force_insert" , False ): # New model entry
8382 self .createby = current_user
8483 self .lastupdateby = current_user
8584
@@ -100,5 +99,6 @@ class UpdateInfoBaseModel(UpdateTimeBaseModel, UpdateUserBaseModel):
10099
101100 Important: "threadlocals middleware" must be used!
102101 """
102+
103103 class Meta :
104104 abstract = True
0 commit comments