Fix PHEP-3 (edit project as other user)
This commit is contained in:
parent
ee05ee79ba
commit
2a6993cad8
|
|
@ -1,11 +1,11 @@
|
|||
from django.contrib.auth import get_user_model
|
||||
from django.db import models
|
||||
from django_extensions.db.models import TitleSlugDescriptionModel
|
||||
from graphql_relay import to_global_id
|
||||
|
||||
from core.mixins import GraphqlNodeMixin
|
||||
from users.models import User
|
||||
|
||||
|
||||
class Project(TitleSlugDescriptionModel, GraphqlNodeMixin):
|
||||
objectives = models.TextField(blank=True)
|
||||
appearance = models.CharField(blank=True, null=False, max_length=255)
|
||||
|
|
@ -21,6 +21,7 @@ class Project(TitleSlugDescriptionModel, GraphqlNodeMixin):
|
|||
self.final and self.student.get_teacher().id == user.id
|
||||
)
|
||||
|
||||
|
||||
class ProjectEntry(models.Model):
|
||||
activity = models.TextField(blank=True)
|
||||
reflection = models.TextField(blank=True)
|
||||
|
|
|
|||
|
|
@ -24,23 +24,11 @@ class MutateProject(relay.ClientIDMutation):
|
|||
|
||||
@classmethod
|
||||
def mutate_and_get_payload(cls, root, info, **kwargs):
|
||||
data = kwargs.get('project')
|
||||
data['student'] = info.context.user.id
|
||||
raise Exception('Must be subclassed')
|
||||
|
||||
if data.get('id') is not None:
|
||||
entity = get_object(Project, data['id'])
|
||||
serializer = ProjectSerializer(entity, data=data)
|
||||
else:
|
||||
serializer = ProjectSerializer(data=data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
props = {
|
||||
'project': serializer.instance,
|
||||
'errors': None
|
||||
}
|
||||
return cls(**props)
|
||||
|
||||
return cls(errors=['{}: {}'.format(key, value) for key, value in serializer.errors.items()])
|
||||
@classmethod
|
||||
def create_error_response(cls, serializer):
|
||||
return cls(room=None, errors=['{}: {}'.format(key, value) for key, value in serializer.errors.items()])
|
||||
|
||||
|
||||
class AddProject(MutateProject):
|
||||
|
|
@ -58,13 +46,37 @@ class AddProject(MutateProject):
|
|||
serializer.save()
|
||||
return cls(project=serializer.instance)
|
||||
|
||||
return cls(room=None, errors=['{}: {}'.format(key, value) for key, value in serializer.errors.items()])
|
||||
return cls.create_error_response(serializer)
|
||||
|
||||
|
||||
class UpdateProject(MutateProject):
|
||||
class Input:
|
||||
project = graphene.Argument(UpdateProjectArgument)
|
||||
|
||||
@classmethod
|
||||
def mutate_and_get_payload(cls, root, info, **kwargs):
|
||||
data = kwargs.get('project')
|
||||
|
||||
cls.user_is_owner(data, info.context.user)
|
||||
data['student'] = info.context.user.id
|
||||
|
||||
serializer = ProjectSerializer(data=data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
props = {
|
||||
'project': serializer.instance,
|
||||
'errors': None
|
||||
}
|
||||
return cls(**props)
|
||||
|
||||
return cls.create_error_response(serializer)
|
||||
|
||||
@classmethod
|
||||
def user_is_owner(cls, data, user):
|
||||
project = get_object(Project, data['id'])
|
||||
if not project or not project.student == user.id:
|
||||
raise PermissionDenied('not allowed')
|
||||
|
||||
|
||||
class MutateProjectEntry(relay.ClientIDMutation):
|
||||
errors = graphene.List(graphene.String)
|
||||
|
|
@ -73,7 +85,6 @@ class MutateProjectEntry(relay.ClientIDMutation):
|
|||
@classmethod
|
||||
def mutate_and_get_payload(cls, root, info, **kwargs):
|
||||
data = kwargs.get('project_entry')
|
||||
project = None
|
||||
|
||||
if data.get('project') is not None:
|
||||
project = get_object(Project, data.get('project'))
|
||||
|
|
|
|||
|
|
@ -91,7 +91,8 @@ mutation UpdateProjectMutation($input: UpdateProjectInput!){
|
|||
'input': input
|
||||
})
|
||||
self.assertIsNotNone(result.errors)
|
||||
self.assertTrue('Permission' in result.errors)
|
||||
self.assertTrue('message' in result.errors[0])
|
||||
self.assertEqual(result.errors[0]['message'], 'not allowed')
|
||||
|
||||
|
||||
class ProjectMutationsTestCase(DefaultUserTestCase):
|
||||
|
|
|
|||
Loading…
Reference in New Issue