Skip to content

Commit 9dde416

Browse files
authored
Merge pull request #49 from OmkarTendolkar/main
Added user registration functionality with modal popup
2 parents 73428ac + 72ea513 commit 9dde416

File tree

15 files changed

+213
-3
lines changed

15 files changed

+213
-3
lines changed

Ecommerce_Project/accounts/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

Ecommerce_Project/accounts/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountsConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'accounts'

Ecommerce_Project/accounts/migrations/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

Ecommerce_Project/accounts/urls.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.urls import path
2+
from . import views
3+
4+
app_name = 'accounts'
5+
6+
urlpatterns = [
7+
path('register', views.RegisterUser, name='register_user')
8+
]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import re
2+
from django.shortcuts import render, redirect
3+
from django.contrib.auth.models import User
4+
from django.contrib import messages
5+
from django.http import JsonResponse
6+
7+
# Create your views here.
8+
def RegisterUser(request):
9+
if request.method == 'POST' and request.headers.get("X-Requested-With") == "XMLHttpRequest":
10+
firstname = request.POST.get('firstname')
11+
lastname = request.POST.get('lastname')
12+
username = request.POST.get('username')
13+
email = request.POST.get('email')
14+
password = request.POST.get('password')
15+
16+
username_pattern = r'^[a-zA-Z0-9_]{3,30}$'
17+
18+
if not firstname or not lastname or not username or not email or not password:
19+
return JsonResponse({'success': False, 'error': "All fields are required!"}, status=400)
20+
21+
if not re.match(username_pattern, username):
22+
return JsonResponse({'success': False, 'error': 'Username must be 3-30 characters long and can only contain letters, digits, and underscores!'}, status=400)
23+
24+
if User.objects.filter(username = username).exists():
25+
return JsonResponse({'success': False, 'error': "Username already exists!"}, status=400)
26+
27+
if User.objects.filter(email = email).exists():
28+
return JsonResponse({'success': False, 'error': "Email Address is already registered!"}, status=400)
29+
30+
if len(password) < 6:
31+
return JsonResponse({'success': False, 'error': "Password must be at least 6 characters long"}, status=400)
32+
33+
user = User.objects.create_user(
34+
first_name = firstname,
35+
last_name = lastname,
36+
username = username,
37+
email = email,
38+
password = password
39+
)
40+
user.save()
41+
42+
return JsonResponse({'success': True, 'message': "Account created successfully"}, status=200)
43+
44+
return JsonResponse({'success': False, 'error': "Invalid Request"}, status=400)

Ecommerce_Project/ecommerceproject/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# Application definition
3232

3333
INSTALLED_APPS = [
34+
'accounts',
3435
'shop',
3536
'search_app',
3637
'cart',

Ecommerce_Project/ecommerceproject/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
path('admin/', admin.site.urls),
2323
path('',include('shop.urls')),
2424
path('search/',include('search_app.urls')),
25-
path('cart/',include('cart.urls'))
25+
path('cart/',include('cart.urls')),
26+
path('auth/', include('accounts.urls'))
2627

2728
]
2829
if settings.DEBUG:

0 commit comments

Comments
 (0)