Ejemplo n.º 1
0
    def put(self, movie):
        """Method for editing (PUT) a single movie item in the database"""
        db_movie = Movie.query.filter_by(name=movie).first()
        if db_movie is None:
            return create_error_response(404, "Not found", 
                "No movie was found with the name {}".format(movie)
            )
        
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                "Requests must be JSON"
            )

        try:
            validate(request.json, Movie.get_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
    
        db_movie.name = request.json["name"]
        db_movie.genre = request.json["genre"]
        
        try:
            db.session.commit()
        except IntegrityError:
            return create_error_response(409, "Already exists", 
                "Movie with name '{}' already exists.".format(request.json["name"])
            )

        return Response(status=204)
Ejemplo n.º 2
0
    def post(self):
        """
        POST method adds a new user to collection
        """
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, BoardBuilder.user_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        new_user = User(id=request.json["id"],
                        name=request.json["name"],
                        password=request.json["password"])

        try:
            db.session.add(new_user)
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "User with name '{}' already exists".format(
                    request.json["name"]))

        return Response(status=201,
                        headers={
                            "Location":
                            api.url_for(UserItem, name=request.json["name"])
                        })
Ejemplo n.º 3
0
    def post(self):
        """
        POST method adds new match to collection
        """
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Request must be JSON")

        try:
            validate(request.json, GameBuilder.match_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        new_match = Match(
            #id=request.json["id"],
            team1=request.json["team1"],
            team2=request.json["team2"],
            date=request.json["date"],
            team1_points=request.json["team1_points"],
            team2_points=request.json["team2_points"])

        try:
            db.session.add(new_match)
            db.session.commit()
        except IntegrityError:
            return create_error_response(409, "Already exists",
                                         "Match already inserted")

        return Response(
            status=201,
            headers={"Location": api.url_for(MatchItem, id=new_match.id)})
Ejemplo n.º 4
0
def check_for_json_errors(json_body):
    response = {}
    if json_body:
        leagues = json_body.get(CONSTANTS.LEAGUES_KEY)
        if leagues:
            date = json_body.get(CONSTANTS.DATE_KEY)
            if date and utils.validate_date(date):
                date_object = datetime.strptime(date, CONSTANTS.DATE_FORMAT)
                response = {
                    CONSTANTS.LEAGUES_KEY:
                    leagues,
                    CONSTANTS.DATE_KEY:
                    date_object,
                    CONSTANTS.INCLUDE_ODDS_KEY:
                    json_body.get(CONSTANTS.INCLUDE_ODDS_KEY)
                }
            else:
                message = "Need to provide valid date in format MM-DD-YYYY (ex: 02-18-2021)"
                response = utils.create_error_response(500, message)
        else:
            message = "Please provide valid leagues array (ex: \"leagues\": [\"nhl\", \"nba\"]"
            response = utils.create_error_response(500, message)
    else:
        message = "The request body is not valid json!"
        response = utils.create_error_response(500, message)
    return response
Ejemplo n.º 5
0
    def put(self, id):
        """
        PUT method edits a single throw 
        """
        db_throwid = Throw.query.filter_by(id=id).first()
        if db_throwid is None:
            return create_error_response(
                404, "Not found", "No throw was found with id {}".format(id))

        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, GameBuilder.throw_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        db_throwid.id = request.json["id"]
        db_throwid.points = request.json["points"]
        db_throwid.player_id = request.json["player_id"]
        db_throwid.match_id = request.json["match_id"]

        try:
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Throw with id '{}' already exists".format(request.json["id"]))

        return Response(status=204)
Ejemplo n.º 6
0
    def put(self, name):
        """
        PUT method edits a player
        """
        db_player = Player.query.filter_by(name=name).first()
        if db_player is None:
            return create_error_response(
                404, "Not found",
                "No player was found with name {}".format(name))

        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, GameBuilder.player_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        db_player.name = request.json["name"]
        db_player.team = request.json["team"]

        try:
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Player with name '{}' already exists".format(
                    request.json["name"]))

        return Response(status=204)
Ejemplo n.º 7
0
    def post(self):
        """
        POST method adds a new message to collection
        """
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, BoardBuilder.message_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        new_message = Message(id=request.json["id"],
                              parent_topic_id=request.json["parent_topic_id"],
                              message=request.json["message"],
                              time=request.json["time"],
                              user_id=request.json["user_id"])

        try:
            db.session.add(new_message)
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "message with id '{}' already exists".format(
                    request.json["id"]))

        return Response(status=201,
                        headers={
                            "Location":
                            api.url_for(MessageItem, id=request.json["id"])
                        })
Ejemplo n.º 8
0
    def post(self):
        """
        POST method adds a new throw to collection
        """
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, GameBuilder.throw_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        new_throw = Throw(id=request.json["id"],
                          points=request.json["points"],
                          player_id=request.json["player_id"],
                          match_id=request.json["match_id"])

        try:
            db.session.add(new_throw)
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Throw with id '{}' already exists".format(request.json["id"]))

        return Response(status=201,
                        headers={
                            "Location":
                            api.url_for(ThrowItem, id=request.json["id"])
                        })
Ejemplo n.º 9
0
    def post(self):
        """Method for POST a new single movie item to the database"""
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                "Requests must be JSON"
            )

        try:
            validate(request.json, Movie.get_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
        
        #Find uploader for the movie:
        uploader=request.json["uploader"]
        #email=request.json["email"]
        uploader_object = Uploader.query.filter_by(uploader_name=uploader).first()
        
        movie = Movie(
            name=request.json["name"],
            genre=request.json["genre"],
            uploader=uploader_object
        )
        
        try:
            db.session.add(movie)
            db.session.commit()
        except IntegrityError:
            return create_error_response(409, "Already exists", 
                "Movie with name '{}' already exists.".format(request.json["name"])
            )

        return Response(status=201, headers={
            "Location": api.url_for(MovieItem, movie=request.json["name"])
        })
Ejemplo n.º 10
0
    def delete(self, project, member):
        """
        delete member from project
        """
        db_project = Project.query.filter_by(name=project).first()
        if db_project is None:
            return create_error_response(
                404, "Project not found",
                f"Project with name {project} not found")

        db_tasks = Tasks.query.filter_by(project_id=db_project.id)
        if db_tasks is []:
            return create_error_response(404, "not found",
                                         f"no tasks for this project")

        db_member = Members.query.filter_by(name=member).first()
        if db_member is None:
            return create_error_response(404, "not found",
                                         f"member {member} not in database")

        for task in db_tasks:
            db_team = Teams.query.filter_by(task_id=task.id,
                                            member_id=db_member.id).first()
            if db_team is not None:
                db.session.delete(db_team)
                db.session.commit()
                return Response(status=204)
        return create_error_response(
            404, "not found", f"member {member} not in project {project}")
Ejemplo n.º 11
0
    def put(self, id):
        """
        PUT method edits a single message
        """
        db_messageid = Message.query.filter_by(id=id).first()
        if db_messageid is None:
            return create_error_response(
                404, "Not found", "No message was found with id {}".format(id))

        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, BoardBuilder.message_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        db_messageid.id = request.json["id"],
        db_messageid.parent_topic_id = request.json["parent_topic_id"],
        db_messageid.message = request.json["message"],
        db_messageid.time = request.json["time"],
        db_messageid.user_id = request.json["user_id"]

        try:
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Message with id '{}' already exists".format(
                    request.json["id"]))

        return Response(status=204)
Ejemplo n.º 12
0
    def put(self, member):
        """
        edit member
        """

        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, MemberBuilder.member_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        db_member = Members.query.filter_by(name=member).first()
        if db_member is None:
            return create_error_response(
                404, "Not found",
                "No member was found with the name {}".format(member))

        db_member.name = request.json["name"]

        try:
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Member with name '{}' already exists.".format(
                    request.json["name"]))

        return Response(status=204,
                        headers={
                            "location":
                            api.url_for(MemberItem, member=db_member.name)
                        })
Ejemplo n.º 13
0
    def post(self):
        """
        add new meber to database
        """
        # add new member
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, MemberBuilder.member_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        new_member = Members(name=request.json["name"])

        try:
            db.session.add(new_member)
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Member with name '{}' already exists.".format(
                    request.json["name"]))

        return Response(status=201,
                        headers={
                            "Location":
                            api.url_for(MemberItem, member=new_member.name)
                        })
Ejemplo n.º 14
0
    def put(self, uploader_name):
        """Method for editing a single uploader in the database"""
        db_uploader = Uploader.query.filter_by(uploader_name=uploader_name).first()
        if db_uploader is None:
            return create_error_response(404, "Not found", 
                "No uploader was found with the name {}".format(uploader_name)
            )
        
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                "Requests must be JSON"
            )

        try:
            validate(request.json, Uploader.get_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
    
        db_uploader.uploader_name = request.json["uploader_name"]
        db_uploader.email = request.json["email"]
        
        try:
            db.session.commit()
        except IntegrityError:
            return create_error_response(409, "Already exists", 
                "Uploader with name '{}' already exists.".format(request.json["uploader_name"])
            )

        return Response(status=204)
Ejemplo n.º 15
0
    def post(self):
        """
        POST method adds new topic to collection
        """
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Request must be JSON")

        try:
            validate(request.json, BoardBuilder.topic_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        new_topic = Topic(id=request.json["id"],
                          header=request.json["header"],
                          message=request.json["message"],
                          time=request.json["time"],
                          user_id=request.json["user_id"])

        try:
            db.session.add(new_topic)
            db.session.commit()
        except IntegrityError:
            return create_error_response(409, "Already exists",
                                         "Topic already inserted")

        return Response(
            status=201,
            headers={"Location": api.url_for(TopicItem, id=new_topic.id)})
Ejemplo n.º 16
0
    def put(self, project, phase, task):
        """
        Modify existing task.
        """
        db.session.rollback()

        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")
        try:
            validate(request.json, TaskBuilder.task_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        db_task = Tasks.query.filter_by(name=task).first()
        if db_task is None:
            return create_error_response(
                404, "Not found",
                "No phase was found with the name {}".format(task))

        db_task.name = request.json["task_name"]

        try:
            db_task.start = datetime.datetime.strptime(
                request.json["task_start"], "%Y-%m-%d")
        except KeyError:
            pass

        try:
            db_task.end = datetime.datetime.strptime(request.json["task_end"],
                                                     "%Y-%m-%d")
        except KeyError:
            pass

        try:
            db_task.status = request.json["task_status"]
        except KeyError:
            pass

        try:
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Phase with name '{}' already exists.".format(
                    request.json["name"]))

        return Response(status=204,
                        headers={
                            "location":
                            api.url_for(TaskItem,
                                        project=project,
                                        phase=phase,
                                        task=db_task.name)
                        })
Ejemplo n.º 17
0
    def post(self, project):

        #db.session.rollback()
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")
        try:
            validate(request.json, PhaseBuilder.phase_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        try:
            project = Project.query.filter_by(name=project).first()
        except KeyError:
            pass

        new_phase = Phase(
            name=request.json["name"],
            project=project,
        )

        try:
            new_phase.deadline = datetime.datetime.strptime(
                request.json["deadline"], "%Y-%m-%d")
        except KeyError:
            pass

        try:
            new_phase.status = status_type[request.json["status"]]
        except KeyError:
            pass

        if new_phase.deadline != None:
            new_phase.deadline = new_phase.deadline
        try:
            db.session.add(new_phase)
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Phase with name '{}' already exists.".format(
                    request.json["name"]))

        return Response(status=201,
                        headers={
                            "Location":
                            api.url_for(PhaseItem,
                                        project=project.name,
                                        phase=new_phase.name)
                        })
Ejemplo n.º 18
0
    def post(self, project):
        """
        add new member to project
        """
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")
        try:
            validate(request.json, MemberBuilder.member_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))
        # add new member to task
        db_member = Members.query.filter_by(name=request.json["name"]).first()
        db_task = Tasks.query.filter_by(name=f"{project}-default").first()
        db_project = Project.query.filter_by(name=project).first()

        if db_member is None:
            return create_error_response(
                404, "not found",
                f"member {request.json['name']} not in database")
        if db_task is None:
            new_task = Tasks(project=db_project,
                             name=f"{project}-default",
                             status=status_type["NOT_STARTED"])
            db.session.add(new_task)
            db.session.commit()

        db_task = Tasks.query.filter_by(name=f"{project}-default").first()
        db_team = Teams.query.filter_by(task_id=db_task.id,
                                        member_id=db_member.id).first()
        if db_team is not None:
            return create_error_response(
                409, "Already exists",
                "Member already in task '{}'.".format(db_task.name))
        new_team = Teams(team_tasks=db_task, team_members=db_member)

        try:
            db.session.add(new_team)
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Member already in task '{}'.".format(task))
        return Response(status=201,
                        headers={
                            "Location":
                            api.url_for(ProjectMemberItem,
                                        project=project,
                                        member=db_member.name)
                        })
Ejemplo n.º 19
0
    def post(self):
        """
        this function adds new project to the database
        """
        # add new project
        db.session.rollback()
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")
        try:
            validate(request.json, ProjectBuilder.project_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        manager, start, end = None, None, None
        new_project = Project(name=request.json["name"],
                              status=status_type[request.json["status"]])
        try:
            manager = Members.query.filter_by(
                name=request.json["project_manager"]).first()
        except KeyError:
            pass
        try:
            start = request.json["start"]
            new_project.start = datetime.datetime.strptime(start, "%Y-%m-%d")
        except (KeyError, TypeError):
            pass
        try:
            end = request.json["end"]
            new_project.end = datetime.datetime.strptime(end, "%Y-%m-%d")
        except (KeyError, TypeError):
            pass
        if manager is not None:
            new_project.project_manager = manager
        try:
            db.session.add(new_project)
            db.session.commit()
        except IntegrityError:
            return create_error_response(
                409, "Already exists",
                "Project with name '{}' already exists.".format(
                    request.json["name"]))

        return Response(status=201,
                        headers={
                            "location":
                            api.url_for(ProjectItem, project=new_project.name)
                        })
Ejemplo n.º 20
0
def process_games(json_body, is_revenge_games):
    response = {}
    parsed_json = check_for_json_errors(json_body)
    if not parsed_json.get(CONSTANTS.ERROR_KEY):
        leagues = parsed_json[CONSTANTS.LEAGUES_KEY]
        date_object = parsed_json[CONSTANTS.DATE_KEY]
        include_odds = parsed_json.get(CONSTANTS.INCLUDE_ODDS_KEY)
        response[CONSTANTS.DATA_KEY] = {}
        for league in leagues:
            if (utils.validate_league(league)):
                json_logic = get_json_logic(league, leagues)
                if is_revenge_games:
                    number_of_years_back = json_body.get(
                        CONSTANTS.YEARS_BACK_KEY)
                    if not number_of_years_back:
                        number_of_years_back = CONSTANTS.DEFAULT_NUMBER_OF_YEARS_BACK
                    generator = RevengeGameGenerator(league,
                                                     number_of_years_back,
                                                     date_object, json_logic)
                else:
                    generator = GameGenerator(league, date_object, json_logic)
                games = generator.get_games(include_odds)
                response_array = []
                for game in games:
                    response_array.append(game.to_dictionary())
                response[CONSTANTS.DATA_KEY][league] = response_array
            else:
                message = str(league) + " is not a valid league (ex: nhl)"
                response = utils.create_error_response(500, message)
    else:
        response = parsed_json
    return response
Ejemplo n.º 21
0
    def get(self, uploader_name):
        """Method for getting a single uploader from the database"""
        db_uploader = Uploader.query.filter_by(uploader_name=uploader_name).first()
        if db_uploader is None:
            return create_error_response(404, "Not found", 
                "No uploader was found with the name {}".format(uploader_name)
            )
        
        body = MovieBuilder(
            uploader_name=db_uploader.uploader_name,
            email=db_uploader.email,
        )
        body.add_namespace("mwl", LINK_RELATIONS_URL)
        body.add_control("self", api.url_for(UploaderItem, uploader_name=uploader_name))
        body.add_control("uploaders-all", api.url_for(UploaderCollection))
        body.add_control_delete_uploader(uploader_name)
        body.add_control_modify_uploader(uploader_name)
        
        body["items"] = []
        for db_movie in Movie.query.all():

            uploader_object = db_movie.uploader
            if uploader_object is not None:
                uploader_object_name = uploader_object.uploader_name
            else:
                uploader_object_name = "No name"
            if uploader_object_name == uploader_name:
                item = MovieBuilder(
                    name=db_movie.name,
                    genre=db_movie.genre
                )
                item.add_control("self", api.url_for(MovieItem, movie=db_movie.name))
                item.add_control("profile", MOVIE_PROFILE)
                body["items"].append(item)
        return Response(json.dumps(body), 200, mimetype=MASON)
Ejemplo n.º 22
0
    def get(self, project, phase, task):
        db_task = Tasks.query.filter_by(name=task).first()
        if db_task == None:
            return create_error_response(404, "Not found",
                                         f"Task with name {task} not found.")

        if db_task.start != None:
            db_task.start = datetime.datetime.strftime(db_task.start,
                                                       "%Y-%m-%d")

        if db_task.end != None:
            db_task.end = datetime.datetime.strftime(db_task.end, "%Y-%m-%d")

        body = TaskBuilder(name=db_task.name,
                           task_start=str(db_task.start),
                           task_end=str(db_task.end),
                           status=str(db_task.status.value))

        body.add_namespace("promana", LINK_RELATIONS_URL)
        body.add_control(
            "self",
            api.url_for(TaskItem, project=project, phase=phase, task=task))
        body.add_control(
            "collection",
            api.url_for(TaskCollection, project=project, phase=phase))
        body.add_control_task_phase(project, phase)
        body.add_control_task_members(project, phase, task)
        body.add_control_edit_task(project, phase, task)
        body.add_control_delete_task(project, phase, task)

        return Response(json.dumps(body), 200, mimetype=MASON)
Ejemplo n.º 23
0
    def get(self, project, phase):
        db_phase = Phase.query.filter_by(name=phase).first()
        if db_phase == None:
            return create_error_response(
                404, "Not found", f"Phase with name {phase} not found.")

        #if db_phase.deadline is None:
        #    db_phase.deadline = datetime.datetime.strftime(db_phase.deadline, "%Y-%m-%d")
        try:
            deadline = db_phase.deadline.strftime("%Y-%m-%d")
        except AttributeError:
            deadline = None

        body = PhaseBuilder(name=db_phase.name,
                            deadline=deadline,
                            status=str(db_phase.status.value))
        body.add_namespace("promana", LINK_RELATIONS_URL)
        body.add_control("self",
                         api.url_for(PhaseItem, project=project, phase=phase))
        body.add_control("collection",
                         api.url_for(PhaseCollection, project=project))
        body.add_control_up_project(project)
        body.add_control_edit_phase(project, phase)
        body.add_control_delete_phase(project, phase)

        return Response(json.dumps(body), 200, mimetype=MASON)
Ejemplo n.º 24
0
    def post(self):
        """
        POST method adds a new message to collection
        """
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                                         "Requests must be JSON")

        try:
            validate(request.json, BoardBuilder.message_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        new_message = Message(id=request.json["id"],
                              message=request.json["message"],
                              date=request.json["date"],
                              user_id=request.json["user_id"],
                              parent_topic_id=request.json["parent_topic_id"])
        """
Ejemplo n.º 25
0
    def get(self, project, phase, task):
        """
        get all task members
        """
        db_task = Tasks.query.filter_by(name=task).first()
        if db_task is None:
            return create_error_response(404, "Task not found",
                                         f"Task with name {task} not found")

        body = MemberBuilder()
        body.add_namespace("promana", LINK_RELATIONS_URL)
        body.add_control(
            "self",
            api.url_for(TaskMembers, project=project, phase=phase, task=task))
        body.add_control(
            "up", f"/api/projects/{project}/phases/{phase}/tasks/{task}/")
        body.add_control_delete_member("member",
                                       project=project,
                                       phase=phase,
                                       task=task)
        body.add_control_add_member(project=project, phase=phase, task=task)
        body["items"] = []

        db_teams = Teams.query.filter_by(task_id=db_task.id)
        for team in db_teams:
            member = team.team_members
            try:
                if member.name not in body["items"]:
                    item = MemberBuilder(name=member.name)
                    item.add_control(
                        "self",
                        api.url_for(TaskMemberItem,
                                    project=project,
                                    phase=phase,
                                    task=task,
                                    member=member.name))
                    body["items"].append(item)
            except AttributeError:
                return create_error_response(404, "not found",
                                             "user not found")

        return Response(json.dumps(body), 200, mimetype=MASON)
Ejemplo n.º 26
0
    def post(self):
        """Method for POST a new single uploader to the database"""
        if not request.json:
            return create_error_response(415, "Unsupported media type",
                "Requests must be JSON"
            )

        try:
            validate(request.json, Uploader.get_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid JSON document", str(e))

        uploader = Uploader(
            uploader_name=request.json["uploader_name"],
            email=request.json["email"],
        )
        
        exists = Uploader.query.filter_by(uploader_name=uploader.uploader_name).first()
        
        
        
        print("\nposting: " + str(exists))
        print("\nposting: " + str(uploader.uploader_name))
        
        try:
            db.session.add(uploader)
            
            if exists == None:
                db.session.commit()
            else:
                print("\nNothing to add, uploader already exists.")
                db.session.rollback()
            
        except IntegrityError:
            return create_error_response(409, "Already exists", 
                "Uploader with name '{}' already exists.".format(request.json["uploader_name"])
            )

        return Response(status=201, headers={
            "Location": api.url_for(UploaderItem, uploader_name=request.json["uploader_name"])
        })
Ejemplo n.º 27
0
    def delete(self, movie):
        """Method for deleting a movie from the database with movie name"""
        db_movie = Movie.query.filter_by(name=movie).first()
        if db_movie is None:
            return create_error_response(404, "Not found", 
                "No movie was found with the name {}".format(movie)
            )

        db.session.delete(db_movie)
        db.session.commit()

        return Response(status=204)
Ejemplo n.º 28
0
    def delete(self, uploader_name):
        """Method for deleting a uploader from the database"""
        db_uploader = Uploader.query.filter_by(uploader_name=uploader_name).first()
        if db_uploader is None:
            return create_error_response(404, "Not found", 
                "No Uploader was found with the name {}".format(uploader_name)
            )

        db.session.delete(db_uploader)
        db.session.commit()

        return Response(status=204)        
Ejemplo n.º 29
0
    def delete(self, id):
        """
        DELETE method deletes a single message
        """
        db_messageid = Throw.query.filter_by(id=id).first()
        if db_messageid is None:
            return create_error_response(
                404, "Not found", "No message was found with id {}".format(id))

        db.session.delete(db_messageid)
        db.session.commit()

        return Response(status=204)
Ejemplo n.º 30
0
    def get(self, project):
        """
        get all project members
        """
        db_project = Project.query.filter_by(name=project).first()
        if db_project is None:
            return create_error_response(
                404, "Project not found",
                f"Project with name {project} not found")

        body = MemberBuilder()
        body.add_namespace("promana", LINK_RELATIONS_URL)
        body.add_control("self", api.url_for(ProjectMembers, project=project))
        body.add_control("up", f"/api/projects/{project}/")
        body.add_control_delete_member("member", project=project)
        body.add_control_add_member(project=project)
        body["items"] = []

        db_tasks = Tasks.query.filter_by(project_id=db_project.id)

        for task in db_tasks:
            db_teams = Teams.query.filter_by(team_tasks=task)
            for team in db_teams:
                member = team.team_members
                if member is not None:
                    try:
                        if member.name not in body["items"]:
                            item = MemberBuilder(name=member.name)
                            item.add_control(
                                "self",
                                api.url_for(ProjectMemberItem,
                                            project=project,
                                            member=member.name))
                            body["items"].append(item)
                    except AttributeError:
                        return create_error_response(404, "not found",
                                                     "user not found")

        return Response(json.dumps(body), 200, mimetype=MASON)