From f8c4b3c741d15433eb6b6161f24bde7b86417239 Mon Sep 17 00:00:00 2001 From: Nikhil <154459496+alphaplayerofdooms@users.noreply.github.com> Date: Wed, 10 Dec 2025 16:08:36 +0530 Subject: [PATCH 1/4] profile page for all users --- corpus/accounts/urls.py | 4 +- corpus/accounts/views.py | 67 ++- corpus/corpus/settings.py | 3 +- corpus/templates/accounts/edit_profile.html | 440 +++++++++--------- corpus/templates/accounts/profile.html | 42 +- corpus/templates/blog/full_post.html | 30 +- .../components/navbar_profile_dropdown.html | 6 +- corpus/templates/pages/core_box.html | 22 +- corpus/templates/pages/member_box.html | 25 +- corpus/templates/smp/program.html | 275 +++++------ corpus/templates/virtual_expo/report.html | 253 +++++----- 11 files changed, 612 insertions(+), 555 deletions(-) diff --git a/corpus/accounts/urls.py b/corpus/accounts/urls.py index 670ad956..30eccc95 100644 --- a/corpus/accounts/urls.py +++ b/corpus/accounts/urls.py @@ -32,6 +32,6 @@ PasswordResetCompleteView.as_view(), name="password_reset_complete", ), - path("profile/", profile, name="accounts_profile"), - path("profile//edit", edit_profile, name="edit_profile"), + path("profile/", profile, name="accounts_profile"), + path("profile//edit", edit_profile, name="edit_profile"), ] diff --git a/corpus/accounts/views.py b/corpus/accounts/views.py index bd32592f..0f1e28fd 100644 --- a/corpus/accounts/views.py +++ b/corpus/accounts/views.py @@ -1,5 +1,7 @@ import re +from django.contrib.auth import get_user_model + from blog.models import Post from constants import MAX_IMAGE_SIZE from django.contrib import messages @@ -18,6 +20,9 @@ from .forms import UserForm from .models import ExecutiveMember +User = get_user_model() + + # Create your views here. @@ -90,15 +95,22 @@ def signout(request): return redirect("index") -def profile(request, roll_no): - exec_member = get_object_or_404(ExecutiveMember, roll_number=roll_no) - profile_user = exec_member.user +def profile(request, pk): + profile_user = get_object_or_404(User, pk=pk) + + try: + exec_member = ExecutiveMember.objects.get(user=profile_user) + except ExecutiveMember.DoesNotExist: + exec_member = None # Get Virtual Expo Reports - reports = Report.objects.filter(reportmember__member=exec_member) - - # Get Blogs written by the Executive Member - blogs = Post.objects.filter(author=exec_member) + if exec_member: + reports = Report.objects.filter(reportmember__member=exec_member) + # Get Blogs written by the Executive Member + blogs = Post.objects.filter(author=exec_member) + else: + reports = None + blogs = None args = { "exec_member": exec_member, @@ -112,30 +124,41 @@ def profile(request, roll_no): @login_required -def edit_profile(request, roll_no): - user = request.user # Get the currently logged-in user - +def edit_profile(request, pk): + user = get_object_or_404(User, pk=pk) # Get the user explicitly by ID + + # Authorization check: Ensure logged-in user can only edit their own profile + if request.user != user: + messages.warning(request, "You are not authorized to edit this profile.") + return redirect("index") + # Check if the user has an associated ExecutiveMember record try: - executive_member = ExecutiveMember.objects.get(roll_number=roll_no, user=user) + executive_member = ExecutiveMember.objects.get(user=user) except ExecutiveMember.DoesNotExist: - messages.warning(request, "You are not authorized to edit this profile.") - return redirect("index") + executive_member = None if request.method == "POST": user_form = UserForm(request.POST, request.FILES, instance=user) - executive_member_form = ExecutiveMemberForm( - request.POST, instance=executive_member - ) - - if user_form.is_valid() and executive_member_form.is_valid(): - user_form.save() - executive_member_form.save() - return redirect("accounts_profile", roll_no=roll_no) + if executive_member: + executive_member_form = ExecutiveMemberForm( + request.POST, instance=executive_member + ) + if user_form.is_valid() and executive_member_form.is_valid(): + user_form.save() + executive_member_form.save() + return redirect("accounts_profile", pk=pk) + else: + if user_form.is_valid(): + user_form.save() + return redirect("accounts_profile", pk=pk) else: user_form = UserForm(instance=user) - executive_member_form = ExecutiveMemberForm(instance=executive_member) + if executive_member: + executive_member_form = ExecutiveMemberForm(instance=executive_member) + else: + executive_member_form = None return render( request, diff --git a/corpus/corpus/settings.py b/corpus/corpus/settings.py index a61b776e..e8dc7b20 100644 --- a/corpus/corpus/settings.py +++ b/corpus/corpus/settings.py @@ -35,7 +35,7 @@ ) # TODO: Stricter host policies -ALLOWED_HOSTS = ["localhost", "127.0.0.1", ".onrender.com"] +ALLOWED_HOSTS = ["*"] CSRF_TRUSTED_ORIGINS = ["https://*.onrender.com"] if os.getenv("ENVIRONMENT", "PRODUCTION") == "PRODUCTION": ALLOWED_HOSTS.append("ieee.nitk.ac.in") @@ -67,6 +67,7 @@ "farewell.apps.FarewellConfig", "virtual_expo.apps.VirtualExpoConfig", "blog", + 'django_extensions', "smp.apps.SmpConfig", "codeRed.apps.CoderedConfig", "chimera.apps.ChimeraConfig", diff --git a/corpus/templates/accounts/edit_profile.html b/corpus/templates/accounts/edit_profile.html index c8b906c6..1f7928b2 100644 --- a/corpus/templates/accounts/edit_profile.html +++ b/corpus/templates/accounts/edit_profile.html @@ -1,277 +1,281 @@ {% extends 'base.html' %} {% block title %} - Edit Profile | {{ user }} +Edit Profile | {{ user }} {% endblock %} {% block content %} -
-

Edit Profile

- -
- {% csrf_token %} - - -
-
-

Personal Information

-
- - -
- - - -
- {% if user.profile_pic %} - {{ user }} - {% else %} - {{ user }} - {% endif %} -
- - - - - +
+

Edit Profile

+ + + {% csrf_token %} + + +
+
+

Personal Information

+
+ + +
+ + + +
+ {% if user.profile_pic %} + {{ user }} + {% else %} + {{ user }} + {% endif %}
- -
- {% for field in user_form %} -
- - {{ field }} - {% if field.errors %} -
- {% for error in field.errors %} - {{ error }} - {% endfor %} -
- {% endif %} - {% if field.help_text %} - - {% endif %} -
- {% endfor %} -
-
-
-
+ + - {% if executive_member_form %} - -
-
-

NITK Details

-
-
- - {{ executive_member_form.edu_email }} -
-
- - {{ executive_member_form.roll_number }} -
-
- - {{ executive_member_form.reg_number }} -
-
- - {{ executive_member_form.minor_branch }} -
-
-
- -
-
-

IEEE Details

+
+ {% for field in user_form %}
- {{ executive_member_form.ieee_number }} -
-
+ {{ field }} + {% if field.errors %} +
+ {% for error in field.errors %} + {{ error }} + {% endfor %} +
+ {% endif %} + {% if field.help_text %} - {{ executive_member_form.ieee_email }} + {% endif %}
+ {% endfor %}
+
- - -
-
-

Socials

-
- -
- - {{ executive_member_form.linkedin }} - -
- - -
- - {{ executive_member_form.github }} - -
+
+ + {% if executive_member_form %} + +
+
+

NITK Details

+
+
+ + {{ executive_member_form.edu_email }} +
+
+ + {{ executive_member_form.roll_number }} +
+
+ + {{ executive_member_form.reg_number }} +
+
+ + {{ executive_member_form.minor_branch }}
- {% endif %} - - - diff --git a/corpus/templates/blog/full_post.html b/corpus/templates/blog/full_post.html index a199b03c..287ed05a 100644 --- a/corpus/templates/blog/full_post.html +++ b/corpus/templates/blog/full_post.html @@ -16,31 +16,27 @@ {% endif %} {% load markdown_extras %} -
+
{{individual_post.title}}
{% for tag in individual_post.blog_tag.all %} -
{{ tag.tag_name }}
+
{{ tag.tag_name }}
{% endfor %}
-
published on {{individual_post.published_date}}
+ -by + {{individual_post.author}}
-
+
published on {{individual_post.published_date}}
+
+
{{individual_post.text|render_function}}
-
- {% if not preview %} - - {% else %} - Back - {% endif %} +
+ {% endblock %} diff --git a/corpus/templates/components/navbar_profile_dropdown.html b/corpus/templates/components/navbar_profile_dropdown.html index af164492..294cfa74 100644 --- a/corpus/templates/components/navbar_profile_dropdown.html +++ b/corpus/templates/components/navbar_profile_dropdown.html @@ -3,8 +3,6 @@
  • Welcome, {{ user.first_name }} {{ user.last_name }}!
  • - {% if user.executivemember %} -
  • Profile
  • - {% endif %} +
  • Profile
  • Logout
  • - + \ No newline at end of file diff --git a/corpus/templates/pages/core_box.html b/corpus/templates/pages/core_box.html index 8b0edd96..59f7f872 100644 --- a/corpus/templates/pages/core_box.html +++ b/corpus/templates/pages/core_box.html @@ -1,5 +1,5 @@ {% load static %} - + - + \ No newline at end of file diff --git a/corpus/templates/pages/member_box.html b/corpus/templates/pages/member_box.html index c0207413..6fa2b0eb 100644 --- a/corpus/templates/pages/member_box.html +++ b/corpus/templates/pages/member_box.html @@ -1,5 +1,5 @@ {% load static %} - +
    - + \ No newline at end of file diff --git a/corpus/templates/smp/program.html b/corpus/templates/smp/program.html index b3dabdd3..4bddb25c 100644 --- a/corpus/templates/smp/program.html +++ b/corpus/templates/smp/program.html @@ -1,156 +1,167 @@ {% extends "smp/base.html" %} {% block title %} - {{ program.title }} - {{ block.super }} +{{ program.title }} +{{ block.super }} {% endblock %} {% block content %} - {% include "smp/header.html" with year=program.year %} +{% include "smp/header.html" with year=program.year %} - {% if preview %} -
    - Preview Mode - This is a draft version of the program +{% if preview %} +
    + Preview Mode - This is a draft version of the program +
    +{% endif %} + +
    +
    +
    +

    {{ program.title }}

    +
    + {% for sig in program.sigs %} + + {{ sig }} + + {% endfor %} +
    - {% endif %} -
    -
    -
    -

    {{ program.title }}

    -
    - {% for sig in program.sigs %} - - {{ sig }} - - {% endfor %} +
    + + {% if program.abstract %} + - -
    - - {% if program.abstract %} - - {% endif %} - {% if program.abstract %} - - {% endif %} - -
    - {{ program.description | safe }} + {% endif %} + {% if program.abstract %} + + {% endif %} - +
    + {{ program.description | safe }} +
    - -
    -

    Program Information

    + + + +
    +

    Program Information

    -
    - - +
    + + - + - - + + - +
    -
    +
    +
    -
    - {% if not preview %} -
    -

    Explore More Programs

    - - View All {{ program.year }} Programs - -
    - {% else %} - - {% endif %} +
    + {% if not preview %} + + {% else %} + + {% endif %}
    -{% endblock %} +
    +{% endblock %} \ No newline at end of file diff --git a/corpus/templates/virtual_expo/report.html b/corpus/templates/virtual_expo/report.html index 6e8c9ed5..f4e47c89 100644 --- a/corpus/templates/virtual_expo/report.html +++ b/corpus/templates/virtual_expo/report.html @@ -1,147 +1,156 @@ {% extends "virtual_expo/base.html" %} {% block title %} - {{ report.title }} - {{ block.super }} +{{ report.title }} +{{ block.super }} {% endblock %} {% block content %} - {% include "virtual_expo/header.html" with year=report.year %} +{% include "virtual_expo/header.html" with year=report.year %} - {% if preview %} -
    - Preview Mode - This is a draft version of the report +{% if preview %} +
    + Preview Mode - This is a draft version of the report +
    +{% endif %} + +
    +
    +
    +

    {{ report.title }}

    +
    + + {{ report.report_type }} + + {% for sig in report.sigs %} + + {{ sig }} + + {% endfor %} +
    - {% endif %} -
    -
    -
    -

    {{ report.title }}

    -
    - - {{ report.report_type }} - - {% for sig in report.sigs %} - - {{ sig }} - - {% endfor %} +
    + + {% if report.abstract %} + - -
    - - {% if report.abstract %} - - {% endif %} - {% if report.abstract %} - - {% endif %} - -
    - {{ report.content | safe }} + {% endif %} + {% if report.abstract %} + + {% endif %} - -
    -

    Report Information

    - -
    - - +
    + {{ report.content | safe }} +
    - + +
    +

    Report Information

    + +
    + + - - -
    +
    +
    -
    - {% if not preview %} -
    -

    Explore More Projects

    - - View All {{ report.year }} Projects - -
    - {% else %} - - {% endif %} +
    + {% if not preview %} +
    +

    Explore More Projects

    + + View All {{ report.year }} Projects + +
    + {% else %} + + {% endif %}
    +
    {% endblock %} \ No newline at end of file From 574ddd31fc805fcf8b6c0bcfac962ec939bad32f Mon Sep 17 00:00:00 2001 From: Nikhil Kottoli <154459496+NikhilKottoli@users.noreply.github.com> Date: Wed, 10 Dec 2025 16:19:04 +0530 Subject: [PATCH 2/4] revert allowed hosts --- corpus/corpus/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/corpus/corpus/settings.py b/corpus/corpus/settings.py index e8dc7b20..38a37010 100644 --- a/corpus/corpus/settings.py +++ b/corpus/corpus/settings.py @@ -35,7 +35,7 @@ ) # TODO: Stricter host policies -ALLOWED_HOSTS = ["*"] +ALLOWED_HOSTS = ["localhost", "127.0.0.1", ".onrender.com"] CSRF_TRUSTED_ORIGINS = ["https://*.onrender.com"] if os.getenv("ENVIRONMENT", "PRODUCTION") == "PRODUCTION": ALLOWED_HOSTS.append("ieee.nitk.ac.in") From 07e66b268826bc9764e90df4f1dcfd711c9f1be2 Mon Sep 17 00:00:00 2001 From: Rajsimha M V Date: Wed, 24 Dec 2025 16:26:23 +0530 Subject: [PATCH 3/4] Add profile and edit profile pages for faculty --- corpus/accounts/forms.py | 12 +++ corpus/accounts/views.py | 35 +++++++- corpus/pages/views.py | 2 + corpus/requirements.txt | 1 + corpus/templates/accounts/edit_profile.html | 56 +++++++++++++ corpus/templates/accounts/profile.html | 91 ++++++++++++++++----- corpus/templates/pages/faculty_box.html | 80 +++++++++--------- 7 files changed, 215 insertions(+), 62 deletions(-) diff --git a/corpus/accounts/forms.py b/corpus/accounts/forms.py index 442a29a9..4c9a4f57 100644 --- a/corpus/accounts/forms.py +++ b/corpus/accounts/forms.py @@ -6,6 +6,7 @@ from .models import ExecutiveMember from .models import User +from .models import Faculty from corpus.forms import CorpusModelForm @@ -102,3 +103,14 @@ class Meta: "hide_github", "hide_linkedin", ] + +class FacultyForm(CorpusModelForm): + class Meta: + model = Faculty + fields = [ + "post", + "sig", + "society", + "linkedin", + "website", + ] diff --git a/corpus/accounts/views.py b/corpus/accounts/views.py index 0f1e28fd..1b4f59da 100644 --- a/corpus/accounts/views.py +++ b/corpus/accounts/views.py @@ -17,8 +17,10 @@ from .forms import CorpusCreationForm from .forms import CorpusLoginForm from .forms import ExecutiveMemberForm +from .forms import FacultyForm from .forms import UserForm from .models import ExecutiveMember +from .models import Faculty User = get_user_model() @@ -100,8 +102,13 @@ def profile(request, pk): try: exec_member = ExecutiveMember.objects.get(user=profile_user) + faculty = None except ExecutiveMember.DoesNotExist: exec_member = None + try : + faculty = Faculty.objects.get(user=profile_user) + except Faculty.DoesNotExist : + faculty = None # Get Virtual Expo Reports if exec_member: @@ -114,6 +121,7 @@ def profile(request, pk): args = { "exec_member": exec_member, + "faculty" : faculty, "profile_user": profile_user, "curr_user": request.user, "reports": reports, @@ -135,8 +143,13 @@ def edit_profile(request, pk): # Check if the user has an associated ExecutiveMember record try: executive_member = ExecutiveMember.objects.get(user=user) + faculty = None except ExecutiveMember.DoesNotExist: executive_member = None + try : + faculty = Faculty.objects.get(user=user) + except Faculty.DoesNotExist: + faculty = None if request.method == "POST": user_form = UserForm(request.POST, request.FILES, instance=user) @@ -148,17 +161,32 @@ def edit_profile(request, pk): user_form.save() executive_member_form.save() return redirect("accounts_profile", pk=pk) + else: - if user_form.is_valid(): - user_form.save() - return redirect("accounts_profile", pk=pk) + if faculty : + faculty_form = FacultyForm( + request.POST, instance=faculty + ) + if user_form.is_valid() and faculty_form.is_valid(): + user_form.save() + faculty_form.save() + return redirect("accounts_profile", pk=pk) + else : + if user_form.is_valid(): + user_form.save() + return redirect("accounts_profile", pk=pk) else: user_form = UserForm(instance=user) if executive_member: executive_member_form = ExecutiveMemberForm(instance=executive_member) + faculty_form = None else: executive_member_form = None + if faculty : + faculty_form = FacultyForm(instance=faculty) + else : + faculty_form = None return render( request, @@ -166,6 +194,7 @@ def edit_profile(request, pk): { "user_form": user_form, "executive_member_form": executive_member_form, + "faculty_form" : faculty_form, "exec_member": executive_member, "max_image_size": MAX_IMAGE_SIZE, }, diff --git a/corpus/pages/views.py b/corpus/pages/views.py index 260761df..a97d7c20 100644 --- a/corpus/pages/views.py +++ b/corpus/pages/views.py @@ -191,6 +191,8 @@ def team(request): "sac_core": sac_core, "faculty": faculty, } + + print(context) return render(request, "pages/team.html", context) diff --git a/corpus/requirements.txt b/corpus/requirements.txt index 64167717..2c30f688 100644 --- a/corpus/requirements.txt +++ b/corpus/requirements.txt @@ -25,6 +25,7 @@ typing_extensions==4.8.0 urllib3==2.2.1 virtualenv==20.25.0 wheel==0.41.2 +django-extensions celery dotenv redis diff --git a/corpus/templates/accounts/edit_profile.html b/corpus/templates/accounts/edit_profile.html index 1f7928b2..2500a476 100644 --- a/corpus/templates/accounts/edit_profile.html +++ b/corpus/templates/accounts/edit_profile.html @@ -162,6 +162,62 @@

    Socials

    + + {% else %} + {% if faculty_form %} + + +
    +
    +

    IEEE Details

    +
    +
    + + {{ faculty_form.post }} +
    +
    + + {{ faculty_form.sig }} +
    +
    + + {{ faculty_form.society }} +
    +
    +
    +
    + + +
    +
    +

    Socials

    +
    + +
    + + {{ faculty_form.linkedin }} +
    + + +
    + + {{ faculty_form.website }} +
    +
    +
    +
    + + {% endif %} {% endif %} diff --git a/corpus/templates/accounts/profile.html b/corpus/templates/accounts/profile.html index d8b1296b..826edb07 100644 --- a/corpus/templates/accounts/profile.html +++ b/corpus/templates/accounts/profile.html @@ -18,8 +18,13 @@

    {{ profile_user }}

    {% if exec_member %}

    {{ exec_member.sig }}

    + {% else %} + {% if exec_member %} +

    {{ faculty.post.name }}

    + {% endif %} {% endif %} + {% if curr_user == profile_user %}
    @@ -64,8 +69,33 @@

    {{ exec_member.sig }}

    + {% else %} + {% if faculty %} +
    +
    IEEE Faculty Information
    +
    +
    +
    Post
    +
    {{ faculty.post.name }}
    +
    +
    +
    SIG
    + {% if faculty.sig %} +
    {{ faculty.sig }}
    + {% endif %} +
    +
    +
    Society
    + {% if faculty.society %} +
    {{ faculty.society }}
    + {% endif %} +
    +
    +
    + {% endif %} {% endif %} + {% if exec_member %}
    Reports Published
    @@ -109,26 +139,24 @@

    {{ blog.title }}

    {% endif %}
    + {% endif %}
    Links
    + {% if exec_member %}
    GitHub
    - {% if exec_member %} - {% if exec_member.hide_github %} - -- + {% if exec_member.hide_github %} + -- + {% else %} + {% if exec_member.github %} + GitHub Profile {% else %} - {% if exec_member.github %} - GitHub Profile - {% else %} - None - {% endif %} + None {% endif %} - {% else %} - None {% endif %}
    @@ -137,21 +165,44 @@

    {{ blog.title }}

    LinkedIn
    - {% if exec_member %} - {% if exec_member.hide_linkedin %} - -- + {% if exec_member.hide_linkedin %} + -- + {% else %} + {% if exec_member.linkedin %} + LinkedIn Profile {% else %} - {% if exec_member.linkedin %} - LinkedIn Profile - {% else %} - None - {% endif %} + None {% endif %} - {% else %} - None {% endif %}
    + {% else %} + {% if faculty %} +
    + +
    Website
    +
    + {% if faculty.website %} + Website + {% else %} + None + {% endif %} +
    +
    + +
    + +
    LinkedIn
    +
    + {% if faculty.linkedin %} + LinkedIn Profile + {% else %} + None + {% endif %} +
    +
    + {% endif %} + {% endif %}
    diff --git a/corpus/templates/pages/faculty_box.html b/corpus/templates/pages/faculty_box.html index e2530831..b7fab934 100644 --- a/corpus/templates/pages/faculty_box.html +++ b/corpus/templates/pages/faculty_box.html @@ -1,45 +1,47 @@ {% load static %} -
    -
    -
    - photo -
    -
    -

    {{ faculty.user }}

    -

    {{ faculty.post.name }}

    -
    - {% if faculty.linkedin %} - - - + +
    +
    +
    + photo +
    +
    +

    {{ faculty.user }}

    +

    {{ faculty.post.name }}

    +
    + {% if faculty.linkedin %} + + + + + + {% endif %} + {% if faculty.website %} + + + + + - {% endif %} - {% if faculty.website %} - - - - - - - - {% endif %} + {% endif %} +
    -
    \ No newline at end of file + From 3a42b6c16aa5689521c8011c72c342d3cfc931b9 Mon Sep 17 00:00:00 2001 From: Rajsimha M V Date: Wed, 21 Jan 2026 19:30:36 +0530 Subject: [PATCH 4/4] Add chnages to make faculty able to change their post names v3 --- corpus/accounts/forms.py | 15 +++-- corpus/accounts/models.py | 2 +- corpus/accounts/views.py | 64 ++++++++++++++------- corpus/templates/accounts/edit_profile.html | 30 ++++------ 4 files changed, 65 insertions(+), 46 deletions(-) diff --git a/corpus/accounts/forms.py b/corpus/accounts/forms.py index 4c9a4f57..eb8193fe 100644 --- a/corpus/accounts/forms.py +++ b/corpus/accounts/forms.py @@ -5,8 +5,9 @@ from django.forms.widgets import ClearableFileInput from .models import ExecutiveMember -from .models import User from .models import Faculty +from .models import Post +from .models import User from corpus.forms import CorpusModelForm @@ -104,13 +105,19 @@ class Meta: "hide_linkedin", ] + class FacultyForm(CorpusModelForm): class Meta: model = Faculty fields = [ - "post", - "sig", - "society", "linkedin", "website", ] + + +class PostForm(CorpusModelForm): + class Meta: + model = Post + fields = [ + "name", + ] diff --git a/corpus/accounts/models.py b/corpus/accounts/models.py index d955e3cb..e49e345a 100644 --- a/corpus/accounts/models.py +++ b/corpus/accounts/models.py @@ -204,7 +204,7 @@ class Meta: society = models.ForeignKey( Society, blank=True, null=True, on_delete=models.CASCADE ) - post = models.ForeignKey(Post, null=True, blank=True, on_delete=models.CASCADE) + post = models.OneToOneField(Post, null=True, blank=True, on_delete=models.CASCADE) website = models.URLField(max_length=200, null=True, blank=True) linkedin = models.URLField( blank=True, null=True, verbose_name="Linkedin Profile URL" diff --git a/corpus/accounts/views.py b/corpus/accounts/views.py index 1b4f59da..2483482a 100644 --- a/corpus/accounts/views.py +++ b/corpus/accounts/views.py @@ -1,11 +1,10 @@ import re -from django.contrib.auth import get_user_model - from blog.models import Post from constants import MAX_IMAGE_SIZE from django.contrib import messages from django.contrib.auth import authenticate +from django.contrib.auth import get_user_model from django.contrib.auth import login from django.contrib.auth import logout from django.contrib.auth.decorators import login_required @@ -18,6 +17,7 @@ from .forms import CorpusLoginForm from .forms import ExecutiveMemberForm from .forms import FacultyForm +from .forms import PostForm from .forms import UserForm from .models import ExecutiveMember from .models import Faculty @@ -25,7 +25,6 @@ User = get_user_model() - # Create your views here. @@ -99,15 +98,15 @@ def signout(request): def profile(request, pk): profile_user = get_object_or_404(User, pk=pk) - + try: exec_member = ExecutiveMember.objects.get(user=profile_user) faculty = None except ExecutiveMember.DoesNotExist: exec_member = None - try : + try: faculty = Faculty.objects.get(user=profile_user) - except Faculty.DoesNotExist : + except Faculty.DoesNotExist: faculty = None # Get Virtual Expo Reports @@ -121,7 +120,7 @@ def profile(request, pk): args = { "exec_member": exec_member, - "faculty" : faculty, + "faculty": faculty, "profile_user": profile_user, "curr_user": request.user, "reports": reports, @@ -133,23 +132,26 @@ def profile(request, pk): @login_required def edit_profile(request, pk): - user = get_object_or_404(User, pk=pk) # Get the user explicitly by ID - + user = get_object_or_404(User, pk=pk) # Get the user explicitly by ID + # Authorization check: Ensure logged-in user can only edit their own profile if request.user != user: messages.warning(request, "You are not authorized to edit this profile.") return redirect("index") - + # Check if the user has an associated ExecutiveMember record try: executive_member = ExecutiveMember.objects.get(user=user) faculty = None + post = None except ExecutiveMember.DoesNotExist: executive_member = None - try : + try: faculty = Faculty.objects.get(user=user) + post = faculty.post except Faculty.DoesNotExist: faculty = None + post = None if request.method == "POST": user_form = UserForm(request.POST, request.FILES, instance=user) @@ -157,21 +159,35 @@ def edit_profile(request, pk): executive_member_form = ExecutiveMemberForm( request.POST, instance=executive_member ) + faculty_form = None + post_form = None if user_form.is_valid() and executive_member_form.is_valid(): user_form.save() executive_member_form.save() return redirect("accounts_profile", pk=pk) - + else: - if faculty : - faculty_form = FacultyForm( - request.POST, instance=faculty - ) - if user_form.is_valid() and faculty_form.is_valid(): + if faculty and post: + faculty_form = FacultyForm(request.POST, instance=faculty) + post_form = PostForm(request.POST, instance=post) + executive_member_form = None + if ( + user_form.is_valid() + and faculty_form.is_valid() + and post_form.is_valid() + ): user_form.save() faculty_form.save() + post_form.save() return redirect("accounts_profile", pk=pk) - else : + if not faculty_form.is_valid(): + return render( + request, + "accounts/edit_profile.html", + {"form": faculty_form, "errors": faculty_form.errors}, + ) + + else: if user_form.is_valid(): user_form.save() return redirect("accounts_profile", pk=pk) @@ -181,12 +197,15 @@ def edit_profile(request, pk): if executive_member: executive_member_form = ExecutiveMemberForm(instance=executive_member) faculty_form = None + post_form = None else: executive_member_form = None - if faculty : + if faculty: faculty_form = FacultyForm(instance=faculty) - else : - faculty_form = None + post_form = PostForm(instance=faculty.post) + else: + faculty_form = None + post_form = None return render( request, @@ -194,7 +213,8 @@ def edit_profile(request, pk): { "user_form": user_form, "executive_member_form": executive_member_form, - "faculty_form" : faculty_form, + "faculty_form": faculty_form, + "post_form": post_form, "exec_member": executive_member, "max_image_size": MAX_IMAGE_SIZE, }, diff --git a/corpus/templates/accounts/edit_profile.html b/corpus/templates/accounts/edit_profile.html index 2500a476..dd3d3209 100644 --- a/corpus/templates/accounts/edit_profile.html +++ b/corpus/templates/accounts/edit_profile.html @@ -5,6 +5,9 @@ {% endblock %} {% block content %} +{% if errors %} +
    {{ errors }}
    +{% endif %}

    Edit Profile

    @@ -166,32 +169,21 @@

    Socials

    {% else %} {% if faculty_form %} - + {% if post_form %} +
    -

    IEEE Details

    +

    Post Details

    - {{ faculty_form.post }} + {{ post_form.name }}
    -
    - - {{ faculty_form.sig }} -
    -
    - - {{ faculty_form.society }} -
    -
    + {% endif %}
    @@ -216,7 +208,7 @@

    Socials

    - + {% endif %} {% endif %} @@ -334,4 +326,4 @@

    Confirm Go Back

    -{% endblock %} \ No newline at end of file +{% endblock %}