diff --git a/CDC_Backend/APIs/adminViews.py b/CDC_Backend/APIs/adminViews.py index 4acf47c..de1166e 100644 --- a/CDC_Backend/APIs/adminViews.py +++ b/CDC_Backend/APIs/adminViews.py @@ -39,6 +39,7 @@ def markStatus(request, id, email, user_type): sendEmail(email, subject, data, STUDENT_APPLICATION_STATUS_SELECTED_TEMPLATE) else: sendEmail(email, subject, data, STUDENT_APPLICATION_STATUS_NOT_SELECTED_TEMPLATE) + application.chaged_by = get_object_or_404(User, id=id) application.save() else: raise ValueError("Student - " + i[STUDENT_ID] + " didn't apply for this opening") @@ -89,6 +90,7 @@ def updateDeadline(request, id, email, user_type): opening = get_object_or_404(Placement, pk=data[OPENING_ID]) # Updating deadline date with correct format in datetime field opening.deadline_datetime = datetime.datetime.strptime(data[DEADLINE_DATETIME], '%Y-%m-%d %H:%M:%S %z') + opening.changed_by = get_object_or_404(User, id=id) opening.save() return Response({'action': "Update Deadline", 'message': "Deadline Updated"}, status=status.HTTP_200_OK) @@ -111,6 +113,7 @@ def updateOfferAccepted(request, id, email, user_type): opening = get_object_or_404(Placement, pk=data[OPENING_ID]) opening.offer_accepted = True if data[OFFER_ACCEPTED] == True else False print(opening.offer_accepted) + opening.changed_by = get_object_or_404(User, id=id) opening.save() return Response({'action': "Update Offer Accepted", 'message': "Offer Accepted Updated"}, status=status.HTTP_200_OK) @@ -131,6 +134,7 @@ def updateEmailVerified(request, id, email, user_type): data = request.data opening = get_object_or_404(Placement, pk=data[OPENING_ID]) opening.email_verified = True if data[EMAIL_VERIFIED] == "true" else False + opening.changed_by = get_object_or_404(User, id=id) opening.save() return Response({'action': "Update Email Verified", 'message': "Email Verified Updated"}, status=status.HTTP_200_OK) @@ -156,6 +160,7 @@ def updateAdditionalInfo(request, id, email, user_type): opening.additional_info = data[ADDITIONAL_INFO] else: raise ValueError("Additional Info must be a list") + opening.changed_by = get_object_or_404(User, id=id) opening.save() return Response({'action': "Update Additional Info", 'message': "Additional Info Updated"}, status=status.HTTP_200_OK) @@ -216,6 +221,7 @@ def submitApplication(request, id, email, user_type): else: additional_info[i] = data[ADDITIONAL_INFO][i] application.additional_info = json.dumps(additional_info) + application.changed_by = get_object_or_404(User, id=id) application.save() return Response({'action': "Add Student Application", 'message': "Application added"}, status=status.HTTP_200_OK) @@ -235,6 +241,7 @@ def submitApplication(request, id, email, user_type): additional_info[i] = data[ADDITIONAL_INFO][i] application.additional_info = json.dumps(additional_info) + application.changed_by = get_object_or_404(User, id=id) application.save() return Response({'action': "Add Student Application", 'message': "Application updated"}, status=status.HTTP_200_OK) @@ -325,6 +332,7 @@ def addPPO(request, id, email, user_type): PPO.tier = data[TIER] if COMPENSATION_DETAILS in data: PPO.compensation_details = data[COMPENSATION_DETAILS] + PPO.changed_by = get_object_or_404(User, id=id) PPO.save() return Response({'action': "Add PPO", 'message': "PPO added"}, status=status.HTTP_200_OK) diff --git a/CDC_Backend/APIs/models.py b/CDC_Backend/APIs/models.py index 2a74d5f..c42982e 100644 --- a/CDC_Backend/APIs/models.py +++ b/CDC_Backend/APIs/models.py @@ -32,16 +32,41 @@ class Student(models.Model): default=list, blank=True) cpi = models.DecimalField(decimal_places=2, max_digits=4) can_apply = models.BooleanField(default=True, verbose_name='Registered') - history = HistoricalRecords() + changed_by = models.ForeignKey(User, blank=True, on_delete=models.RESTRICT, default=None, null=True) + history = HistoricalRecords(user_model=User) def __str__(self): return str(self.roll_no) + @property + def _history_user(self): + return self.changed_by + + @_history_user.setter + def _history_user(self, value): + if isinstance(value, User): + self.changed_by = value + else: + self.changed_by = None + + class Admin(models.Model): id = models.CharField(blank=False, max_length=15, primary_key=True) name = models.CharField(blank=False, max_length=JNF_TEXT_MAX_CHARACTER_COUNT) - history = HistoricalRecords() + changed_by = models.ForeignKey(User, blank=True, on_delete=models.RESTRICT, default=None, null=True) + history = HistoricalRecords(user_model=User) + + @property + def _history_user(self): + return self.changed_by + + @_history_user.setter + def _history_user(self, value): + if isinstance(value, User): + self.changed_by = value + else: + self.changed_by = None def two_day_after_today(): @@ -118,8 +143,8 @@ class Placement(models.Model): deadline_datetime = models.DateTimeField(blank=False, verbose_name="Deadline Date", default=two_day_after_today) created_at = models.DateTimeField(blank=False, default=None, null=True) updated_at = models.DateTimeField(blank=False, default=None, null=True) - history = HistoricalRecords() - + changed_by = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True) + history = HistoricalRecords() def format(self): if self.company_name is not None: self.company_name = self.company_name.strip()[:JNF_SMALLTEXT_MAX_CHARACTER_COUNT] @@ -163,6 +188,17 @@ class Placement(models.Model): if self.additional_info is not None: self.additional_info = [info.strip()[:JNF_TEXTMEDIUM_MAX_CHARACTER_COUNT] for info in list(self.additional_info)] + @property + def _history_user(self): + return self.changed_by + + @_history_user.setter + def _history_user(self, value): + if isinstance(value, User): + self.changed_by = value + else: + self.changed_by = None + def save(self, *args, **kwargs): ''' On save, add timestamps ''' if not self.created_at: @@ -184,7 +220,8 @@ class PlacementApplication(models.Model): selected = models.BooleanField(null=True, default=None, blank=True) applied_at = models.DateTimeField(blank=False, default=None, null=True) updated_at = models.DateTimeField(blank=False, default=None, null=True) - history = HistoricalRecords() + changed_by = models.ForeignKey(User, blank=False, on_delete=models.RESTRICT, default=None, null=True) + history = HistoricalRecords(user_model=User) def save(self, *args, **kwargs): ''' On save, add timestamps ''' @@ -194,6 +231,17 @@ class PlacementApplication(models.Model): return super(PlacementApplication, self).save(*args, **kwargs) + @property + def _history_user(self): + return self.changed_by + + @_history_user.setter + def _history_user(self, value): + if isinstance(value, User): + self.changed_by = value + else: + self.changed_by = None + class Meta: verbose_name_plural = "Placement Applications" unique_together = ('placement_id', 'student_id') @@ -212,4 +260,16 @@ class PrePlacementOffer(models.Model): tier = models.CharField(blank=False, choices=TIERS, max_length=10) designation = models.CharField(blank=False, max_length=25, default=None, null=True) accepted = models.BooleanField(default=None, null=True) - history = HistoricalRecords() + changed_by = models.ForeignKey(User, blank=False, on_delete=models.RESTRICT, default=None, null=True) + history = HistoricalRecords(user_model=User) + + @property + def _history_user(self): + return self.changed_by + + @_history_user.setter + def _history_user(self, value): + if isinstance(value, User): + self.changed_by = value + else: + self.changed_by = None diff --git a/CDC_Backend/APIs/studentViews.py b/CDC_Backend/APIs/studentViews.py index d3c3fe9..27c7c28 100644 --- a/CDC_Backend/APIs/studentViews.py +++ b/CDC_Backend/APIs/studentViews.py @@ -44,7 +44,7 @@ def addResume(request, id, email, user_type): destination_path = STORAGE_DESTINATION_RESUMES + str(student.roll_no) + "/" file_name = saveFile(file, destination_path) student.resumes.append(file_name) - + student.changed_by = get_object_or_404(User, id=id) student.save() return Response({'action': "Upload Resume", 'message': "Resume Added"}, status=status.HTTP_200_OK) @@ -104,6 +104,7 @@ def deleteResume(request, id, email, user_type): if path.exists(destination_path): # remove(destination_path) student.resumes.remove(file_name) + student.changed_by = get_object_or_404(User, id=id) student.save() return Response({'action': "Delete Resume", 'message': "Resume Deleted"}, status=status.HTTP_200_OK) @@ -177,7 +178,7 @@ def submitApplication(request, id, email, user_type): } subject = STUDENT_APPLICATION_SUBMITTED_TEMPLATE_SUBJECT.format(company_name=opening.company_name) sendEmail(email, subject, data, STUDENT_APPLICATION_SUBMITTED_TEMPLATE) - + application.changed_by = get_object_or_404(User, id=id) application.save() return Response({'action': "Submit Application", 'message': "Application Submitted"}, status=status.HTTP_200_OK)