Skip to content

Commit e42e6af

Browse files
Implemented user sign up with email (rather than username as it is default)
1 parent a14f8b0 commit e42e6af

File tree

13 files changed

+153
-132
lines changed

13 files changed

+153
-132
lines changed

pugliaeventi/settings.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
'django.contrib.sessions',
3838
'django.contrib.messages',
3939
'django.contrib.staticfiles',
40-
'recommender.apps.RecommenderConfig',
40+
'recommender_webapp.apps.RecommenderConfig',
4141
]
4242

4343
MIDDLEWARE = [
@@ -124,3 +124,5 @@
124124
# https://docs.djangoproject.com/en/2.1/howto/static-files/
125125

126126
STATIC_URL = '/static/'
127+
128+
AUTH_USER_MODEL = 'recommender_webapp.User'

recommender/admin.py

-3
This file was deleted.

recommender/migrations/0001_initial.py

-63
This file was deleted.

recommender/models.py

-64
This file was deleted.
File renamed without changes.

recommender_webapp/admin.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from django.contrib import admin
2+
from django.utils.translation import ugettext_lazy as _
3+
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
4+
5+
# Register your models here.
6+
7+
from .models import User
8+
9+
10+
@admin.register(User)
11+
class UserAdmin(DjangoUserAdmin):
12+
"""Define admin model for custom User model with no email field."""
13+
14+
fieldsets = (
15+
(None, {'fields': ('email', 'password')}),
16+
(_('Personal info'), {'fields': ('first_name', 'last_name')}),
17+
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
18+
'groups', 'user_permissions')}),
19+
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
20+
)
21+
add_fieldsets = (
22+
(None, {
23+
'classes': ('wide',),
24+
'fields': ('email', 'password1', 'password2'),
25+
}),
26+
)
27+
list_display = ('email', 'first_name', 'last_name', 'is_staff')
28+
search_fields = ('email', 'first_name', 'last_name')
29+
ordering = ('email',)

recommender/apps.py recommender_webapp/apps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33

44
class RecommenderConfig(AppConfig):
5-
name = 'recommender'
5+
name = 'recommender_webapp'
File renamed without changes.
File renamed without changes.
File renamed without changes.

recommender_webapp/models.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from django.db import models
2+
from django.dispatch import receiver
3+
from django.db.models.signals import post_save
4+
from recommender_webapp.common.utils import ChoiceEnum
5+
from django.utils.translation import ugettext_lazy as _
6+
from django.contrib.auth.base_user import BaseUserManager
7+
from django.contrib.auth.models import User, AbstractUser
8+
9+
10+
class UserManager(BaseUserManager):
11+
"""Define a model manager for User model with no username field."""
12+
13+
use_in_migrations = True
14+
15+
def _create_user(self, email, password, **extra_fields):
16+
"""Create and save a User with the given email and password."""
17+
if not email:
18+
raise ValueError('The given email must be set')
19+
email = self.normalize_email(email)
20+
user = self.model(email=email, **extra_fields)
21+
user.set_password(password)
22+
user.save(using=self._db)
23+
return user
24+
25+
def create_user(self, email, password=None, **extra_fields):
26+
"""Create and save a regular User with the given email and password."""
27+
extra_fields.setdefault('is_staff', False)
28+
extra_fields.setdefault('is_superuser', False)
29+
return self._create_user(email, password, **extra_fields)
30+
31+
def create_superuser(self, email, password, **extra_fields):
32+
"""Create and save a SuperUser with the given email and password."""
33+
extra_fields.setdefault('is_staff', True)
34+
extra_fields.setdefault('is_superuser', True)
35+
36+
if extra_fields.get('is_staff') is not True:
37+
raise ValueError('Superuser must have is_staff=True.')
38+
if extra_fields.get('is_superuser') is not True:
39+
raise ValueError('Superuser must have is_superuser=True.')
40+
41+
return self._create_user(email, password, **extra_fields)
42+
43+
44+
class User(AbstractUser):
45+
"""User model."""
46+
47+
username = None
48+
email = models.EmailField(_('email address'), unique=True)
49+
50+
USERNAME_FIELD = 'email'
51+
REQUIRED_FIELDS = []
52+
53+
objects = UserManager()
54+
55+
56+
class Profile(models.Model):
57+
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.SET_NULL)
58+
userId = models.AutoField(primary_key=True)
59+
location = models.CharField(max_length=40, blank=False)
60+
birth_date = models.DateField(null=True, blank=True)
61+
bio = models.TextField(max_length=500, blank=True)
62+
profession = models.CharField(max_length=40, blank=True)
63+
empathy = models.FloatField(null=True, blank=True)
64+
65+
def __str__(self):
66+
return str(self.userId) + '|' + self.location + '|' + self.birth_date.strftime('%d/%m/%Y')
67+
68+
69+
@receiver(post_save, sender=User)
70+
def update_user_profile(sender, instance, created, **kwargs):
71+
if created:
72+
Profile.objects.create(user=instance)
73+
instance.profile.save()
74+
75+
76+
class Place(models.Model):
77+
placeId = models.IntegerField(primary_key=True)
78+
name = models.CharField(max_length=100, blank=False)
79+
location = models.CharField(max_length=40, blank=False)
80+
freeEntry = models.BooleanField()
81+
bere = models.BooleanField()
82+
mangiare = models.BooleanField()
83+
benessere = models.BooleanField()
84+
dormire = models.BooleanField()
85+
goloso = models.BooleanField()
86+
libri = models.BooleanField()
87+
romantico = models.BooleanField()
88+
museo = models.BooleanField()
89+
spiaggia = models.BooleanField()
90+
teatro = models.BooleanField()
91+
92+
def __str__(self):
93+
return str(self.placeId) + '|' + self.name + '|' + self.location
94+
95+
96+
class Mood(ChoiceEnum):
97+
angry = 1
98+
joyful = 2
99+
sad = 3
100+
101+
102+
class Companionship(ChoiceEnum):
103+
withFriends = 1
104+
alone = 2
105+
106+
107+
class Rating(models.Model):
108+
id = models.AutoField(primary_key=True)
109+
userId = models.ForeignKey(Profile, on_delete=models.PROTECT, blank=False)
110+
mood = models.CharField(max_length=1, choices=Mood.choices(), blank=False)
111+
companionship = models.CharField(max_length=1, choices=Companionship.choices(), blank=False)
112+
placeId = models.ForeignKey(Place, on_delete=models.PROTECT, blank=False)
113+
rating = models.IntegerField(blank=False)
114+
115+
class Meta:
116+
unique_together = ('userId', 'mood', 'companionship', 'placeId')
117+
118+
def __str__(self):
119+
return str(self.userId) + '|' + str(self.mood) + '|' + str(self.companionship) \
120+
+ '|' + str(self.placeId) + '|' + str(self.rating)
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)