diff --git a/server/db.sqlite3 b/server/db.sqlite3 new file mode 100644 index 0000000000..f75d540c1b Binary files /dev/null and b/server/db.sqlite3 differ diff --git a/server/djangoapp/templates/djangoapp/about.html b/server/djangoapp/templates/djangoapp/about.html new file mode 100644 index 0000000000..bb525d5275 --- /dev/null +++ b/server/djangoapp/templates/djangoapp/about.html @@ -0,0 +1,5 @@ + +

+Welcome to Best Cars dealership, home to the best cars in North America. We sell domestic and imported cars at reasonable prices. +

+ \ No newline at end of file diff --git a/server/djangoapp/templates/djangoapp/contact.html b/server/djangoapp/templates/djangoapp/contact.html new file mode 100644 index 0000000000..8c7b4dfc63 --- /dev/null +++ b/server/djangoapp/templates/djangoapp/contact.html @@ -0,0 +1,4 @@ + +

email: email@email.com

+

phone: +1 985 2236 145

+ \ No newline at end of file diff --git a/server/djangoapp/templates/djangoapp/index.html b/server/djangoapp/templates/djangoapp/index.html index 1a9ee6e39a..f080196ed0 100644 --- a/server/djangoapp/templates/djangoapp/index.html +++ b/server/djangoapp/templates/djangoapp/index.html @@ -14,9 +14,35 @@ - - This is the index page of your Django app! + diff --git a/server/djangoapp/templates/djangoapp/registration.html b/server/djangoapp/templates/djangoapp/registration.html index ae11ea4b71..4c4eba58f0 100644 --- a/server/djangoapp/templates/djangoapp/registration.html +++ b/server/djangoapp/templates/djangoapp/registration.html @@ -6,6 +6,22 @@ - +
+
+

Sign Up

+
+ + + + + + + + +
+ {% csrf_token %} + +
+
\ No newline at end of file diff --git a/server/djangoapp/urls.py b/server/djangoapp/urls.py index 37b1c89d01..a14f2412c0 100644 --- a/server/djangoapp/urls.py +++ b/server/djangoapp/urls.py @@ -10,18 +10,25 @@ # name the URL # path for about view + path(route='about', view=views.about, name='About Us'), + # path for contact us view + path(route='contact', view=views.contact, name='contact'), + # path for registration + path('registration/', views.registration_request, name='registration'), # path for login + path('login/', views.login_request, name='login'), # path for logout + path('logout/', views.logout_request, name='logout'), + # path for dealer reviews view path(route='', view=views.get_dealerships, name='index'), - # path for dealer reviews view # path for add a review view diff --git a/server/djangoapp/views.py b/server/djangoapp/views.py index 61cc664da0..fd80e41ddb 100644 --- a/server/djangoapp/views.py +++ b/server/djangoapp/views.py @@ -19,23 +19,82 @@ # Create an `about` view to render a static about page # def about(request): -# ... +def about(request): + context = {} + if request.method == "GET": + return render(request, 'djangoapp/about.html', context) # Create a `contact` view to return a static contact page #def contact(request): +def contact(request): + context = {} + if request.method == "GET": + return render(request, 'djangoapp/contact.html', context) # Create a `login_request` view to handle sign in request # def login_request(request): -# ... +def login_request(request): + context = {} + # Handles POST request + if request.method == "POST": + # Get username and password from request.POST dictionary + username = request.POST['username'] + password = request.POST['psw'] + # Try to check if provide credential can be authenticated + user = authenticate(username=username, password=password) + if user is not None: + # If user is valid, call login method to login current user + login(request, user) + return redirect('djangoapp:contact') + else: + # If not, return to login page again + return render(request, 'djangoapp/index.html', context) + else: + return render(request, 'djangoapp/index.html', context) # Create a `logout_request` view to handle sign out request # def logout_request(request): -# ... +def logout_request(request): + # Get the user object based on session id in request + print("Log out the user `{}`".format(request.user.username)) + # Logout user in the request + logout(request) + # Redirect user back to course list view + return redirect('djangoapp:index') # Create a `registration_request` view to handle sign up request # def registration_request(request): -# ... +def registration_request(request): + context = {} + # If it is a GET request, just render the registration page + if request.method == 'GET': + return render(request, 'djangoapp/registration.html', context) + # If it is a POST request + elif request.method == 'POST': + # Get user information from request.POST + username = request.POST['username'] + password = request.POST['psw'] + first_name = request.POST['firstname'] + last_name = request.POST['lastname'] + user_exist = False + try: + # Check if user already exists + User.objects.get(username=username) + user_exist = True + except: + # If not, simply log this is a new user + logger.debug("{} is new user".format(username)) + # If it is a new user + if not user_exist: + # Create user in auth_user table + user = User.objects.create_user(username=username, first_name=first_name, last_name=last_name, + password=password) + # Login the user and redirect to course list page + login(request, user) + return redirect("djangoapp:popular_course_list") + else: + return render(request, 'djangoapp/user_registration.html', context) # Update the `get_dealerships` view to render the index page with a list of dealerships def get_dealerships(request): diff --git a/server/requirements.txt b/server/requirements.txt index 2851cca128..dc42689d89 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,6 +1,5 @@ requests Django==3.1.3 -Pillow==8.0.1 gunicorn==20.1.0 ibm-cloud-sdk-core==3.10.0 ibm-watson==5.2.2