Add model and mutation for adding notes
This commit is contained in:
parent
95bc633cfb
commit
a8a6abb2d8
|
|
@ -11,12 +11,13 @@ from basicknowledge.queries import BasicKnowledgeQuery
|
|||
from books.schema.mutations.main import BookMutations
|
||||
from books.schema.queries import BookQuery
|
||||
from core.schema.mutations.main import CoreMutations
|
||||
from notes.mutations import NoteMutations
|
||||
from objectives.mutations import ObjectiveMutations
|
||||
from objectives.schema import ObjectivesQuery
|
||||
from portfolio.mutations import PortfolioMutations
|
||||
from portfolio.schema import PortfolioQuery
|
||||
from surveys.schema import SurveysQuery
|
||||
from surveys.mutations import SurveysMutations
|
||||
from surveys.mutations import SurveyMutations
|
||||
from rooms.mutations import RoomMutations
|
||||
from rooms.schema import RoomsQuery, ModuleRoomsQuery
|
||||
from users.schema import UsersQuery
|
||||
|
|
@ -32,7 +33,7 @@ class Query(UsersQuery, ModuleRoomsQuery, RoomsQuery, ObjectivesQuery, BookQuery
|
|||
|
||||
|
||||
class Mutation(BookMutations, RoomMutations, AssignmentMutations, ObjectiveMutations, CoreMutations, PortfolioMutations,
|
||||
ProfileMutations, SurveysMutations, graphene.ObjectType):
|
||||
ProfileMutations, SurveyMutations, NoteMutations, graphene.ObjectType):
|
||||
|
||||
if settings.DEBUG:
|
||||
debug = graphene.Field(DjangoDebug, name='__debug')
|
||||
|
|
|
|||
|
|
@ -146,37 +146,3 @@ class DeleteContentBlock(relay.ClientIDMutation):
|
|||
return cls(success=True)
|
||||
except ContentBlock.DoesNotExist:
|
||||
return cls(success=False, errors='Content block not found')
|
||||
|
||||
|
||||
class UpdateContentBookmark(relay.ClientIDMutation):
|
||||
class Input:
|
||||
id = graphene.String(required=True)
|
||||
content_block = graphene.ID(required=True)
|
||||
bookmarked = graphene.Boolean(required=True)
|
||||
|
||||
success = graphene.Boolean()
|
||||
errors = graphene.String()
|
||||
|
||||
@classmethod
|
||||
def mutate_and_get_payload(cls, root, info, **kwargs):
|
||||
id = kwargs.get('id')
|
||||
user = info.context.user
|
||||
content_block_id = kwargs.get('content_block')
|
||||
bookmarked = kwargs.get('bookmarked')
|
||||
|
||||
content_block = get_object(ContentBlock, content_block_id)
|
||||
|
||||
if bookmarked:
|
||||
ContentBlockBookmark.objects.create(
|
||||
content_block=content_block,
|
||||
id=id,
|
||||
user=user
|
||||
)
|
||||
else:
|
||||
ContentBlockBookmark.objects.get(
|
||||
content_block=content_block,
|
||||
id=id,
|
||||
user=user
|
||||
).delete()
|
||||
|
||||
return cls(success=True)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
from books.schema.mutations.contentblock import MutateContentBlock, AddContentBlock, DeleteContentBlock, \
|
||||
UpdateContentBookmark
|
||||
from books.schema.mutations.contentblock import MutateContentBlock, AddContentBlock, DeleteContentBlock
|
||||
from books.schema.mutations.module import UpdateSolutionVisibility, UpdateLastModule
|
||||
|
||||
|
||||
|
|
@ -9,4 +8,3 @@ class BookMutations(object):
|
|||
delete_content_block = DeleteContentBlock.Field()
|
||||
update_solution_visibility = UpdateSolutionVisibility.Field()
|
||||
update_last_module = UpdateLastModule.Field()
|
||||
update_content_bookmark = UpdateContentBookmark.Field()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
import graphene
|
||||
from graphene import InputObjectType
|
||||
|
||||
|
||||
class AddNoteArgument(InputObjectType):
|
||||
content = graphene.UUID(required=True)
|
||||
content_block = graphene.ID(required=True)
|
||||
text = graphene.String(required=True)
|
||||
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
# Generated by Django 2.0.6 on 2019-10-09 09:19
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('notes', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Note',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('text', models.TextField()),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='contentblockbookmark',
|
||||
name='note',
|
||||
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='notes.Note'),
|
||||
),
|
||||
]
|
||||
|
|
@ -5,9 +5,14 @@ from core.wagtail_utils import StrictHierarchyPage
|
|||
from users.models import User
|
||||
|
||||
|
||||
class Note(models.Model):
|
||||
text = models.TextField()
|
||||
|
||||
|
||||
class Bookmark(models.Model):
|
||||
id = models.UUIDField(primary_key=True)
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
note = models.OneToOneField(Note, null=True, on_delete=models.SET_NULL)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
import graphene
|
||||
import json
|
||||
from graphene import relay
|
||||
|
||||
from api.utils import get_object
|
||||
from books.models import ContentBlock
|
||||
from notes.inputs import AddNoteArgument
|
||||
from notes.models import ContentBlockBookmark, Note
|
||||
from notes.schema import NoteNode
|
||||
|
||||
class UpdateContentBookmark(relay.ClientIDMutation):
|
||||
class Input:
|
||||
id = graphene.String(required=True)
|
||||
content_block = graphene.ID(required=True)
|
||||
bookmarked = graphene.Boolean(required=True)
|
||||
|
||||
success = graphene.Boolean()
|
||||
errors = graphene.String()
|
||||
|
||||
@classmethod
|
||||
def mutate_and_get_payload(cls, root, info, **kwargs):
|
||||
id = kwargs.get('id')
|
||||
user = info.context.user
|
||||
content_block_id = kwargs.get('content_block')
|
||||
bookmarked = kwargs.get('bookmarked')
|
||||
|
||||
content_block = get_object(ContentBlock, content_block_id)
|
||||
|
||||
if bookmarked:
|
||||
ContentBlockBookmark.objects.create(
|
||||
content_block=content_block,
|
||||
id=id,
|
||||
user=user
|
||||
)
|
||||
else:
|
||||
ContentBlockBookmark.objects.get(
|
||||
content_block=content_block,
|
||||
id=id,
|
||||
user=user
|
||||
).delete()
|
||||
|
||||
return cls(success=True)
|
||||
|
||||
|
||||
|
||||
class AddNote(relay.ClientIDMutation):
|
||||
class Input:
|
||||
note = graphene.Argument(AddNoteArgument)
|
||||
|
||||
note = graphene.Field(NoteNode)
|
||||
|
||||
@classmethod
|
||||
def mutate_and_get_payload(cls, root, info, **kwargs):
|
||||
user = info.context.user
|
||||
|
||||
note = kwargs.get('note')
|
||||
content_uuid = note.get('content')
|
||||
content_block_id = note.get('content_block')
|
||||
content_block = get_object(ContentBlock, content_block_id)
|
||||
text = note.get('text')
|
||||
|
||||
bookmark = ContentBlockBookmark.objects.get(
|
||||
content_block=content_block,
|
||||
id=content_uuid,
|
||||
user=user
|
||||
)
|
||||
|
||||
bookmark.note = Note.objects.create(text=text)
|
||||
bookmark.save()
|
||||
|
||||
return cls(note=bookmark.note)
|
||||
|
||||
|
||||
class NoteMutations:
|
||||
add_note = AddNote.Field()
|
||||
update_content_bookmark = UpdateContentBookmark.Field()
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
import graphene
|
||||
from graphene import relay
|
||||
from graphene_django import DjangoObjectType
|
||||
|
||||
from notes.models import Note
|
||||
|
||||
|
||||
class NoteNode(DjangoObjectType):
|
||||
pk = graphene.Int()
|
||||
|
||||
class Meta:
|
||||
model = Note
|
||||
interfaces = (relay.Node,)
|
||||
|
||||
def resolve_pk(self, *args, **kwargs):
|
||||
return self.id
|
||||
|
|
@ -33,5 +33,5 @@ class UpdateAnswer(relay.ClientIDMutation):
|
|||
return cls(answer=answer)
|
||||
|
||||
|
||||
class SurveysMutations:
|
||||
class SurveyMutations:
|
||||
update_answer = UpdateAnswer.Field()
|
||||
|
|
|
|||
Loading…
Reference in New Issue