From 1655dbccb780d25d9ebe0605fd9e8ddaa75959df Mon Sep 17 00:00:00 2001 From: TBS093A Date: Sat, 29 Aug 2020 16:28:36 +0200 Subject: [PATCH] refactor old methods -> models.py (generalApp) --- generalApp/AbstractCRUD.py | 9 +++++ generalApp/models.py | 68 +++++++++----------------------------- 2 files changed, 25 insertions(+), 52 deletions(-) diff --git a/generalApp/AbstractCRUD.py b/generalApp/AbstractCRUD.py index 53d8768..009fa05 100644 --- a/generalApp/AbstractCRUD.py +++ b/generalApp/AbstractCRUD.py @@ -1,5 +1,13 @@ from django.db import models +class ValidationUtils(): + + def fromDict(self, dict): + self.__dict__.update(dict) + + class Meta: + abstract = True + class AbstractUtilsCRUD(): """ This class have a primary utilities for CRUD functionality @@ -179,6 +187,7 @@ class AbstractDelete(AbstractUtilsCRUD): class AbstractCRUD( models.Model, + ValidationUtils, AbstractGet, AbstractCreate, AbstractUpdate, diff --git a/generalApp/models.py b/generalApp/models.py index ba473c7..22d2710 100755 --- a/generalApp/models.py +++ b/generalApp/models.py @@ -207,12 +207,11 @@ class Threads(AbstractCRUD): name = models.CharField(max_length=30) user = models.ForeignKey(Users, on_delete = models.CASCADE) + parent_id_field = 'user_id' + def __str__(self): return self.name - def fromDict(self, dict): - self.__dict__.update(dict) - def toDict(self): return {"id": self.id, "name": self.name, @@ -278,20 +277,14 @@ class Subjects(AbstractCRUD): user = models.ForeignKey(Users, on_delete = models.CASCADE) thread = models.ForeignKey(Threads, on_delete = models.CASCADE) + parent_id_field = 'thread_id' + def __str__(self): return f"{self.id} {self.name}" def setParentID(self, parentID): self.__dict__.update({ "thread_id": parentID }) - @classmethod - def getAllByParentID(self, parentID): - list = [ x.toDict() for x in self.objects.filter(thread_id = parentID)] - return json.dumps(list) - - def fromDict(self, dict): - self.__dict__.update(dict) - def toDict(self): return {"id": self.id, "name": self.name, @@ -343,23 +336,17 @@ class Comments(AbstractCRUD): user = models.ForeignKey(Users, on_delete = models.CASCADE) subject = models.ForeignKey(Subjects, on_delete = models.CASCADE) + parent_id_field = 'subject_id' + def __str__(self): return f"{self.user} -> {self.subject}" def setParentID(self, parentID): self.__dict__.update({ "subject_id": parentID }) - @classmethod - def getAllByParentID(self, parentID): - list = [ x.toDict() for x in self.objects.filter(subject_id = parentID)] - return json.dumps(list) - def commentSVG(self): return - def fromDict(self, dict): - self.__dict__.update(dict) - def toDict(self): return {"id": self.id, "text": self.text, @@ -403,20 +390,14 @@ class Ratings(AbstractCRUD): user = models.ForeignKey(Users, on_delete = models.CASCADE) comment = models.ForeignKey(Comments, on_delete = models.CASCADE) + parent_id_field = 'comment_id' + def __str__(self): return f"{self.user}, value: {self.value} -> comment in: {self.comment.subject}" def setParentID(self, parentID): self.__dict__.update({ "comment_id": parentID }) - @classmethod - def getAllByParentID(self, parentID): - list = [ x.toDict() for x in self.objects.filter(comment_id = parentID)] - return json.dumps(list) - - def fromDict(self, dict): - self.__dict__.update(dict) - def toDict(self): return {"id": self.id, "value": self.value, @@ -444,20 +425,14 @@ class Transactions(AbstractCRUD): course_on_payment = models.FloatField(default=255) user = models.ForeignKey(Users, on_delete = models.CASCADE) + parent_id_field = 'user_id' + def __str__(self): return f"{self.user.login}, cash: {self.price}, prognosis: {self.price_forecast}" def setParentID(self, parentID): self.__dict__.update({ "user_id": parentID }) - @classmethod - def getAllByParentID(self, parentID): - list = [ x.toDict() for x in self.objects.filter(user_id = parentID)] - return json.dumps(list) - - def fromDict(self, dict): - self.__dict__.update(dict) - def toDict(self): return {"id": self.id, "price": self.price, @@ -475,20 +450,14 @@ class Triggers(AbstractCRUD): status = models.IntegerField(default=1) user = models.ForeignKey(Users, on_delete = models.CASCADE) + parent_id_field = 'user_id' + def __str__(self): return f"{self.user.login}, trigger value: {self.course_values_for_trigger}, date: {self.date_of_trigger}" def setParentID(self, parentID): self.__dict__.update({ "user_id": parentID }) - @classmethod - def getAllByParentID(self, parentID): - list = [ x.toDict() for x in self.objects.filter(user_id = parentID)] - return json.dumps(list) - - def fromDict(self, dict): - self.__dict__.update(dict) - def toDict(self): return {"id": self.id, "course_values_for_trigger": self.course_values_for_trigger, @@ -497,7 +466,7 @@ class Triggers(AbstractCRUD): "user_id": self.user.id, "author": self.user.login,} - def setActualTime(self): + def _setActualTimeTrigger(self) self.date_of_trigger = str(datetime.now().strftime("%Y-%d-%m %H:%M")) @@ -505,19 +474,14 @@ class Notifications(ObjectAbstract): message = models.CharField(max_length=255) user = models.ForeignKey(Users, on_delete = models.CASCADE) + parent_id_field = 'user_id' + def __str__(self): return f"Message: {self.message}, for User: {self.user.login}" - def setParentID(self, parentID): + def _setParentID(self, parentID): self.__dict__.update({ "user_id": parentID }) - @classmethod - def getAllByParentID(self, parentID): - return json.dumps(list(self.objects.filter(user_id = parentID).values())) - - def fromDict(self, dict): - self.__dict__.update(dict) - def toDict(self): return {"id": self.id, "message": self.message,