diff --git a/main/models.py b/main/models.py index 0eb5fad..c2fdb7c 100644 --- a/main/models.py +++ b/main/models.py @@ -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 @@ -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 = ( @@ -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) @@ -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 = ( @@ -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) @@ -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): diff --git a/main/urls.py b/main/urls.py index abd19bb..b81e71e 100644 --- a/main/urls.py +++ b/main/urls.py @@ -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) diff --git a/main/views.py b/main/views.py index d88991b..ff29576 100644 --- a/main/views.py +++ b/main/views.py @@ -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)