def mutate_and_get_payload(cls, root, info, link_id, user_id, client_mutation_id=None): user = get_user_from_auth_token(info.context) or None if not user: raise Exception('Only logged-in users may vote!') if user_id: user_from_id = Node.get_node_from_global_id(info, user_id) if (not user_from_id) or (user_from_id.pk != user.pk): raise Exception( 'Supplied user id does not match logged-in user!') link = Node.get_node_from_global_id(info, link_id) if not link: raise Exception('Requested link not found!') if VoteModel.objects.filter(user_id=user.pk, link_id=link.pk).count() > 0: raise Exception('A vote already exists for this user and link!') vote = VoteModel(user_id=user.pk, link_id=link.pk) vote.save() return CreateVote(vote=vote)
def mutate_and_get_payload(cls, root, info, url, description, posted_by_id=None, client_mutation_id=None): # In order to have this work with early stages of the front-end tutorial, this will allow # links to be created without a user auth token or postedById. If a postedById is present, # then the auth token must be as well. If both are present, then they must agree. user = get_user_from_auth_token(info.context) or None if user or posted_by_id: if not user: raise Exception('Only logged-in users may create links!') if posted_by_id: try: posted_by_user = Node.get_node_from_global_id( info, posted_by_id) assert posted_by_user.pk == user.pk except: raise Exception('postedById does not match user ID!') link = LinkModel( url=url, description=description, posted_by=user, ) link.save() return CreateLink(link=link)
def default_model_resolver(self, info, id): return Node.get_node_from_global_id(info, id)