-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,153 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from django.contrib import admin | ||
from .models import Profile | ||
|
||
admin.site.register(Profile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class UsersConfig(AppConfig): | ||
"""Configuration for the 'users' app. | ||
Args: | ||
AppConfig (type): Base class for application configuration. | ||
Attributes: | ||
name (str): The name of the app. | ||
""" | ||
# default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'users' | ||
|
||
def ready(self): | ||
"""Registers signals when the application is ready. | ||
""" | ||
import users.signals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
from django import forms | ||
from django.core.validators import RegexValidator | ||
from django.contrib.auth.models import User | ||
from django.contrib.auth.forms import UserCreationForm | ||
from .models import Profile | ||
from phonenumber_field.formfields import PhoneNumberField | ||
|
||
|
||
class UserRegisterForm(UserCreationForm): | ||
"""Form for user registration. | ||
Args: | ||
UserCreationForm (class): Django's built-in form for user creation. | ||
Attributes: | ||
email (EmailField): Field for the user's email. | ||
phone_number (PhoneNumberField): Field for the user's phone number, | ||
allows blank values. | ||
""" | ||
email = forms.EmailField() | ||
phone_number = PhoneNumberField(required=False) | ||
|
||
class Meta: | ||
"""Metadata for the UserRegisterForm. | ||
Attributes: | ||
model (class): The User model. | ||
fields (list): The fields to include in the form. | ||
""" | ||
model = User | ||
fields = [ | ||
'username', | ||
'email', | ||
'password1', | ||
'password2', | ||
'phone_number' | ||
] | ||
|
||
def save(self, commit=True): | ||
"""Save the user and create/update the associated profile. | ||
Args: | ||
commit (bool, optional): Whether to commit the changes. | ||
Default True. | ||
Returns: | ||
User: The user object. | ||
""" | ||
user = super().save(commit=False) | ||
user.email = self.cleaned_data['email'] | ||
|
||
if commit: | ||
user.save() | ||
|
||
# Create or update the associated profile | ||
profile, created = Profile.objects.get_or_create(user=user) | ||
phone_number = self.cleaned_data['phone_number'] | ||
|
||
# clean phone number | ||
phone_number_value = phone_number.raw_input | ||
if phone_number_value[0] == "+": | ||
phone_number_value = phone_number_value[1:] | ||
if phone_number_value[0] == "0": | ||
phone_number_value = "254" + phone_number_value[1:] | ||
|
||
profile.phone_number = phone_number_value | ||
|
||
if commit: | ||
profile.save() | ||
|
||
return user | ||
|
||
|
||
class UserUpdateForm(forms.ModelForm): | ||
"""Form for updating user information. | ||
Args: | ||
forms (module): Django forms module. | ||
Attributes: | ||
email (EmailField): Field for the user's email address. | ||
""" | ||
email = forms.EmailField() | ||
|
||
class Meta: | ||
"""Metadata for the UserUpdateForm. | ||
Attributes: | ||
model (class): The User model. | ||
fields (list): The fields to include in the form. | ||
""" | ||
model = User | ||
fields = ['username', 'email'] | ||
|
||
|
||
class ProfileUpdateForm(forms.ModelForm): | ||
"""Form for updating user profile information. | ||
Args: | ||
forms (module): The Django forms module. | ||
Attributes: | ||
phone_number (PhoneNumberField): A user's phone number, allows blank. | ||
""" | ||
phone_number = PhoneNumberField(required=False) | ||
|
||
class Meta: | ||
"""Metadata for the ProfileUpdateForm. | ||
Attributes: | ||
model (class): The Profile model. | ||
fields (list): The fields to include in the form. | ||
""" | ||
model = Profile | ||
fields = ['image', 'phone_number'] | ||
|
||
def save(self, commit=True): | ||
"""Saves the profile and processes the phone number. | ||
Args: | ||
commit (bool, optional): Indicates whether to commit the changes. | ||
Default True. | ||
Returns: | ||
Profile: The profile object. | ||
""" | ||
# Call the superclass's save() method to save the form data | ||
profile = super(ProfileUpdateForm, self).save(commit=False) | ||
|
||
# Process the phone number | ||
phone_number = self.cleaned_data.get('phone_number') | ||
if phone_number: | ||
phone_number_value = phone_number.raw_input | ||
|
||
if phone_number_value[0] == "+": | ||
phone_number_value = phone_number_value[1:] | ||
if phone_number_value[0] == "0": | ||
phone_number_value = "254" + phone_number_value[1:] | ||
|
||
# Save the processed phone number to the profile | ||
profile.phone_number = phone_number_value | ||
|
||
if commit: | ||
profile.save() | ||
|
||
return profile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 4.2.7 on 2023-11-12 04:54 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Profile', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('image', models.ImageField(default='default.jpg', upload_to='profile_pics')), | ||
('User', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.7 on 2023-11-15 09:54 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('users', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name='profile', | ||
old_name='User', | ||
new_name='user', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.7 on 2023-11-15 15:27 | ||
|
||
from django.db import migrations | ||
import phonenumber_field.modelfields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('users', '0002_rename_user_profile_user'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='profile', | ||
name='phone_number', | ||
field=phonenumber_field.modelfields.PhoneNumberField(blank=True, max_length=128, null=True, region=None), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.7 on 2023-11-20 07:28 | ||
|
||
from django.db import migrations | ||
import phonenumber_field.modelfields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('users', '0003_profile_phone_number'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='profile', | ||
name='phone_number', | ||
field=phonenumber_field.modelfields.PhoneNumberField(blank=True, help_text='Please enter your name...', max_length=128, null=True, region=None), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.7 on 2023-12-08 09:46 | ||
|
||
from django.db import migrations | ||
import phonenumber_field.modelfields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('users', '0004_alter_profile_phone_number'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='profile', | ||
name='phone_number', | ||
field=phonenumber_field.modelfields.PhoneNumberField(blank=True, help_text='Phone Number...', max_length=128, null=True, region=None), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.db import models | ||
from django.contrib.auth.models import AbstractUser, User | ||
from PIL import Image | ||
from phonenumber_field.modelfields import PhoneNumberField | ||
|
||
|
||
class Profile(models.Model): | ||
"""Model representing user profiles. | ||
Args: | ||
models (module): The Django models module. | ||
Attributes: | ||
user (OneToOneField): A one-to-one relationship with the User model, | ||
specifying the user associated with the profile. | ||
phone_number (PhoneNumberField): A phone number field allowing blank | ||
and null values, with optional help text. | ||
image (ImageField): An image field with a default image and a | ||
specified upload path for profile pictures. | ||
Returns: | ||
str: A string repr of the class, showing the associated username. | ||
""" | ||
user = models.OneToOneField(User, on_delete=models.CASCADE) | ||
phone_number = PhoneNumberField( | ||
blank=True, null=True, help_text=u"Phone Number...") | ||
image = models.ImageField(default='default.jpg', upload_to='profile_pics') | ||
|
||
def __str__(self): | ||
return f'{self.user.username} Profile' | ||
|
||
# image compression using PIL disabled for AWS storage | ||
|
||
# def save(self, *args, **kwargs): | ||
# super().save(*args, **kwargs) | ||
|
||
# img = Image.open(self.image.path) | ||
# if img.height > 300 or img.width > 300: | ||
# output_size = (300, 300) | ||
# img.thumbnail(output_size) | ||
# print(vars(self.image)) | ||
# img.save(self.image.path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from django.db.models.signals import post_save | ||
from django.contrib.auth.models import User | ||
from django.dispatch import receiver | ||
from .models import Profile | ||
|
||
|
||
@receiver(post_save, sender=User) | ||
def create_profile(sender, instance, created, **kwargs): | ||
"""Creates a user profile when a new user is created. | ||
Args: | ||
sender (signal): The signal for user creation. | ||
instance (object): The user object created. | ||
created (bool): A flag indicating whether the user is newly created. | ||
""" | ||
if created: | ||
Profile.objects.create(user=instance) | ||
|
||
|
||
@receiver(post_save, sender=User) | ||
def save_profile(sender, instance, **kwargs): | ||
"""Saves user profile information when a user is created or updated. | ||
Args: | ||
sender (signal): The model object that is the source of the signal. | ||
instance (object): The user object. | ||
""" | ||
instance.profile.save() |
Oops, something went wrong.