-
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
10 changed files
with
160 additions
and
131 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,26 +1,23 @@ | ||
# admin.py | ||
|
||
from django.contrib import admin | ||
from django.contrib.auth.admin import UserAdmin | ||
from django.contrib.auth.models import User | ||
from .models import Profile, UserSession | ||
from django.contrib.auth.admin import UserAdmin | ||
from .models import * | ||
|
||
|
||
class ProfileInline(admin.StackedInline): | ||
model = Profile | ||
class AccountInline(admin.StackedInline): | ||
model = Account | ||
can_delete = False | ||
verbose_name_plural = 'Profile' | ||
verbose_name_plural = 'Accounts' | ||
|
||
|
||
class CustomUserAdmin(UserAdmin): | ||
inlines = (ProfileInline,) | ||
class CustomizeUserAdmin(UserAdmin): | ||
inlines = (AccountInline,) | ||
|
||
|
||
admin.site.unregister(User) | ||
admin.site.register(User, CustomUserAdmin) | ||
|
||
|
||
class ProfileAdmin(admin.ModelAdmin): | ||
list_display = ['user', 'middle_name'] # Display fields in the admin list view | ||
|
||
admin.site.register(Profile, ProfileAdmin) | ||
admin.site.register(User, CustomizeUserAdmin) | ||
|
||
admin.site.register(UserSession) | ||
admin.site.register(Channels) | ||
admin.site.register(Subchannels) |
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
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 |
---|---|---|
@@ -1,15 +1,30 @@ | ||
from django.forms import ModelForm, inlineformset_factory | ||
from django import forms | ||
from django.contrib.auth.models import User | ||
from .models import * | ||
from .models import Account | ||
|
||
class CombinedAccountCreationForm(forms.ModelForm): | ||
username = forms.CharField(max_length=150) | ||
email = forms.EmailField() | ||
password1 = forms.CharField(widget=forms.PasswordInput) | ||
# Additional fields from AccountCreationFormAdd | ||
gender = forms.CharField(max_length=6, widget=forms.Select(choices=[('MALE', 'male'), ('FEMALE', 'female')])) | ||
student_number = forms.CharField(max_length=12) | ||
middle_name = forms.CharField(max_length=12) | ||
|
||
class profileForm(ModelForm): | ||
class Meta: | ||
model = Profile | ||
fields = '__all__' | ||
|
||
|
||
class userForm(ModelForm): | ||
class Meta: | ||
model = User | ||
fields = ['first_name', 'last_name', 'email'] | ||
fields = ['username', 'email', 'password1'] | ||
|
||
def save(self, commit=True): | ||
user = super(CombinedAccountCreationForm, self).save(commit=False) | ||
user.set_password(self.cleaned_data["password1"]) | ||
if commit: | ||
user.save() | ||
# Save additional fields into Account model | ||
account = Account.objects.create( | ||
user=user, | ||
gender=self.cleaned_data['gender'], | ||
student_number=self.cleaned_data['student_number'], | ||
middle_name=self.cleaned_data['middle_name'] | ||
) | ||
return user, account |
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
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
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
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
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
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 |
---|---|---|
@@ -1,86 +1,75 @@ | ||
# chat/views.py | ||
from django.http import JsonResponse | ||
from django.shortcuts import render, redirect | ||
from django.shortcuts import render, redirect, get_object_or_404 | ||
from django.contrib import messages | ||
from .forms import * | ||
from django.contrib.auth.decorators import login_required | ||
from django.contrib.auth import authenticate, login | ||
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm | ||
from django.contrib.auth.models import User | ||
from .models import * | ||
from .forms import * | ||
|
||
|
||
def home(request): | ||
return render(request, "chat/splash-page.html") | ||
|
||
|
||
def index(request): | ||
users_all = User.objects.all() | ||
context={'users_all':users_all} | ||
return render(request, "chat/index.html",context) | ||
channels = Channels.objects.all() | ||
context = {'channels': channels} | ||
return render(request, 'chat/index.html', context) | ||
|
||
|
||
def channels(request, channel_name): | ||
channel = get_object_or_404(Channels, channel_name=channel_name) | ||
subchannels = Subchannels.objects.all() | ||
context = {'channel': channel, 'subchannel': subchannels} | ||
return render(request, 'chat/channel.html', context) | ||
|
||
|
||
def subchannels(request, channel_name, sub_channel_name): | ||
channel = get_object_or_404(Channels, channel_name=channel_name) | ||
subchannels = Subchannels.objects.filter(subchannel_name=sub_channel_name) | ||
context = {'subchannel': subchannels, 'channel_name': channel} | ||
return render(request, 'chat/subchannel.html', context) | ||
|
||
|
||
def loginPage(request): | ||
page = 'login' | ||
|
||
context = {'page': page} | ||
|
||
if request.method == 'POST': | ||
if request.method == "POST": | ||
username = request.POST.get('username') | ||
password = request.POST.get('password') | ||
user = authenticate(request, username=username, password=password) | ||
if user is not None: | ||
# Authenticate the user with the provided username and password | ||
user = authenticate(username=username, password=password) | ||
|
||
if user is None: | ||
# Display an error message if authentication fails (invalid password) | ||
messages.error(request, "Invalid Password") | ||
return redirect('login') | ||
else: | ||
# Log in the user and redirect to the home page upon successful login | ||
login(request, user) | ||
# Create a new session for the user | ||
request.session.create() | ||
# Redirect to a success page or home page | ||
return redirect('index') | ||
else: | ||
# Handle invalid login credentials | ||
return render(request, 'chat/register_login.html', {'error': 'Invalid username or password'}) | ||
else: | ||
# Render the login form | ||
return render(request, 'chat/register_login.html', context) | ||
|
||
# if request.method == "POST": | ||
# username = request.POST.get('username') | ||
# password = request.POST.get('password') | ||
# # Authenticate the user with the provided username and password | ||
# user = authenticate(username=username, password=password) | ||
# | ||
# if user is None: | ||
# # Display an error message if authentication fails (invalid password) | ||
# messages.error(request, "Invalid Password") | ||
# return redirect('login') | ||
# else: | ||
# # Log in the user and redirect to the home page upon successful login | ||
# login(request, user) | ||
# return redirect('index') | ||
|
||
def registerPage(request): | ||
if request.method == 'POST': | ||
first_name = request.POST.get('first_name') | ||
middle_name = request.POST.get('middle_name') # Retrieve middle name field | ||
last_name = request.POST.get('last_name') | ||
username = request.POST.get('username') | ||
password = request.POST.get('password') | ||
|
||
user = User.objects.create_user( | ||
first_name=first_name, | ||
last_name=last_name, | ||
username=username | ||
) | ||
|
||
profile = Profile.objects.create( | ||
user=user, | ||
middle_name=middle_name | ||
) | ||
return render(request, 'chat/register_login.html', context) | ||
|
||
user.set_password(password) | ||
user.save() | ||
|
||
return redirect('login') | ||
|
||
return render(request, 'chat/register_login.html') | ||
|
||
def registerPage(request): | ||
if request.method == 'POST': | ||
form = CombinedAccountCreationForm(request.POST) | ||
if form.is_valid(): | ||
form.save() | ||
# Redirect to a success page or login page | ||
return redirect('login') | ||
else: | ||
# Form is invalid, render back to the user with error messages | ||
for field, errors in form.errors.items(): | ||
for error in errors: | ||
messages.error(request, f"Error in {field}: {error}") | ||
else: | ||
form = CombinedAccountCreationForm() | ||
|
||
def room(request, room_name): | ||
return render(request, "chat/room.html", {"room_name": room_name}) | ||
context = {'form': form} | ||
return render(request, 'chat/register_login.html', context) |
Oops, something went wrong.