예제 #1
0
    def mutate_and_get_payload(cls, root, info, **input):
        if current_user.is_anonymous:
            return cls(
                success=False,
                errors=[UserError(field="", message="Authentication required")]
            )

        contact_id = input['contact_id']
        contact = Contact.query.get(contact_id)
        if not contact or contact.user != current_user:
            return cls(
                success=False,
                errors=[UserError(
                    field="contact_id",
                    message="Contact not found"
                )]
            )

        serializer = ContactSerializer(exclude=['user'], partial=True)
        result = serializer.load(input, instance=contact)

        if result.errors:
            return cls(
                success=False,
                errors=UserError.from_marshmallow(result.errors)
            )

        db.session.add(result.data)
        db.session.commit()

        return cls(
            success=True,
            contact=contact,
        )
예제 #2
0
    def mutate_and_get_payload(cls, root, info, **input):
        if current_user.is_anonymous:
            return cls(
                success=False,
                errors=[UserError(field="", message="Authentication required")]
            )

        contact_id = input['contact_id']
        contact = Contact.query.get(contact_id)
        if not contact or contact.user != current_user:
            return cls(
                success=False,
                errors=[UserError(
                    field="contact_id",
                    message="Contact not found"
                )]
            )

        db.session.delete(contact)
        db.session.commit()

        return cls(
            success=True,
            contact=contact,
        )
예제 #3
0
    def mutate_and_get_payload(cls, root, info, **input):
        if current_user.is_anonymous:
            return cls(
                success=False,
                errors=[UserError(field="", message="Authentication required")]
            )

        serializer = ContactSerializer(exclude=['user'])
        result = serializer.load(input)

        if result.errors:
            return cls(
                success=False,
                errors=UserError.from_marshmallow(result.errors)
            )

        contact = result.data
        contact.user = current_user
        db.session.add(contact)
        db.session.commit()

        return cls(
            success=True,
            contact=contact,
        )
예제 #4
0
    def mutate_and_get_payload(cls, root, info, **input):
        if current_user.is_anonymous:
            return cls(
                success=False,
                errors=[UserError(field="", message="Authentication required")]
            )

        tag_id = input['tag_id']
        tag = Tag.query.get(tag_id)
        if not tag or tag.user != current_user:
            return cls(
                success=False,
                errors=[UserError(
                    field="tag_id",
                    message="Tag not found"
                )]
            )

        serializer = TagSerializer(exclude=['user'], partial=True)
        result = serializer.load(input, instance=tag)

        if result.errors:
            return cls(
                success=False,
                errors=UserError.from_marshmallow(result.errors)
            )

        db.session.add(result.data)
        db.session.commit()

        return cls(
            success=True,
            tag=tag,
        )
예제 #5
0
    def mutate_and_get_payload(cls, root, info, **input):
        if current_user.is_anonymous:
            return cls(
                success=False,
                errors=[UserError(field="", message="Authentication required")]
            )

        tag_id = input['tag_id']
        tag = Tag.query.get(tag_id)
        if not tag or contact.user != current_user:
            return cls(
                success=False,
                errors=[UserError(
                    field="tag_id",
                    message="Tag not found"
                )]
            )

        db.session.delete(tag)
        db.session.commit()

        return cls(
            success=True,
            tag=tag,
        )
예제 #6
0
 def mutate_and_get_payload(cls, root, info, **input):
     result = UserSerializer().load(input)
     if result.errors:
         return cls(
             success=False,
             errors=UserError.from_marshmallow(result.errors),
         )
     user_exists = db.session.query(
         User.query.filter(User.username == input['username']).exists()
     ).scalar()
     if user_exists:
         return cls(
             success=False,
             errors=[UserError(
                 field="username", message="Username already in use"
             )]
         )
     user = result.data
     db.session.add(user)
     db.session.commit()
     return cls(
         success=True,
         token=jwt_token_for_user(user).decode('utf8'),
         viewer=user,
     )
예제 #7
0
 def mutate_and_get_payload(cls, root, info, *, username, password, **input):
     user = (
         User.query
         .filter(User.username == username)
         .scalar()
     )
     if not user:
         return cls(
             success=False,
             errors=[
                 UserError('username', 'Specified user does not exist')
             ],
             token=None,
             viewer=None,
         )
     if not user.active:
         return cls(
             success=False,
             errors=[
                 UserError('username', 'Account is disabled')
             ],
             token=None,
             viewer=None,
         )
     if not user.verify_password(password):
         return cls(
             success=False,
             errors=[
                 UserError('password', 'Invalid password')
             ],
             token=None,
             viewer=None,
         )
     # Login was successful!
     login_user(user)
     return cls(
         success=True,
         errors=[],
         token=jwt_token_for_user(user).decode('utf8'),
         viewer=user,
     )
예제 #8
0
    def mutate_and_get_payload(cls, root, info, **input):
        if current_user.is_anonymous:
            return cls(
                success=False,
                errors=[UserError(field="", message="Authentication required")]
            )

        serializer = TagSerializer(exclude=['user'])
        result = serializer.load(input)

        if result.errors:
            return cls(
                success=False,
                errors=UserError.from_marshmallow(result.errors)
            )

        tag = result.data
        tag.user = current_user
        db.session.add(tag)

        try:
            db.session.commit()
        except IntegrityError:
            message = 'Tag with name "{name}" already exists'.format(
                name=tag.name
            )
            error = UserError(field="name", message=message)
            return cls(
                success=False,
                errors=[error],
            )

        return cls(
            success=True,
            errors=[],
            tag=tag,
        )