Esempio n. 1
0
    def mutate(self, _, id: str, category: str, is_published: bool = False):
        category_wrapper = process_model_wrapper(CategoryWrapper,
                                                 id=id,
                                                 category=category.strip(),
                                                 is_published=is_published)

        return UpdateCategory(success=True, model=category_wrapper.model)
Esempio n. 2
0
    def mutate(self,
               _,
               id: str,
               url: str,
               name: str,
               rank: Mapping,
               categories: Sequence[str] = (),
               is_published: bool = False):
        if len(categories) == 0:
            categories: Sequence[str] = \
                (CategoryWrapper.model_class.objects.get_or_create(category='uncategorized')[0].id, )

        url = url.strip()
        name = name.strip()

        categories: List[CategoryWrapper] = get_wrappers_by_ids(
            CategoryWrapper, categories)

        tutorial_anchor_wrapper = process_model_wrapper(
            TutorialAnchorWrapper,
            id=id,
            url=url,
            name=name,
            level=rank['level'],
            section=rank['section'],
            categories=categories,
            is_published=is_published)

        return UpdateTutorialAnchor(success=True,
                                    model=tutorial_anchor_wrapper.model)
Esempio n. 3
0
    def mutate(self, _, id: str, name: str, code: str, tutorial: str):
        tutorial_wrapper = get_wrapper_by_id(TutorialAnchorWrapper, tutorial)

        code_wrapper = process_model_wrapper(CodeWrapper,
                                             id=id, name=name, code=code, tutorial=tutorial_wrapper)

        return UpdateCode(success=True, model=code_wrapper.model)
Esempio n. 4
0
    def mutate(self, _, lang: str, content: MutableMapping):
        graph_info_translation_table: Optional[Type[GraphTranslationBase]] = get_graph_info_trans_table(lang.strip())

        if not graph_info_translation_table:
            raise GraphQLError(f'Language {lang} is not supported.')

        content['graph_anchor'] = get_wrapper_by_id(GraphWrapper, content['graph_anchor'])

        graph_info_content_wrapper = process_model_wrapper(GraphTranslationContentWrapper,
                                                           model_class=graph_info_translation_table,
                                                           **content)

        return UpdateGraphInfoContent(success=True, model=graph_info_content_wrapper.model)
Esempio n. 5
0
    def mutate(self, _, code_id, result_json_dict: MutableMapping):
        if not isinstance(result_json_dict, Mapping):
            raise GraphQLError('`result_json_dict` must be a mapping of graph ids and result jsons')

        code_wrapper: CodeWrapper = get_wrapper_by_id(CodeWrapper, code_id)

        result_json_models = []
        for key, value in result_json_dict.items():
            graph_wrapper: GraphWrapper = get_wrapper_by_id(GraphWrapper, key)
            json_obj = json.loads(value) if isinstance(value, str) else value
            result_wrapper: ExecResultJsonWrapper = process_model_wrapper(ExecResultJsonWrapper,
                                                                          code=code_wrapper, graph=graph_wrapper,
                                                                          json=json_obj)
            result_json_models.append(result_wrapper.model)

        return UpdateResultJson(success=True, models=result_json_models)
Esempio n. 6
0
    def mutate(self, info):

        files: Mapping[str, InMemoryUploadedFile] = info.context.FILES

        if len(files) == 0:
            raise GraphQLError('No files are added.')

        if not all('image' in file.content_type for file in files.values()):
            raise GraphQLError('Invalid form data.')

        wrappers: List[UploadsWrapper] = []

        for name, file in files.items():
            wrappers.append(process_model_wrapper(UploadsWrapper, file=file, alias=name))

        return UploadStatic(success=True, urls=[wrapper.model.relative_url for wrapper in wrappers])
Esempio n. 7
0
    def mutate(self,
               email: str,
               username: str,
               password: str,
               invitation_code: str,
               first_name: str = '',
               last_name: str = ''):
        email = email.strip()
        username = username.strip()
        password = password.strip()
        invitation_code = invitation_code.strip()
        first_name = first_name.strip()
        last_name = last_name.strip()

        Register.check_existence(email, username)

        if not invitation_code:
            raise GraphQLError("You have to enter an invitation code!")

        invitation_model: Optional[
            InvitationCodeModel] = InvitationCodeModel.objects.get(
                invitation_code=invitation_code)

        if invitation_model is None or not invitation_model.usable:
            raise GraphQLError("Invitation code %s is not valid." %
                               invitation_code)

        role: int = invitation_model.role

        try:
            password_validator(password)
        except AssertionError as e:
            raise GraphQLError(f'Malformed password. Error: {e}')

        with transaction.atomic():
            user_wrapper: UserWrapper = process_model_wrapper(
                UserWrapper,
                email=email,
                username=username,
                role=role,
                first_name=first_name,
                last_name=last_name)

            user_wrapper.model.set_password(password)
            user_wrapper.save_model()

        return Register(success=True, user=user_wrapper.model)
Esempio n. 8
0
    def mutate(self, _, id: str, url: str, name: str, cyjs: Mapping, is_published: bool = False,
               priority: int = GraphPriority.TRIV, authors: Sequence[str] = (), categories: Sequence[str] = (),
               tutorials: Sequence[str] = ()):
        url = url.strip()
        name = name.strip()

        if not isinstance(cyjs, Mapping):
            raise GraphQLError('CYJS must be a json mapping')

        author_wrappers: List[UserWrapper] = get_wrappers_by_ids(UserWrapper, authors)
        category_wrappers: List[CategoryWrapper] = get_wrappers_by_ids(CategoryWrapper, categories)
        tutorial_wrappers: List[TutorialAnchorWrapper] = get_wrappers_by_ids(TutorialAnchorWrapper, tutorials)

        graph_wrapper: GraphWrapper = process_model_wrapper(GraphWrapper,
                                                            id=id, url=url, name=name, cyjs=cyjs,
                                                            is_published=is_published, priority=priority,
                                                            authors=author_wrappers, categories=category_wrappers,
                                                            tutorials=tutorial_wrappers)

        return UpdateGraph(success=True, model=graph_wrapper.model)
Esempio n. 9
0
    def mutate(self, _, lang: str, content: MutableMapping):
        translation_table: Optional[Type[TranslationBase]] = get_translation_table(lang.strip())

        if not translation_table:
            raise GraphQLError(f'Language {lang} is no supported.')

        content['authors'] = get_wrappers_by_ids(UserWrapper, content['authors'])
        content['tutorial_anchor'] = get_wrapper_by_id(TutorialAnchorWrapper, content['tutorial_anchor'])

        graph_set: QuerySet[Graph] = Graph.objects.filter(id__in=content.pop('graph_set'))

        with transaction.atomic():
            tutorial_content_wrapper = process_model_wrapper(TutorialTranslationContentWrapper,
                                                             model_class=translation_table, **content)

            tutorial_content_wrapper.model.tutorial_anchor.graph_set.set(graph_set)

            if hasattr(tutorial_content_wrapper.model, 'code'):
                code_list = [tutorial_content_wrapper.model.code]
                _result_json_updater(code_list, graph_set)

        return UpdateTutorialContent(success=True, model=tutorial_content_wrapper.model)
Esempio n. 10
0
    def mutate(self, email: str, username: str, password: str, invitation_code: str):
        email = email.strip()
        username = username.strip()
        password = password.strip()
        invitation_code = invitation_code.strip()

        Register.check_existence(email, username)

        if not invitation_code:
            raise GraphQLError("You have to enter an invitation code!")

        role_string: Optional[str] = next((label for label, value in InvitationCode.code_collection.items()
                                          if value == invitation_code), None)

        if role_string is None:
            raise GraphQLError("Invitation code %s is not valid." % invitation_code)

        role: int = InvitationCode.role_mapping[role_string]

        user_wrapper: UserWrapper = process_model_wrapper(UserWrapper,
                                                          email=email, username=username, password=password, role=role)

        return Register(success=True, user=user_wrapper.model)
Esempio n. 11
0
    def mutate(self, _, lang: str, content: MutableMapping):
        translation_table: Optional[
            Type[TranslationBase]] = get_translation_table(lang.strip())

        if not translation_table:
            raise GraphQLError(f'Language {lang} is no supported.')

        content['authors'] = get_wrappers_by_ids(UserWrapper,
                                                 content['authors'])
        content['tutorial_anchor'] = get_wrapper_by_id(
            TutorialAnchorWrapper, content['tutorial_anchor'])

        graph_set: List[str] = content.pop('graph_set')

        tutorial_content_wrapper = process_model_wrapper(
            TutorialTranslationContentWrapper,
            model_class=translation_table,
            **content)

        tutorial_content_wrapper.model.tutorial_anchor.graph_set.set(
            Graph.objects.filter(id__in=graph_set))

        return UpdateTutorialContent(success=True,
                                     model=tutorial_content_wrapper.model)