From 48964c65c662cb787b1fd782a3479d2d7167a685 Mon Sep 17 00:00:00 2001 From: reactVishnu Date: Sat, 9 Dec 2023 18:09:24 +0530 Subject: [PATCH] Added Feature #2 --- wallet_hub/core/__init__.py | 0 wallet_hub/core/admin.py | 5 + wallet_hub/core/apps.py | 6 ++ wallet_hub/core/migrations/0001_initial.py | 35 ++++++ wallet_hub/core/migrations/0002_details.py | 41 +++++++ .../migrations/0003_rename_details_detail.py | 17 +++ .../core/migrations/0004_alter_detail_user.py | 20 ++++ wallet_hub/core/migrations/__init__.py | 0 wallet_hub/core/models.py | 102 ++++++++++++++++++ wallet_hub/core/views.py | 3 + wallet_hub/wallet_hub/settings.py | 24 +++-- 11 files changed, 244 insertions(+), 9 deletions(-) create mode 100644 wallet_hub/core/__init__.py create mode 100644 wallet_hub/core/admin.py create mode 100644 wallet_hub/core/apps.py create mode 100644 wallet_hub/core/migrations/0001_initial.py create mode 100644 wallet_hub/core/migrations/0002_details.py create mode 100644 wallet_hub/core/migrations/0003_rename_details_detail.py create mode 100644 wallet_hub/core/migrations/0004_alter_detail_user.py create mode 100644 wallet_hub/core/migrations/__init__.py create mode 100644 wallet_hub/core/models.py create mode 100644 wallet_hub/core/views.py diff --git a/wallet_hub/core/__init__.py b/wallet_hub/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wallet_hub/core/admin.py b/wallet_hub/core/admin.py new file mode 100644 index 0000000..7729551 --- /dev/null +++ b/wallet_hub/core/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Detail +# Register your models here. + +admin.site.register(Detail) diff --git a/wallet_hub/core/apps.py b/wallet_hub/core/apps.py new file mode 100644 index 0000000..8115ae6 --- /dev/null +++ b/wallet_hub/core/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class CoreConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'core' diff --git a/wallet_hub/core/migrations/0001_initial.py b/wallet_hub/core/migrations/0001_initial.py new file mode 100644 index 0000000..abc95fc --- /dev/null +++ b/wallet_hub/core/migrations/0001_initial.py @@ -0,0 +1,35 @@ +# Generated by Django 5.0 on 2023-12-09 11:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('email', models.EmailField(max_length=100, unique=True)), + ('name', models.CharField(max_length=255)), + ('is_active', models.BooleanField(default=True)), + ('is_staff', models.BooleanField(default=False)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/wallet_hub/core/migrations/0002_details.py b/wallet_hub/core/migrations/0002_details.py new file mode 100644 index 0000000..336fb2e --- /dev/null +++ b/wallet_hub/core/migrations/0002_details.py @@ -0,0 +1,41 @@ +# Generated by Django 5.0 on 2023-12-09 11:41 + +import core.models +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Details', + fields=[ + ('e_id', models.AutoField(auto_created=True, primary_key=True, serialize=False)), + ('first_name', models.CharField(max_length=100)), + ('last_name', models.CharField(max_length=100)), + ('address', models.CharField(max_length=252)), + ('aadhar_no', models.CharField(max_length=12)), + ('pan_no', models.CharField(max_length=10)), + ('profile_pic', models.ImageField(blank=True, null=True, upload_to=core.models.user_image_upload_path)), + ('aadhar_file', models.FileField(blank=True, null=True, upload_to=core.models.user_aadhar_upload_path)), + ('pan_file', models.FileField(blank=True, null=True, upload_to=core.models.user_pan_upload_path)), + ('blood_group', models.CharField(max_length=5)), + ('designation', models.CharField(blank=True, max_length=200, null=True)), + ('employee_code', models.CharField(editable=False, max_length=255, null=True)), + ('date_of_birth', models.DateField(blank=True, null=True)), + ('current_package', models.IntegerField(blank=True, null=True)), + ('monthly_income', models.IntegerField(blank=True, null=True)), + ('gender', models.CharField(blank=True, choices=[('male', 'Male'), ('female', 'Female'), ('other', 'Other')], max_length=10, null=True)), + ('marital_status', models.CharField(blank=True, choices=[('married', 'Married'), ('single', 'Single'), ('divorced', 'Divorced')], max_length=10, null=True)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/wallet_hub/core/migrations/0003_rename_details_detail.py b/wallet_hub/core/migrations/0003_rename_details_detail.py new file mode 100644 index 0000000..4751b38 --- /dev/null +++ b/wallet_hub/core/migrations/0003_rename_details_detail.py @@ -0,0 +1,17 @@ +# Generated by Django 5.0 on 2023-12-09 11:45 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0002_details'), + ] + + operations = [ + migrations.RenameModel( + old_name='Details', + new_name='Detail', + ), + ] diff --git a/wallet_hub/core/migrations/0004_alter_detail_user.py b/wallet_hub/core/migrations/0004_alter_detail_user.py new file mode 100644 index 0000000..a8723a2 --- /dev/null +++ b/wallet_hub/core/migrations/0004_alter_detail_user.py @@ -0,0 +1,20 @@ +# Generated by Django 5.0 on 2023-12-09 11:55 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0003_rename_details_detail'), + ] + + operations = [ + migrations.AlterField( + model_name='detail', + name='user', + field=models.OneToOneField(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/wallet_hub/core/migrations/__init__.py b/wallet_hub/core/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wallet_hub/core/models.py b/wallet_hub/core/models.py new file mode 100644 index 0000000..532c5a6 --- /dev/null +++ b/wallet_hub/core/models.py @@ -0,0 +1,102 @@ +from django.db import models +from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin +from django.contrib.auth import get_user_model +from django.utils import timezone + + +# User Manager +class UserManager(BaseUserManager): + def create_user(self, email, password=None, **extra_fields): + """Create, save and return a new user""" + if not email: + raise ValueError('User must have an email address.') + user = self.model(email=self.normalize_email(email), **extra_fields) + user.set_password(password) + user.save(using=self._db) + + return user + + def create_superuser(self, email, password=None): + """For creating a superuser or an admin user which is going to have the access for django admin panel.""" + user = self.create_user(email, password) + user.is_staff = True + user.is_superuser = True + user.save(using=self._db) + + return user + + +# Create your models here. +class User(AbstractBaseUser, PermissionsMixin): + """ + User in the system + """ + email = models.EmailField(max_length=100, unique=True) + name = models.CharField(max_length=255) + is_active = models.BooleanField(default=True) + is_staff = models.BooleanField(default=False) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + USERNAME_FIELD = 'email' + objects = UserManager() + + +def user_image_upload_path(instance, filename): + # Assuming the user's name is stored in a field called 'name' + filename = 'profile_pic.jpeg' + return f'profile/{instance.user.email}/{filename}' + + +def user_aadhar_upload_path(instance, filename): + # Assuming the user's name is stored in a field called 'name' + return f'aadhar/{instance.user.email}/{filename}' + + +def user_pan_upload_path(instance, filename): + # Assuming the user's name is stored in a field called 'name' + return f'pan/{instance.user.email}/{filename}' + + +class Detail(models.Model): + e_id = models.AutoField(primary_key=True, auto_created=True, serialize=False) + user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE, editable=False) + first_name = models.CharField(max_length=100) + last_name = models.CharField(max_length=100) + address = models.CharField(max_length=252) + aadhar_no = models.CharField(max_length=12) + pan_no = models.CharField(max_length=10) + profile_pic = models.ImageField(upload_to=user_image_upload_path, null=True, blank=True, ) + aadhar_file = models.FileField(upload_to=user_aadhar_upload_path, null=True, blank=True, ) + pan_file = models.FileField(upload_to=user_pan_upload_path, null=True, blank=True, ) + blood_group = models.CharField(max_length=5) + designation = models.CharField(max_length=200, null=True, blank=True) + employee_code = models.CharField(max_length=255, null=True, editable=False) + date_of_birth = models.DateField(null=True, blank=True) + current_package = models.IntegerField(null=True, blank=True) + monthly_income = models.IntegerField(null=True, blank=True) + GENDER_CHOICES = [ + ('male', 'Male'), + ('female', 'Female'), + ('other', 'Other'), + ] + gender = models.CharField(max_length=10, choices=GENDER_CHOICES, null=True, blank=True) + MARITIAL_CHOICES = [ + ('married', 'Married'), + ('single', 'Single'), + ('divorced', 'Divorced') + ] + marital_status = models.CharField(max_length=10, choices=MARITIAL_CHOICES, null=True, blank=True) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + def save(self, *args, **kwargs): + self.updated_at = timezone.now() + super().save(*args, **kwargs) + + # Set the employee_code based on e_id after saving + self.employee_code = f'emp{str(self.e_id).zfill(4)}' + super().save(update_fields=['employee_code'], *args, **kwargs) + + def __str__(self): + return f'User: {self.user.name}' diff --git a/wallet_hub/core/views.py b/wallet_hub/core/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/wallet_hub/core/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/wallet_hub/wallet_hub/settings.py b/wallet_hub/wallet_hub/settings.py index 5810089..ed1c028 100644 --- a/wallet_hub/wallet_hub/settings.py +++ b/wallet_hub/wallet_hub/settings.py @@ -1,5 +1,5 @@ """ -Django settings for wallet_hub project. +Django's settings for wallet_hub project. Generated by 'django-admin startproject' using Django 5.0. @@ -11,23 +11,22 @@ """ from pathlib import Path +import os # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-628zhv_ztevm!3#+#6=_4v%1%jx()n6@x*#@xkja230cq*=629' +SECRET_KEY = 'django-insecure-628zhv_ztevm!3#+#6=_4v%1%jx()n6@x*#@xkja230cq*=629' # noqa # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = [ @@ -37,6 +36,10 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'storages', + 'rest_framework', + 'drf_spectacular', + 'core', # Core App ] MIDDLEWARE = [ @@ -69,7 +72,6 @@ WSGI_APPLICATION = 'wallet_hub.wsgi.application' - # Database # https://docs.djangoproject.com/en/5.0/ref/settings/#databases @@ -80,7 +82,6 @@ } } - # Password validation # https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators @@ -99,7 +100,6 @@ }, ] - # Internationalization # https://docs.djangoproject.com/en/5.0/topics/i18n/ @@ -111,13 +111,19 @@ USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.0/howto/static-files/ STATIC_URL = 'static/' - +STATIC_ROOT = os.path.join(BASE_DIR, 'static') # Default primary key field type # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +# Media Files (Which user uploads) +MEDIA_ROOT = BASE_DIR / 'static/media' +MEDIA_URL = '/media/' + +# User Model for this entire application +AUTH_USER_MODEL = 'core.User'