create a AbstractCRUD classes && make AbstractUtilsCRUD doc && make AbstractGet & AbstractCreate && fix old classes in models.py
parent
572e9cdf31
commit
e154e0e2ed
|
|
@ -0,0 +1,120 @@
|
|||
from django.db import models
|
||||
|
||||
class AbstractUtilsCRUD():
|
||||
"""
|
||||
This class have a primary utils for CRUD functionality
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def objectFactory(self):
|
||||
"""
|
||||
return a new specific object
|
||||
"""
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def setParentID(self, parentID):
|
||||
"""
|
||||
set object parent id
|
||||
"""
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def allObjectsDict(self):
|
||||
"""
|
||||
map all class objects to dict
|
||||
"""
|
||||
objectAll = self.objectFactory().objects.all()
|
||||
list = []
|
||||
for x in objectAll:
|
||||
list.append(x.toDict())
|
||||
return list
|
||||
|
||||
|
||||
class AbstractGet(AbstractUtilsCRUD):
|
||||
|
||||
@classmethod
|
||||
def getObject(self, request, objectID, privilige): # request, privilige is unnecessary
|
||||
return self.__getObjectNormal(self, objectID)
|
||||
|
||||
def __getObjectNormal(self, objectID):
|
||||
object = self.objectFactory().objects.get(pk = objectID).toDict()
|
||||
return HttpResponse(json.dumps(object))
|
||||
|
||||
@classmethod
|
||||
def getAllObjects(self, request, privilige):
|
||||
objectsAll = self.allObjectsDict()
|
||||
return HttpResponse(json.dumps(objectsAll))
|
||||
|
||||
@classmethod
|
||||
def getObjectsByParentID(self, request, parentID, privilige):
|
||||
if self.modelHaveParent(self):
|
||||
return HttpResponse(self.getAllByParentID(parentID))
|
||||
return HttpResponse("No Permission")
|
||||
|
||||
def __getAllByParentID(self, parentID):
|
||||
list = [ x.toDict() for x in self.objectFactory().__get.objects.filter(subject_id = parentID)]
|
||||
return json.dumps(list)
|
||||
|
||||
|
||||
class AbstractCreate(AbstractUtilsCRUD):
|
||||
|
||||
@classmethod
|
||||
def addObject(request, privilige):
|
||||
object = jsonLoad(request)
|
||||
if checkSession(request, privilige):
|
||||
if self.__validateUnique(object):
|
||||
return self.__saveObject(object)
|
||||
else:
|
||||
return HttpResponse("Object Is Already Exist")
|
||||
else:
|
||||
return HttpResponse("No Permission")
|
||||
|
||||
@classmethod
|
||||
def __validateUnique(self, userDict):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def __saveObject(self, objectDict):
|
||||
newObject = self.__getObject()
|
||||
newObject.fromDict(objectDict)
|
||||
newObject.save()
|
||||
return HttpResponse(f"Add new Object: {newObject.toDict()}")
|
||||
|
||||
@classmethod
|
||||
def __saveObject(self, parentID, objectDict):
|
||||
newObject = self.__getObject()
|
||||
newObject.fromDict(objectDict)
|
||||
|
||||
self.__setParentID(parentID)
|
||||
self.__createFirstComment(newObject, objectDict)
|
||||
self.__setActualTimeTrigger()
|
||||
|
||||
newObject.save()
|
||||
return HttpResponse(f"Add new Subject: {newObject.toDict()} -> {newComment.toDict()}")
|
||||
|
||||
@classmethod
|
||||
def __createFirstComment(newSubject, objectDict):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def __setActualTimeTrigger():
|
||||
pass
|
||||
|
||||
|
||||
class AbstractUpdate(AbstractUtilsCRUD):
|
||||
pass
|
||||
|
||||
|
||||
class AbstractDelete(AbstractUtilsCRUD):
|
||||
pass
|
||||
|
||||
|
||||
class AbstractCRUD(
|
||||
models.Model,
|
||||
AbstractGet,
|
||||
AbstractCreate,
|
||||
AbstractUpdate,
|
||||
AbstractDelete,
|
||||
):
|
||||
pass
|
||||
|
|
@ -233,14 +233,14 @@ class Threads(ObjectAbstract):
|
|||
def addObject(request, privilige):
|
||||
object = jsonLoad(request)
|
||||
if checkSession(request, privilige):
|
||||
if self.__checkUniqueValues(object):
|
||||
if self.__validateUnique(object):
|
||||
return self.__saveObject(object)
|
||||
else:
|
||||
return HttpResponse("Object Is Already Exist")
|
||||
else:
|
||||
return HttpResponse("No Permission")
|
||||
|
||||
def __checkUniqueValues(objectDict):
|
||||
def __validateUnique(objectDict):
|
||||
objectsAll = Threads.__allObjectsDict(model)
|
||||
for x in objectsAll:
|
||||
if x['name'].upper() == objectDict['name'].upper():
|
||||
|
|
@ -301,6 +301,9 @@ class Subjects(ObjectAbstract):
|
|||
"author_privilige": self.user.privilige,
|
||||
"thread_id": self.thread.id,
|
||||
"thread_name": self.thread.name}
|
||||
|
||||
def objectFactory():
|
||||
return Subjects()
|
||||
|
||||
# Get One Subject
|
||||
|
||||
|
|
@ -316,17 +319,20 @@ class Subjects(ObjectAbstract):
|
|||
else:
|
||||
return HttpResponse("No Permission")
|
||||
|
||||
def __saveObject(threadID, objectDict):
|
||||
newObject = Subjects()
|
||||
def __saveObject(self, threadID, objectDict):
|
||||
newObject = self.getObject()
|
||||
newObject.fromDict(objectDict)
|
||||
newObject.setParentID(threadID)
|
||||
|
||||
newComment = Comments(subject = newObject)
|
||||
newComment.fromDict(objectDict['comment'])
|
||||
newComment.save()
|
||||
self.__createFirstComment(newObject, objectDict)
|
||||
|
||||
return HttpResponse(f"Add new Subject: {newObject.toDict()} -> {newComment.toDict()}")
|
||||
|
||||
def __createFirstComment(newSubject, objectDict):
|
||||
newComment = Comments(subject = newSubject)
|
||||
newComment.fromDict(objectDict['comment'])
|
||||
newComment.save()
|
||||
|
||||
# Update Subject
|
||||
|
||||
# Delete Subject
|
||||
|
|
@ -364,8 +370,12 @@ class Comments(ObjectAbstract):
|
|||
"author_privilige": self.user.privilige,
|
||||
"subject_id": self.subject.id,
|
||||
"subject_name": self.subject.name}
|
||||
|
||||
def objectFactory():
|
||||
return Comments()
|
||||
|
||||
# Get One Comment
|
||||
|
||||
# Create Comment
|
||||
|
||||
@classmethod
|
||||
|
|
@ -377,7 +387,7 @@ class Comments(ObjectAbstract):
|
|||
return HttpResponse("No Permission")
|
||||
|
||||
def __saveObject(subjectID, objectDict):
|
||||
newObject = Comments()
|
||||
newObject = self.getObject()
|
||||
newObject.fromDict(objectDict)
|
||||
newObject.setParentID(subject)
|
||||
newObject.save()
|
||||
|
|
@ -416,6 +426,15 @@ class Ratings(ObjectAbstract):
|
|||
"comment_id": self.comment.id,
|
||||
"subject": self.comment.subject.name}
|
||||
|
||||
@classmethod
|
||||
def __validateUnique(model, parentID, objectDict):
|
||||
objectsAll = model.__allObjectsDict(model)
|
||||
for x in objectsAll:
|
||||
if model == Ratings:
|
||||
if int(x['user_id']) == int(objectDict['user_id']) and int(x['comment_id']) == parentID:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class Transactions(ObjectAbstract):
|
||||
price = models.FloatField(default=255)
|
||||
|
|
|
|||
Loading…
Reference in New Issue