Skip to content

Commit

Permalink
Merge pull request #99 from parthahere001/master
Browse files Browse the repository at this point in the history
[FIX] Alumni-By-Year route now works as intended
  • Loading branch information
mayankt18 authored Jun 22, 2023
2 parents e2e970d + ed34346 commit 95b3975
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 42 deletions.
84 changes: 47 additions & 37 deletions main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils import timezone
import datetime
from django.forms.models import model_to_dict

from rest_framework import serializers

def validate_pdf_size(value):
limit = 100 * 1024 * 1024
Expand Down Expand Up @@ -80,10 +80,9 @@ def year_choices():
return [(y, y) for y in range(cuur_year, cuur_year + 4 + 1)]


class Profile(models.Model):
class Alumni(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
user = models.OneToOneField(User, on_delete=models.CASCADE)

# Choices of degree
DEGREE = (
Expand All @@ -92,22 +91,14 @@ class Profile(models.Model):
('MTECH', 'M.Tech'),
)

YEAR = (
('1', 'First'),
('2', 'Second'),
('3', 'Third'),
('4', 'Final'),
)

alias = models.CharField(max_length=64, blank=True, null=True)
bio = models.TextField(max_length=512, blank=True, null=True)
image = models.ImageField(upload_to='member_images/', blank=True, null=True, validators=[validate_image_size])
image = models.ImageField(upload_to='alumni_images/', blank=True, null=True, validators=[validate_image_size])
email = models.EmailField(blank=True, null=True)
phone_number = models.CharField(max_length=14, blank=True, null=True)
degree_name = models.CharField(max_length=64, choices=DEGREE)
passout_year = models.IntegerField(choices=year_choices(), default=2018)
passout_year = models.IntegerField(default=2018)
position = models.CharField(max_length=255, blank=True, null=True)
convert_to_alumni = models.BooleanField(default=False)

git_link = models.URLField(null=True, blank=True)
facebook_link = models.URLField(null=True, blank=True)
Expand All @@ -116,29 +107,17 @@ class Profile(models.Model):
linkedin_link = models.URLField(null=True, blank=True)

def __str__(self):
return self.first_name

def save(self, *args, **kwargs):
"""
Checks if the profile belongs to an alumni or not and converts to alumni if True
"""
if self.convert_to_alumni == True:
initial_data = model_to_dict(self)
if self.user is not None: self.user.is_active = False
self.user.save()
initial_data.pop('convert_to_alumni', None)
initial_data.pop('user', None)
alumni = Alumni(**initial_data)
alumni.save()
self.delete()
return
else:
super(Profile, self).save(*args, **kwargs)
return (self.first_name + " " + self.last_name)

class Meta:
verbose_name_plural = "Alumni"

class Alumni(models.Model):


class Profile(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
user = models.OneToOneField(User, on_delete=models.CASCADE)

# Choices of degree
DEGREE = (
Expand All @@ -147,14 +126,22 @@ class Alumni(models.Model):
('MTECH', 'M.Tech'),
)

YEAR = (
('1', 'First'),
('2', 'Second'),
('3', 'Third'),
('4', 'Final'),
)

alias = models.CharField(max_length=64, blank=True, null=True)
bio = models.TextField(max_length=512, blank=True, null=True)
image = models.ImageField(upload_to='alumni_images/', blank=True, null=True, validators=[validate_image_size])
image = models.ImageField(upload_to='member_images/', blank=True, null=True, validators=[validate_image_size])
email = models.EmailField(blank=True, null=True)
phone_number = models.CharField(max_length=14, blank=True, null=True)
degree_name = models.CharField(max_length=64, choices=DEGREE)
passout_year = models.IntegerField(default=2018)
passout_year = models.IntegerField(choices=year_choices(), default=2018)
position = models.CharField(max_length=255, blank=True, null=True)
convert_to_alumni = models.BooleanField(default=False)

git_link = models.URLField(null=True, blank=True)
facebook_link = models.URLField(null=True, blank=True)
Expand All @@ -163,10 +150,33 @@ class Alumni(models.Model):
linkedin_link = models.URLField(null=True, blank=True)

def __str__(self):
return (self.first_name + " " + self.last_name)
return self.first_name

def save(self, *args, **kwargs):
"""
Checks if the profile belongs to an alumni or not and converts to alumni if True
"""
if self.convert_to_alumni == True:
class AlumniSerializer(serializers.ModelSerializer):
class Meta:
model = Alumni
fields = '__all__'

# Converting the Profile instance to Alumni instance
alumni_data = AlumniSerializer(self).data

# Creating a new Alumni instance from the serialized data
alumni_serializer = AlumniSerializer(data=alumni_data)
alumni_serializer.is_valid(raise_exception=True)
alumni_serializer.save()

# Deleting the Profile instance after converting it to Alumni
self.delete()
return
else:
super(Profile, self).save(*args, **kwargs)


class Meta:
verbose_name_plural = "Alumni"


class CarouselImage(models.Model):
Expand Down
3 changes: 2 additions & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
router.register(r'linit', views.LinitViewSet)
router.register(r'timeline', views.TimelineViewSet)
router.register(r'alumni', views.AlumniViewSet)
router.register(r'alumni-by-year', views.AlumniByYearViewSet)
# In the latest DRF, We need to explicitly set base_name in our viewset url if we don't have queryset defined.
router.register(r'alumni-by-year', views.AlumniByYearViewSet, basename="alumnibyyear")
router.register(r'techbytes', views.TechBytesViewSet)
router.register(r'devposts', views.DevPostViewSet)
router.register(r'configs', views.ConfigViewSet)
Expand Down
13 changes: 9 additions & 4 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,18 @@ class AlumniViewSet(viewsets.ModelViewSet):

# ViewSets define the view behavior.
class AlumniByYearViewSet(viewsets.ModelViewSet):
queryset = Alumni.objects.all().order_by('-passout_year', 'first_name')
serializer_class = serializers.AlumniSerializer
http_method_names = ['get']
def list(self,request):
# We use the get_queryset() method instead of assigning a fixed queryset attribute
# such that the queryset is updated dynamically each time the view is accessed.
def get_queryset(self):
queryset = Alumni.objects.all().order_by('-passout_year', 'first_name')
return queryset

def list(self, request, *args, **kwargs):
data = defaultdict(list)
alumunus = self.queryset
for alumni in alumunus :
queryset = self.get_queryset()
for alumni in queryset:
data[alumni.passout_year].append(self.serializer_class(alumni).data)
return Response(data)

Expand Down

0 comments on commit 95b3975

Please sign in to comment.