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):
|
def addObject(request, privilige):
|
||||||
object = jsonLoad(request)
|
object = jsonLoad(request)
|
||||||
if checkSession(request, privilige):
|
if checkSession(request, privilige):
|
||||||
if self.__checkUniqueValues(object):
|
if self.__validateUnique(object):
|
||||||
return self.__saveObject(object)
|
return self.__saveObject(object)
|
||||||
else:
|
else:
|
||||||
return HttpResponse("Object Is Already Exist")
|
return HttpResponse("Object Is Already Exist")
|
||||||
else:
|
else:
|
||||||
return HttpResponse("No Permission")
|
return HttpResponse("No Permission")
|
||||||
|
|
||||||
def __checkUniqueValues(objectDict):
|
def __validateUnique(objectDict):
|
||||||
objectsAll = Threads.__allObjectsDict(model)
|
objectsAll = Threads.__allObjectsDict(model)
|
||||||
for x in objectsAll:
|
for x in objectsAll:
|
||||||
if x['name'].upper() == objectDict['name'].upper():
|
if x['name'].upper() == objectDict['name'].upper():
|
||||||
|
|
@ -302,6 +302,9 @@ class Subjects(ObjectAbstract):
|
||||||
"thread_id": self.thread.id,
|
"thread_id": self.thread.id,
|
||||||
"thread_name": self.thread.name}
|
"thread_name": self.thread.name}
|
||||||
|
|
||||||
|
def objectFactory():
|
||||||
|
return Subjects()
|
||||||
|
|
||||||
# Get One Subject
|
# Get One Subject
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -316,17 +319,20 @@ class Subjects(ObjectAbstract):
|
||||||
else:
|
else:
|
||||||
return HttpResponse("No Permission")
|
return HttpResponse("No Permission")
|
||||||
|
|
||||||
def __saveObject(threadID, objectDict):
|
def __saveObject(self, threadID, objectDict):
|
||||||
newObject = Subjects()
|
newObject = self.getObject()
|
||||||
newObject.fromDict(objectDict)
|
newObject.fromDict(objectDict)
|
||||||
newObject.setParentID(threadID)
|
newObject.setParentID(threadID)
|
||||||
|
|
||||||
newComment = Comments(subject = newObject)
|
self.__createFirstComment(newObject, objectDict)
|
||||||
newComment.fromDict(objectDict['comment'])
|
|
||||||
newComment.save()
|
|
||||||
|
|
||||||
return HttpResponse(f"Add new Subject: {newObject.toDict()} -> {newComment.toDict()}")
|
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
|
# Update Subject
|
||||||
|
|
||||||
# Delete Subject
|
# Delete Subject
|
||||||
|
|
@ -365,7 +371,11 @@ class Comments(ObjectAbstract):
|
||||||
"subject_id": self.subject.id,
|
"subject_id": self.subject.id,
|
||||||
"subject_name": self.subject.name}
|
"subject_name": self.subject.name}
|
||||||
|
|
||||||
|
def objectFactory():
|
||||||
|
return Comments()
|
||||||
|
|
||||||
# Get One Comment
|
# Get One Comment
|
||||||
|
|
||||||
# Create Comment
|
# Create Comment
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
@ -377,7 +387,7 @@ class Comments(ObjectAbstract):
|
||||||
return HttpResponse("No Permission")
|
return HttpResponse("No Permission")
|
||||||
|
|
||||||
def __saveObject(subjectID, objectDict):
|
def __saveObject(subjectID, objectDict):
|
||||||
newObject = Comments()
|
newObject = self.getObject()
|
||||||
newObject.fromDict(objectDict)
|
newObject.fromDict(objectDict)
|
||||||
newObject.setParentID(subject)
|
newObject.setParentID(subject)
|
||||||
newObject.save()
|
newObject.save()
|
||||||
|
|
@ -416,6 +426,15 @@ class Ratings(ObjectAbstract):
|
||||||
"comment_id": self.comment.id,
|
"comment_id": self.comment.id,
|
||||||
"subject": self.comment.subject.name}
|
"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):
|
class Transactions(ObjectAbstract):
|
||||||
price = models.FloatField(default=255)
|
price = models.FloatField(default=255)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue