From 5daeefbcecfce3e4b2b7ebf288f46dedee4c3e00 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Thu, 18 Oct 2018 10:02:02 +0200 Subject: [PATCH 1/2] Remove custom assignments, add `user created` flag --- .../migrations/0003_auto_20181018_0800.py | 25 +++++++++++++++++++ server/assignments/models.py | 5 +--- server/books/schema/mutations/utils.py | 1 + 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 server/assignments/migrations/0003_auto_20181018_0800.py diff --git a/server/assignments/migrations/0003_auto_20181018_0800.py b/server/assignments/migrations/0003_auto_20181018_0800.py new file mode 100644 index 00000000..9b15e907 --- /dev/null +++ b/server/assignments/migrations/0003_auto_20181018_0800.py @@ -0,0 +1,25 @@ +# Generated by Django 2.0.6 on 2018-10-18 08:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('assignments', '0002_auto_20181015_1237'), + ] + + operations = [ + migrations.RemoveField( + model_name='customassignment', + name='assignment_ptr', + ), + migrations.AddField( + model_name='assignment', + name='user_created', + field=models.BooleanField(default=False), + ), + migrations.DeleteModel( + name='CustomAssignment', + ), + ] diff --git a/server/assignments/models.py b/server/assignments/models.py index 86bb9846..c3b78d15 100644 --- a/server/assignments/models.py +++ b/server/assignments/models.py @@ -12,15 +12,12 @@ class Assignment(TimeStampedModel): owner = models.ForeignKey(get_user_model(), on_delete=models.PROTECT) # probably don't want to delete all assignments if a user gets deleted module = models.ForeignKey(Module, related_name='assignments', on_delete=models.CASCADE) + user_created = models.BooleanField(default=False) def __str__(self): return self.title -class CustomAssignment(Assignment): - pass - - class StudentSubmission(TimeStampedModel): text = models.TextField(blank=True) document = models.URLField(blank=True, default='', max_length=255) diff --git a/server/books/schema/mutations/utils.py b/server/books/schema/mutations/utils.py index f2aae216..fcbd0c3d 100644 --- a/server/books/schema/mutations/utils.py +++ b/server/books/schema/mutations/utils.py @@ -50,6 +50,7 @@ def handle_content_block(content, context, module, allowed_blocks=ALLOWED_BLOCKS assignment=content['value']['assignment'], owner=context.user, module=module, + user_created=True ) return { From 9edbcf3d5670c5a60f00d1ba9dfd0cf86f336470 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Thu, 18 Oct 2018 16:28:06 +0200 Subject: [PATCH 2/2] Add custom assignment test base --- .../tests/test_custom_assignments.py | 107 ++++++++++++++++++ server/books/tests/test_module_mutations.py | 2 +- 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 server/assignments/tests/test_custom_assignments.py diff --git a/server/assignments/tests/test_custom_assignments.py b/server/assignments/tests/test_custom_assignments.py new file mode 100644 index 00000000..94b739d8 --- /dev/null +++ b/server/assignments/tests/test_custom_assignments.py @@ -0,0 +1,107 @@ +import json + +from django.test import TestCase, RequestFactory +from graphene.test import Client + +from api import schema +from api.schema import schema +from assignments.factories import AssignmentFactory +from assignments.models import Assignment +from books.factories import ModuleFactory +from books.models import ContentBlock, Chapter +from core.factories import UserFactory +from users.models import User +from users.services import create_users + + +class CustomAssignmentTestCase(TestCase): + def setUp(self): + create_users() + + self.teacher = User.objects.get(username='teacher') + self.student1 = User.objects.get(username='student1') + self.assignment = AssignmentFactory( + owner=self.teacher, + user_created=True + ) + self.module = ModuleFactory() + chapter = Chapter(slug='some-slug', title='chapter') + self.module.add_child(instance=chapter) + + self.content_block = ContentBlock( + slug='slug', + title='title' + ) + + self.admin = UserFactory( + username='test', + is_staff=True, + is_superuser=True, + first_name='Nicol', + last_name='Bolas' + ) + + chapter.specific.add_child(instance=self.content_block) + + request = RequestFactory().get('/') + request.user = self.student1 + self.client = Client(schema=schema, context_value=request) + + def query_module(self): + query = ''' + query Module($slug: String!){ + module(slug: $slug) { + id + chapters { + edges { + node { + contentBlocks { + edges { + node { + contents + } + } + } + } + } + } + } + } + ''' + + result = self.client.execute(query, variables={ + 'slug': self.module.slug + }) + + self.assertIsNone(result.get('errors')) + return result + + @staticmethod + def get_first_contents(result): + return result.get('data').get('module').get('chapters').get('edges')[0].get('node').get('contentBlocks').get( + 'edges')[0].get('node').get('contents') + + def test_module_query(self): + result = self.query_module() + contents = self.get_first_contents(result) + self.assertIsNotNone(contents) + + def test_global_assignment(self): + title = 'Assignment' + assignment = Assignment.objects.create( + title=title, + assignment='Assignment text', + owner=self.admin, + module=self.module + ) + self.content_block.contents = json.dumps([ + { + 'type': 'assignment', + 'value': { + 'assignment_id': assignment.id + }} + ]) + self.content_block.save() + result = self.query_module() + contents = self.get_first_contents(result) + self.assertEqual(contents[0].get('value').get('title'), title) diff --git a/server/books/tests/test_module_mutations.py b/server/books/tests/test_module_mutations.py index 3ba1363b..be015fd3 100644 --- a/server/books/tests/test_module_mutations.py +++ b/server/books/tests/test_module_mutations.py @@ -3,7 +3,7 @@ from graphene.test import Client from graphql_relay import to_global_id from api.schema import schema -from api.utils import get_graphql_mutation, get_object +from api.utils import get_object from books.factories import ContentBlockFactory, ModuleFactory from books.models import ContentBlock from core.factories import UserFactory