Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
boxabhi committed Mar 9, 2022
0 parents commit 6bc6afa
Show file tree
Hide file tree
Showing 256 changed files with 47,352 additions and 0 deletions.
Empty file added accounts/__init__.py
Empty file.
Binary file added accounts/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added accounts/__pycache__/admin.cpython-310.pyc
Binary file not shown.
Binary file added accounts/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file added accounts/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file added accounts/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added accounts/__pycache__/views.cpython-310.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin

# Register your models here.
from .models import Profile

admin.site.register(Profile)
6 changes: 6 additions & 0 deletions accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
33 changes: 33 additions & 0 deletions accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.0.3 on 2022-03-07 04:59

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Profile',
fields=[
('uid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('created_at', models.DateTimeField(auto_now=True)),
('updated_at', models.DateTimeField(auto_now_add=True)),
('is_email_verified', models.BooleanField(default=False)),
('email_token', models.CharField(blank=True, max_length=100, null=True)),
('profile_image', models.ImageField(upload_to='profile')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
},
),
]
Empty file added accounts/migrations/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
29 changes: 29 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

from django.db import models
from django.contrib.auth.models import User
from base.models import BaseModel
from django.db.models.signals import post_save
from django.dispatch import receiver
import uuid
from base.emails import send_account_activation_email

class Profile(BaseModel):
user = models.OneToOneField(User , on_delete=models.CASCADE , related_name="profile")
is_email_verified = models.BooleanField(default=False)
email_token = models.CharField(max_length=100 , null=True , blank=True)
profile_image = models.ImageField(upload_to = 'profile')



@receiver(post_save , sender = User)
def send_email_token(sender , instance , created , **kwargs):
try:
if created:
email_token = str(uuid.uuid4())
Profile.objects.create(user = instance , email_token = email_token)
email = instance.email
send_account_activation_email(email , email_token)

except Exception as e:
print(e)

3 changes: 3 additions & 0 deletions accounts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from accounts.views import login_page,register_page , activate_email

urlpatterns = [
path('login/' , login_page , name="login" ),
path('register/' , register_page , name="register"),
path('activate/<email_token>/' , activate_email , name="activate_email"),
]
77 changes: 77 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from cmath import log
from tkinter import E
from django.shortcuts import redirect, render
from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib.auth import authenticate , login , logout
from django.http import HttpResponseRedirect,HttpResponse
# Create your views here.
from .models import Profile


def login_page(request):

if request.method == 'POST':
email = request.POST.get('email')
password = request.POST.get('password')
user_obj = User.objects.filter(username = email)

if not user_obj.exists():
messages.warning(request, 'Account not found.')
return HttpResponseRedirect(request.path_info)


if not user_obj[0].profile.is_email_verified:
messages.warning(request, 'Your account is not verified.')
return HttpResponseRedirect(request.path_info)

user_obj = authenticate(username = email , password= password)
if user_obj:
login(request , user_obj)
return redirect('/')



messages.warning(request, 'Invalid credentials')
return HttpResponseRedirect(request.path_info)


return render(request ,'accounts/login.html')


def register_page(request):

if request.method == 'POST':
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
email = request.POST.get('email')
password = request.POST.get('password')
user_obj = User.objects.filter(username = email)

if user_obj.exists():
messages.warning(request, 'Email is already taken.')
return HttpResponseRedirect(request.path_info)

print(email)

user_obj = User.objects.create(first_name = first_name , last_name= last_name , email = email , username = email)
user_obj.set_password(password)
user_obj.save()

messages.success(request, 'An email has been sent on your mail.')
return HttpResponseRedirect(request.path_info)


return render(request ,'accounts/register.html')




def activate_email(request , email_token):
try:
user = Profile.objects.get(email_token= email_token)
user.is_email_verified = True
user.save()
return redirect('/')
except Exception as e:
return HttpResponse('Invalid Email token')
Empty file added base/__init__.py
Empty file.
Binary file added base/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added base/__pycache__/emails.cpython-310.pyc
Binary file not shown.
Binary file added base/__pycache__/models.cpython-310.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions base/emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import imp
from django.conf import settings
from django.core.mail import send_mail




def send_account_activation_email(email , email_token):
return
subject = 'Your account needs to be verified'
email_from = settings.EMAIL_HOST_USER
message = f'Hi, click on the link to activate your account http://127.0.0.1:8000/accounts/activate/{email_token}'
send_mail(subject , message , email_from , [email])
15 changes: 15 additions & 0 deletions base/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import models
import uuid



class BaseModel(models.Model):
uid = models.UUIDField(primary_key=True , editable=False , default=uuid.uuid4)
created_at = models.DateTimeField(auto_now= True)
updated_at = models.DateTimeField(auto_now_add= True)

class Meta:
abstract = True



Binary file added db.sqlite3
Binary file not shown.
Empty file added ecomm/__init__.py
Empty file.
Binary file added ecomm/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added ecomm/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added ecomm/__pycache__/settings.cpython-310.pyc
Binary file not shown.
Binary file added ecomm/__pycache__/settings.cpython-39.pyc
Binary file not shown.
Binary file added ecomm/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added ecomm/__pycache__/wsgi.cpython-310.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions ecomm/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for ecomm project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecomm.settings')

application = get_asgi_application()
Loading

0 comments on commit 6bc6afa

Please sign in to comment.