diff --git a/chat/admin.py b/chat/admin.py index d0a3b3d..d378a91 100644 --- a/chat/admin.py +++ b/chat/admin.py @@ -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) \ No newline at end of file +admin.site.register(Channels) +admin.site.register(Subchannels) \ No newline at end of file diff --git a/chat/consumers.py b/chat/consumers.py index 17bbd50..e6a43de 100644 --- a/chat/consumers.py +++ b/chat/consumers.py @@ -5,7 +5,6 @@ from channels.db import database_sync_to_async from django.db import close_old_connections - class ChatConsumer(AsyncWebsocketConsumer): async def connect(self): # Close old database connections to prevent usage outside of async context diff --git a/chat/forms.py b/chat/forms.py index 4463333..b6f289d 100644 --- a/chat/forms.py +++ b/chat/forms.py @@ -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 diff --git a/chat/models.py b/chat/models.py index 07b59b2..a9e7df0 100644 --- a/chat/models.py +++ b/chat/models.py @@ -2,22 +2,28 @@ from django.contrib.auth.models import User -# Create your models here. -class Profile(models.Model): +class Account(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) - student_number = models.CharField(null=True, blank=True, max_length=11) - middle_name = models.CharField(null=True, blank=True, max_length=100) + gender = models.CharField( + max_length=6, + choices=[('MALE', 'male'), ('FEMALE', 'female')] + ) + student_number = models.CharField(max_length=12, null=True, blank=True) + middle_name = models.CharField(max_length=12, null=True, blank=True) def __str__(self): - return f"{self.user.username} Profile" + return self.user.username +class Channels(models.Model): + host = models.ForeignKey(User, on_delete=models.CASCADE) + channel_name = models.CharField(max_length=12, null=True, blank=True) + def __str__(self): + return self.channel_name -from django.contrib.auth.models import User - -class UserSession(models.Model): - host = models.ForeignKey(User, on_delete=models.CASCADE, null = True) - session_key = models.CharField(max_length=40, unique=True) - room_name = models.CharField(max_length=100) +class Subchannels(models.Model): + subchannel_root = models.ForeignKey(Channels, on_delete=models.CASCADE) + subchannel_name = models.CharField(max_length=12, null=True, blank=True) + members = models.ManyToManyField(User, related_name='members', blank=True) def __str__(self): - return f"{self.host.username}'s session ({self.session_key})" + return self.subchannel_name diff --git a/chat/routing.py b/chat/routing.py index 4c48d8f..e0c8753 100644 --- a/chat/routing.py +++ b/chat/routing.py @@ -4,5 +4,5 @@ from . import consumers websocket_urlpatterns = [ - re_path(r"ws/chat/(?P\w+)/$", consumers.ChatConsumer.as_asgi()), + re_path(r"ws/chat/(?P\w+)/(?P\w+)/$", consumers.ChatConsumer.as_asgi()), ] \ No newline at end of file diff --git a/chat/templates/chat/index.html b/chat/templates/chat/index.html index 28caf61..ec6b426 100644 --- a/chat/templates/chat/index.html +++ b/chat/templates/chat/index.html @@ -8,23 +8,29 @@ What chat room would you like to enter?
-
- - + {% for channel in channels %} + {{ channel.channel_name }}
+ {% endfor %} - +{#
#} +{# #} +{##} +{# #} +{##} +{# #} \ No newline at end of file diff --git a/chat/templates/chat/register_login.html b/chat/templates/chat/register_login.html index 8488106..d4d1c29 100644 --- a/chat/templates/chat/register_login.html +++ b/chat/templates/chat/register_login.html @@ -14,27 +14,42 @@ {% else %} -
-
- {% csrf_token %} - - +
+ {% if messages %} +
    + {% for message in messages %} +
  • {{ message }}
  • + {% endfor %} +
+ {% endif %} - - + + {% csrf_token %} - - +
+

- - +
+

- - +
+

- - -
+
+

+ +
+

+ +
+

+ + + +
{% endif %} diff --git a/chat/urls.py b/chat/urls.py index 35aa796..6059325 100644 --- a/chat/urls.py +++ b/chat/urls.py @@ -4,9 +4,10 @@ from . import views urlpatterns = [ - path("", views.index, name="index"), + path("index/", views.index, name="index"), + path("/", views.channels, name="channels"), + path("//", views.subchannels, name="subchannels"), path("home/", views.home, name="home"), path("login/", views.loginPage, name="login"), path("register/", views.registerPage, name="register"), - path("/", views.room, name="room"), ] \ No newline at end of file diff --git a/chat/views.py b/chat/views.py index a268065..28eb0b0 100644 --- a/chat/views.py +++ b/chat/views.py @@ -1,12 +1,12 @@ # 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): @@ -14,73 +14,62 @@ def home(request): 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) diff --git a/nexusapp/asgi.py b/nexusapp/asgi.py index 41b6d4c..78ee2fd 100644 --- a/nexusapp/asgi.py +++ b/nexusapp/asgi.py @@ -15,6 +15,7 @@ from chat.routing import websocket_urlpatterns from django.conf import settings +from . import settings os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nexusapp.settings")