Example #1
0
    def mutate(_root, info, title, hint=""):
        sender = info.context.user

        if not sender.is_accessible:
            raise ValueError(_("sorry, the genie is now busy"))

        topic = Topic.objects.get_or_pseudo(unicode_string=title)
        hint = smart_lower(hint).strip() or None

        if hint:
            validate_user_text(hint, exctype=ValueError)

        if not topic.valid or (topic.exists and (topic.is_banned or topic.has_entries)):
            raise ValueError(_("we couldn't handle your request. try again later."))

        if not topic.exists:
            topic = Topic.objects.create_topic(title=title)
        else:
            previous_wish = topic.wishes.filter(author=sender)
            deleted, _types = previous_wish.delete()

            if deleted:
                return WishTopic(feedback=_("your wish has been deleted"))

        Wish.objects.create(topic=topic, author=sender, hint=hint)

        return WishTopic(
            feedback=_("your wish is now enlisted. if someone starts a discussion, we will let you know."),
            hint=linebreaksbr(formatted(hint)),
        )
Example #2
0
    def mutate(_root, info, body, recipient):
        sender = info.context.user
        if len(body) < 3:
            return ComposeMessage(
                feedback=_("can't you write down something more?"))

        try:
            recipient_ = Author.objects.get(username=recipient)
            validate_user_text(body)
        except Author.DoesNotExist:
            return ComposeMessage(feedback=_("no such person though"))
        except ValidationError as error:
            return ComposeMessage(feedback=error.message)

        sent = Message.objects.compose(sender, recipient_, body)

        if not sent:
            return ComposeMessage(feedback=_("we couldn't send your message"))

        return ComposeMessage(
            feedback=_("your message has been successfully sent"))
Example #3
0
    def mutate(_root, info, content, pk=None, title=None):
        validate_user_text(content, exctype=ValueError)

        if pk:
            entry = Entry.objects_all.get(is_draft=True,
                                          author=info.context.user,
                                          pk=pk)
            entry.content = content
            entry.date_edited = timezone.now()
            entry.save(update_fields=["content", "date_edited"])
            return DraftEdit(
                pk=entry.pk,
                content=linebreaksbr(formatted(entry.content)),
                feedback=_("your changes have been saved as draft"),
            )

        if title:
            topic = Topic.objects.get_or_pseudo(unicode_string=title)

            if (topic.exists and topic.is_banned) or not topic.valid:
                raise ValueError(
                    _("we couldn't handle your request. try again later."))

            if not topic.exists:
                topic = Topic.objects.create_topic(title=topic.title)

            entry = Entry(author=info.context.user,
                          topic=topic,
                          content=content,
                          is_draft=True)
            entry.save()
            return DraftEdit(
                pk=entry.pk,
                content=linebreaksbr(formatted(entry.content)),
                feedback=_("your entry has been saved as draft"),
            )

        raise ValueError(
            _("we couldn't handle your request. try again later."))