Browse Source

Update ProjectViewSet

pull/10/head
Hironsan 6 years ago
parent
commit
006181e72a
4 changed files with 15 additions and 10 deletions
  1. BIN
      app/db.sqlite3
  2. 11
      app/server/api.py
  3. 8
      app/server/models.py
  4. 6
      app/server/tests/test_models.py

BIN
app/db.sqlite3

11
app/server/api.py

@ -21,19 +21,12 @@ class ProjectViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated, IsAdminUserAndWriteOnly)
def get_queryset(self):
user = self.request.user
queryset = self.queryset.filter(users__id__contains=user.id)
return queryset
return self.request.user.projects
@action(methods=['get'], detail=True)
def progress(self, request, pk=None):
project = self.get_object()
docs = project.get_documents(is_null=True)
total = project.documents.count()
remaining = docs.count()
return Response({'total': total, 'remaining': remaining})
return Response(project.get_progress())
class ProjectLabelsAPI(generics.ListCreateAPIView):

8
app/server/models.py

@ -22,7 +22,7 @@ class Project(models.Model):
guideline = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
users = models.ManyToManyField(User)
users = models.ManyToManyField(User, related_name='projects')
project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)
def get_absolute_url(self):
@ -31,6 +31,12 @@ class Project(models.Model):
def is_type_of(self, project_type):
return project_type == self.project_type
def get_progress(self):
docs = self.get_documents(is_null=True)
total = self.documents.count()
remaining = docs.count()
return {'total': total, 'remaining': remaining}
@property
def image(self):
if self.is_type_of(self.DOCUMENT_CLASSIFICATION):

6
app/server/tests/test_models.py

@ -10,6 +10,12 @@ class TestProject(TestCase):
project = mixer.blend('server.Project')
project.is_type_of(project.project_type)
def test_get_progress(self):
project = mixer.blend('server.Project')
res = project.get_progress()
self.assertEqual(res['total'], 0)
self.assertEqual(res['remaining'], 0)
class TestLabel(TestCase):

Loading…
Cancel
Save