SHARE
TWEET

Untitled

a guest Jun 25th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.db import models
  2. from rems.models.Task import Task
  3.  
  4. class Project(models.Model):
  5. id = models.IntegerField('id', primary_key=True)
  6. name = models.CharField('name', max_length=45, unique=False)
  7. code = models.CharField('code', unique=True, max_length=8)
  8. description = models.CharField('description', max_length=255)
  9. created_at = models.DateTimeField(auto_now_add=True)
  10. updated_at = models.DateTimeField(auto_now=True)
  11.  
  12. class Meta:
  13.     db_table = 'project'
  14.     verbose_name = 'project'
  15.     verbose_name_plural = 'projects'
  16.     ordering = ('-code',)
  17.  
  18. def natural_key(self):
  19.     return self.id
  20.  
  21. def project_get_tree(self):
  22.     children_tree = self.get_children_tree()
  23.  
  24.     returned_data = {
  25.         "id": self.id,
  26.         "name": self.name,
  27.         "code": self.code,
  28.         "description": self.description,
  29.         "tasks": children_tree
  30.     }
  31.  
  32.     return returned_data
  33.  
  34. def get_children_tree(self):
  35.     task_collection = Task.objects.filter(project_id=self.id)
  36.     returned_array = []
  37.     if len(task_collection) > 0:
  38.         for child in task_collection:
  39.             returned_array.append(child.get_tree())
  40.     return returned_array
  41.  
  42. @staticmethod
  43. def get_object(id):
  44.     try:
  45.         return Project.objects.get(id=id)
  46.     except Project.DoesNotExist:
  47.         return False
  48.  
  49. def can_be_deleted(self):
  50.     children_count = Task.objects.filter(project_id=self.id).count()
  51.     if children_count > 0:
  52.         return False
  53.     return True
  54.      
  55. from rest_framework import serializers
  56. from rems.models.Project import Project
  57. from rest_framework.validators import UniqueValidator
  58.  
  59. class ProjectSerializer(serializers.HyperlinkedModelSerializer):
  60.     id = serializers.IntegerField(read_only=True)
  61.     name = serializers.CharField(required=True, min_length=1, max_length=45)
  62.     code = serializers.CharField(required=True, min_length=1, max_length=8,
  63.                              validators=[UniqueValidator(queryset=Project.objects.all())])
  64.  description = serializers.CharField(required=False, min_length=1, max_length=255)
  65.  
  66. class Meta:
  67.     model = Project
  68.     fields = ('id', 'code', 'name', 'description')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top