1
|
|
|
# coding: utf-8 |
2
|
|
|
from dj_diabetes.tools import page_it, right_now |
3
|
|
|
|
4
|
|
|
from django.contrib.auth.models import User |
5
|
|
|
from django.urls import reverse |
6
|
|
|
from django.db import models |
7
|
|
|
from django.db.models.signals import post_save |
8
|
|
|
from django.views.generic.edit import ModelFormMixin |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class HatModel(models.Model): |
12
|
|
|
""" |
13
|
|
|
HatModel |
14
|
|
|
""" |
15
|
|
|
title = models.CharField(max_length=255, unique=True) |
16
|
|
|
created = models.DateTimeField(auto_now_add=True) |
17
|
|
|
modified = models.DateTimeField(auto_now=True) |
18
|
|
|
|
19
|
|
|
class Meta: |
20
|
|
|
abstract = True |
21
|
|
|
|
22
|
|
|
def __str__(self): |
23
|
|
|
return "%s" % self.title |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class UserProfile(models.Model): |
27
|
|
|
|
28
|
|
|
""" |
29
|
|
|
Related user to handle his profile |
30
|
|
|
""" |
31
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE) |
32
|
|
|
name = models.CharField(max_length=100, blank=True) |
33
|
|
|
birth_date = models.DateField(null=True, blank=True) |
34
|
|
|
phone = models.CharField(max_length=15, blank=True) |
35
|
|
|
address = models.TextField(null=True, blank=True) |
36
|
|
|
zipcode = models.CharField(max_length=50, blank=True) |
37
|
|
|
town = models.CharField(max_length=255, blank=True) |
38
|
|
|
role = models.CharField(max_length=250, blank=True) |
39
|
|
|
created = models.DateTimeField(auto_now_add=True) |
40
|
|
|
modified = models.DateTimeField(auto_now=True) |
41
|
|
|
|
42
|
|
|
def __str__(self): |
43
|
|
|
return "%s" % self.user |
44
|
|
|
|
45
|
|
|
|
46
|
|
View Code Duplication |
class Preferences(models.Model): |
|
|
|
|
47
|
|
|
|
48
|
|
|
""" |
49
|
|
|
Preferences |
50
|
|
|
""" |
51
|
|
|
key = models.CharField(max_length=255) |
52
|
|
|
title = models.CharField(max_length=255) |
53
|
|
|
value = models.TextField() |
54
|
|
|
created = models.DateTimeField(auto_now_add=True) |
55
|
|
|
modified = models.DateTimeField(auto_now=True) |
56
|
|
|
|
57
|
|
|
class Meta: |
58
|
|
|
verbose_name = 'Preference' |
59
|
|
|
verbose_name_plural = 'Preferences' |
60
|
|
|
|
61
|
|
|
def __str__(self): |
62
|
|
|
return "%s %s %s" % (self.key, self.title, self.value) |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def create_user_profile(sender, instance, created, **kwargs): |
66
|
|
|
""" |
67
|
|
|
function to create the record in the UserProfile model |
68
|
|
|
""" |
69
|
|
|
if created: |
70
|
|
|
UserProfile.objects.create(user=instance) |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
post_save.connect(create_user_profile, sender=User) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
""" |
77
|
|
|
Common mixin for all models of the app |
78
|
|
|
""" |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
class InitMixin(ModelFormMixin): |
82
|
|
|
""" |
83
|
|
|
Mixin to initialize the date/hour attribute of the model |
84
|
|
|
""" |
85
|
|
|
def get_initial(self): |
86
|
|
|
""" |
87
|
|
|
set the default date and hour of the date_xxx and hour_xxx |
88
|
|
|
property of the current model |
89
|
|
|
""" |
90
|
|
|
return right_now(self.model.__name__.lower()) |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
class SuccessMixin(object): |
94
|
|
|
""" |
95
|
|
|
Mixin to just return to the expected page |
96
|
|
|
where the name is based on the model name |
97
|
|
|
""" |
98
|
|
|
def get_success_url(self): |
99
|
|
|
return reverse(self.model.__name__.lower()) |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
class PaginateMixin(object): |
103
|
|
|
""" |
104
|
|
|
Mixin to just handle the Paginate behavior |
105
|
|
|
""" |
106
|
|
|
def get_context_data(self, **kw): |
107
|
|
|
data = self.model.objects.all() |
108
|
|
|
# paginator vars |
109
|
|
|
record_per_page = 3 |
110
|
|
|
page = self.request.GET.get('page') |
111
|
|
|
# paginator call |
112
|
|
|
data = page_it(data, record_per_page, page) |
113
|
|
|
context = super(PaginateMixin, self).get_context_data(**kw) |
114
|
|
|
context['data'] = data |
115
|
|
|
return context |
116
|
|
|
|