Esempio n. 1
0
    def put(self, item_id):
        json_data = request.get_json()
        task = Task.query.get(item_id)
        if task is None:
            return jsonify({'error': 'Task not found.'}), 404

        result = TaskSchema().load(json_data)

        if result.errors:
            return jsonify(result.errors), 403

        project = task.project
        if not self.is_project_member(project):
            return jsonify({'error': 'Project not found.'}), 404

        schema_data = result.data
        if schema_data.get('is_completed') is True:
            schema_data['completed_at'] = str(datetime.datetime.now())
            schema_data['completed_by_user_id'] = current_identity.id
        if schema_data.get('is_completed') is False:
            schema_data['completed_at'] = None
            schema_data['completed_by_user_id'] = None
        parse_json_to_object(task, result.data)

        db.session.add(task)
        db.session.commit()

        data = TaskSchema().dump(task).data
        return jsonify(data)
Esempio n. 2
0
    def post(self):
        json_data = request.get_json()
        result = TaskCreateSchema().load(json_data)

        if result.errors:
            return jsonify(result.errors), 403

        project = Project.query.get(result.data['project_id'])
        if project is None:
            return jsonify({'error': 'Project not found.'}), 404

        if not self.is_project_member(project):
            return jsonify({'error': 'Project not found.'}), 404

        task = Task()
        parse_json_to_object(task, result.data)
        task.creator = current_identity

        db.session.add(task)
        db.session.flush()

        tasks_order = list(project.tasks_order)
        tasks_order.insert(0, task.id)
        parse_json_to_object(project, {'tasks_order': tasks_order})

        db.session.add(project)
        db.session.commit()

        data = TaskSchema().dump(task).data
        return jsonify(data)
Esempio n. 3
0
 def put(self):
     json_data = request.get_json()
     result = UserNameUpdateSchema().load(json_data)
     if result.errors:
         return jsonify(result.errors), 403
     user = User.query.get(current_identity.id)
     parse_json_to_object(user, result.data)
     db.session.add(user)
     db.session.commit()
     result = UserSchema().dump(user)
     return jsonify(result.data)
Esempio n. 4
0
 def post(self):
     json_data = request.get_json()
     result = UserRegistrationSchema().load(json_data)
     if result.errors:
         return jsonify(result.errors), 403
     user = User()
     parse_json_to_object(user, result.data)
     db.session.add(user)
     db.session.commit()
     token = generate_jwt_token(user.id)
     return jsonify({'token': token})
Esempio n. 5
0
    def post(self):
        json_data = request.get_json()
        result = ProjectCreateSchema().load(json_data)

        if result.errors:
            return jsonify(result.errors), 403

        project = Project()
        parse_json_to_object(project, result.data)
        project.owner = current_identity

        db.session.add(project)
        db.session.commit()

        data = ProjectSchema().dump(project).data
        return jsonify(data)
Esempio n. 6
0
    def delete(self, item_id):
        task = Task.query.get(item_id)
        if task is None:
            return jsonify({'error': 'Task not found.'}), 404

        project = task.project
        if not self.is_project_member(project):
            return jsonify({'error': 'Project not found.'}), 404

        task.is_deleted = True

        tasks_order = list(project.tasks_order)
        tasks_order.remove(task.id)
        parse_json_to_object(project, {'tasks_order': tasks_order})

        db.session.add(task)
        db.session.commit()

        return jsonify({})
Esempio n. 7
0
    def put(self, item_id):
        json_data = request.get_json()
        project = Project.query.get(item_id)
        if project is None:
            return jsonify({'error': 'Project not found.'}), 404

        if current_identity.id == project.owner_id:
            result = ProjectSchema().load(json_data)
        else:
            result = ProjectSchema(exclude=('is_deleted', )).load(json_data)

        if result.errors:
            return jsonify(result.errors), 403

        parse_json_to_object(project, result.data)

        db.session.add(project)
        db.session.commit()

        data = ProjectSchema().dump(project).data
        return jsonify(data)
Esempio n. 8
0
    def post(self):
        """Invite user to project

        ``Example request``:

        .. sourcecode:: http

            POST /invites HTTP/1.1
            Accept: application/json
            Content-Type: application/json
            JWTAuthorization: JWT <jwt_token>

            {
                "email": "*****@*****.**",
                "project_id": 1
            }


        ``Example response``:

        .. sourcecode:: http

            HTTP/1.1 200 OK
            Content-Type: application/json

            {
                "code": "380eca36-4a8d-30f5-969e-ff8363c592f8",
                "email": "*****@*****.**",
                "id": 1,
                "invite_link": "http://localhost:3000/invite/380eca36-4a8d-30f5-969e-ff8363c592f8/",
                "invite_type": "external",
                "project_id": 1,
                "status": "pending"
            }
        """
        json_data = request.get_json()

        invite_type = Invite.TYPE_EXTERNAL
        user = User.query.filter(User.email == json_data['email']).first()
        if user:
            invite_type = Invite.TYPE_INTERNAL
        json_data['invite_type'] = invite_type

        result = InviteSchema().load(json_data)

        if result.errors:
            return jsonify(result.errors), 403

        project = Project.query.get(int(result.data['project_id']))
        if project is None:
            return jsonify({'error': 'Project not found.'}), 404

        if project.owner.email == result.data['email']:
            return jsonify({'email': 'This user already invited.'}), 403

        if result.data['email'] in [
                collab.email for collab in project.collaborators.all()
        ]:
            return jsonify({'email': 'This user already invited.'}), 403

        try:
            invite = Invite.query.filter(
                Invite.email == result.data['email'],
                Invite.project_id == int(result.data['project_id']),
                Invite.status != Invite.STATUS_REJECTED).one()
            return jsonify({'email': 'This user already invited.'}), 403
        except NoResultFound:
            invite = Invite()

        parse_json_to_object(invite, result.data)

        db.session.add(invite)
        db.session.flush()

        invite_link = "{0}{1}/".format(current_app.config['INVITE_REG_LINK'],
                                       result.data['code'])
        send_email(
            'invite.html', {
                "link": invite_link,
                "email": current_identity.email,
                "username": current_identity.name,
                "project": invite.project.name
            }, u"Invite test", [result.data['email']])
        invite.invite_link = invite_link

        db.session.add(invite)
        db.session.commit()

        data = InviteSchema().dump(invite).data
        return jsonify(data)
Esempio n. 9
0
    def put(self):
        """Accept invite to project

        ``Example request``:

        .. sourcecode:: http

            PUT /invites HTTP/1.1
            Accept: application/json
            Content-Type: application/json

            {
                "code": "380eca36-4a8d-30f5-969e-ff8363c592f8"
            }


        ``Example response``:

        .. sourcecode:: http

            HTTP/1.1 200 OK
            Content-Type: application/json

            {
                "token": <jwt_token>
            }
        """
        json_data = request.get_json()

        code = json_data.pop('code', None)
        if code is None:
            return jsonify({'error': 'Invite code is not provided.'}), 403
        try:
            invite = Invite.query.filter(
                Invite.status == Invite.STATUS_PENDING,
                Invite.code == code).one()
        except NoResultFound:
            return jsonify({'error': 'Invite is not valid.'}), 403

        try:
            user = User.query.filter(User.email == invite.email).one()

            invite_type = Invite.TYPE_INTERNAL
        except NoResultFound:
            invite_type = Invite.TYPE_EXTERNAL

            result = UserRegistrationSchema().load(json_data)

            if result.errors:
                return jsonify(result.errors), 403

            user = User()
            parse_json_to_object(user, result.data)

            db.session.add(user)
            db.session.flush()

        invite.invite_type = invite_type
        invite.status = Invite.STATUS_ACCEPTED
        db.session.add(invite)

        project = invite.project
        project.collaborators.append(user)
        db.session.add(project)
        db.session.commit()

        return jsonify({'token': generate_jwt_token(str(user.id))})