Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profile page Modifications #192

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
32 changes: 32 additions & 0 deletions corpus/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.auth.forms import UserCreationForm

from .models import User
from .models import ExecutiveMember


class CorpusCreationForm(UserCreationForm):
Expand Down Expand Up @@ -61,3 +62,34 @@ def clean_username(self):
if username:
username = username.lower()
return username

class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['phone_no', 'gender', 'email', 'profile_pic']
widgets = {
'phone_no': forms.TextInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200'}),
'gender': forms.Select(attrs={'class': 'rounded-lg border-gray-300 bg-base-200'}),
'email': forms.EmailInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200'}),
'profile_pic': forms.ClearableFileInput(attrs={'multiple': False}),

}

class ExecutiveMemberForm(forms.ModelForm):
class Meta:
model = ExecutiveMember
fields = [
'edu_email', 'roll_number', 'reg_number', 'minor_branch',
'ieee_number', 'ieee_email', 'linkedin', 'github', 'is_nep'
]
widgets = {
'edu_email': forms.EmailInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200', 'required': True}),
'roll_number': forms.TextInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200', 'required': True}),
'reg_number': forms.TextInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200', 'required': True}),
'minor_branch': forms.Select(attrs={'class': 'rounded-lg border-gray-300 bg-base-200 w-full',}),
'ieee_number': forms.TextInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200','required': True}),
'ieee_email': forms.EmailInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200'}),
'linkedin': forms.URLInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200'}),
'github': forms.TextInput(attrs={'class': 'rounded-lg border-gray-300 bg-base-200'}),
'is_nep': forms.CheckboxInput(),
}
18 changes: 18 additions & 0 deletions corpus/accounts/migrations/0008_user_profile_pic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.3 on 2024-12-25 12:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0007_alter_executivemember_date_joined'),
]

operations = [
migrations.AddField(
model_name='user',
name='profile_pic',
field=models.ImageField(blank=True, default=None, null=True, upload_to='accounts/profile/pics'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.3 on 2025-01-07 13:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0008_user_profile_pic'),
]

operations = [
migrations.AddField(
model_name='executivemember',
name='hide_github',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='executivemember',
name='hide_linkedin',
field=models.BooleanField(default=False),
),
]
6 changes: 6 additions & 0 deletions corpus/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .validators import validate_phone_number
from .validators import validate_reg_number
from .validators import validate_roll_number
from .validators import validate_image


class UserManager(BaseUserManager):
Expand Down Expand Up @@ -56,6 +57,9 @@ class User(AbstractUser):
)
gender = models.CharField(max_length=1, choices=GENDERS)
email = models.EmailField(unique=True, verbose_name="Personal Email")
profile_pic = models.ImageField(upload_to="accounts/profile/pics",
validators=[validate_image],
blank=True, null=True, default=None)

USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
Expand Down Expand Up @@ -138,6 +142,8 @@ class ExecutiveMember(models.Model):
github = models.CharField(
max_length=39, blank=True, null=True, verbose_name="GitHub Username"
)
hide_github = models.BooleanField(default=False)
hide_linkedin = models.BooleanField(default=False)
is_nep = models.BooleanField(default=False, verbose_name="Is NEP Member?")
date_joined = models.DateTimeField(
default=timezone.localtime, verbose_name="Date Joined"
Expand Down
4 changes: 4 additions & 0 deletions corpus/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from .views import signin
from .views import signout
from .views import signup
from .views import profile
from .views import edit_profile

urlpatterns = [
path("signup/", signup, name="accounts_signup"),
Expand All @@ -30,4 +32,6 @@
PasswordResetCompleteView.as_view(),
name="password_reset_complete",
),
path("profile/<roll_no>", profile, name="accounts_profile"),
path("profile/edit/<roll_no>", edit_profile, name="edit_profile")
]
7 changes: 7 additions & 0 deletions corpus/accounts/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ def validate_ieee_email(value):
if not value.endswith("@ieee.org"):
raise ValidationError("Email must end with @ieee.org")
email_validator(value)

def validate_image(image):
file_size = image.file.size
limit = 5 * 1024 * 1024 # 5 MB limit
if file_size > limit:
raise ValidationError("Image size exceeds 5MB.")

79 changes: 79 additions & 0 deletions corpus/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from django.shortcuts import render
from django.shortcuts import get_object_or_404

from .forms import CorpusCreationForm
from .forms import CorpusLoginForm
from .forms import UserForm
from .forms import ExecutiveMemberForm
from .models import ExecutiveMember
# from .models import User
from virtual_expo.models import Report, ReportMember
from blog.models import Post


# Create your views here.

Expand Down Expand Up @@ -80,3 +88,74 @@ def signout(request):
logout(request)
messages.success(request, "Successfully signed out.")
return redirect("index")

def profile(request, roll_no):
exec_member = get_object_or_404(ExecutiveMember, roll_number=roll_no)
user = exec_member.user

# 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)

args = {
"exec_member": exec_member,
"user": user,
"reports": reports,
"blogs": blogs,
}

return render(request, "accounts/profile.html", args)

@login_required
def edit_profile(request, roll_no):
user = request.user # Get the currently logged-in user

# Check if the user has an associated ExecutiveMember record
try:
executive_member = ExecutiveMember.objects.get(roll_number=roll_no, user=user)
except ExecutiveMember.DoesNotExist:
executive_member = None # Handle the case where the user is not an ExecutiveMember

if request.method == 'POST':
user_form = UserForm(request.POST, request.FILES, instance=user)
if executive_member:
executive_member_form = ExecutiveMemberForm(request.POST, instance=executive_member)
else:
executive_member_form = ExecutiveMemberForm(request.POST)

if user_form.is_valid() and executive_member_form.is_valid():
user_form.save()
# Handle hiding GitHub/LinkedIn if checkboxes are selected
hide_linkedin = request.POST.get('hide_linkedin', False)
hide_github = request.POST.get('hide_github', False)

if hide_linkedin:
executive_member.hide_linkedin = True
else:
executive_member.hide_linkedin = False

if hide_github:
executive_member.hide_github = True
else:
executive_member.hide_github = False
executive_member_form.save()
return redirect('accounts_profile', roll_no=roll_no)

else:
user_form = UserForm(instance=user)
if executive_member:
executive_member_form = ExecutiveMemberForm(instance=executive_member)
else:
executive_member_form = ExecutiveMemberForm()

return render(
request,
'accounts/edit_profile.html',
{
'user_form': user_form,
'executive_member_form': executive_member_form,
'exec_member': executive_member,
}
)
Loading
Loading