Esempio n. 1
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('username', type=str, help='Username to create user')
        parser.add_argument('email', type=str, help='Email address to create user')
        parser.add_argument('pseudo', type=str, help='Pseudo to create user')
        parser.add_argument('password', type=str, help='Password to create user')
        args = parser.parse_args()

        _userUsername = args['username']
        _userEmail = args['email']
        _userPseudo = args['pseudo']
        _userPassword = args['password']
        stack = str(error.tchek_username(_userUsername)) + str(error.tchek_email(_userEmail)) + str(
            error.tchek_password(_userPassword))
        logging.info("We are here, create user \n")
        if _userUsername and _userEmail and _userPassword is not None:
            if stack != "":
                logging.info(stack)
                return error.badRequest(10034, stack)
            new_user = mod.User(_userUsername, _userEmail, _userPseudo, _userPassword)
            mod.db.session.add(new_user)
            mod.db.session.commit()
            result = user_schema.dump(new_user).data
            return make_response(jsonify({'Message': 'OK', 'data': result}), 201)
        else:
            return error.badRequest(10001, "Veuillez remplir tous les champs obligatoire!")
Esempio n. 2
0
    def put(self, video_id):
        # TODO: enable ou pas à faire
        if error.ifId_video(video_id) is False:
            return error.notFound()
        token_head = include.header()
        if error.ifToken(token_head) is True:
            if include.get_user_id_by_token(token_head) != include.get_user_id_by_video_id(video_id):
                return error.forbidden()
            parser = reqparse.RequestParser()
            parser.add_argument('name', type=str, help='Name of the video')
            parser.add_argument('enable', type=int, action='store_true', help='The video could be watched')
            args = parser.parse_args()

            logging.info("Print ARGS :: {} \n\n".format(args))

            _name = args['name']
            _enable = args['enable']

            if _name is not None and _enable is not None:
                video = mod.Video.query.get(video_id)
                if video is not None:
                    logging.info("TYPE OF :: {} \n\n".format(_enable))
                    logging.info("TYPE OF :: {} \n\n".format(type(_enable)))
                    video.name = _name
                    video.enabled = _enable
                    logging.info("TYPE OF :: {} \n\n".format(video.enabled))
                    mod.db.session.commit()
                    return self.get(video_id)
                else:
                    return error.badRequest(10005, "erreur dans le traitement de la requête")
            else:
                return error.badRequest(10001, "Merci de compléter les champs obligatoires")
        else:
            return error.unauthorized()
Esempio n. 3
0
    def post(self, video_id):
        if error.ifId_video(video_id) is False:
            return error.notFound()
        token_head = include.header()
        if error.ifToken(token_head) is True:
            parser = reqparse.RequestParser()
            parser.add_argument('body',
                                type=str,
                                help='Body to create comment')
            args = parser.parse_args()

            _commentBody = args['body']

            _userId = include.get_user_id_by_token(token_head)
            if _commentBody is not None and _commentBody is not "":
                new_comment = mod.Comment(_commentBody, _userId, video_id)
                mod.db.session.add(new_comment)
                mod.db.session.commit()
                result = comment_schema.dump(new_comment).data
                return make_response(CommentById.get(self, str(result['id'])),
                                     201)
            else:
                return error.badRequest(
                    10001, "Veuillez remplir tous les champs obligatoire!")
        else:
            return error.unauthorized()
Esempio n. 4
0
def get_username_by_id(user_id):
    name = User.query.get(user_id)
    if name is not None:
        data = UserSchema().dump(name).data
        return data['username']
    else:
        return error.badRequest(10001, "Impossible de récupérer l'username !")
Esempio n. 5
0
    def post(self):
        global directly_id
        auth = reqparse.RequestParser()
        auth.add_argument('username', type=str, required=True)
        auth.add_argument('password', type=str, required=True)
        args = auth.parse_args()

        usern = args['username']
        passwd = args['password']
        directly_id = str(include.authen(usern, passwd))
        if directly_id != "0" and error.tchek_token_expiration(
                directly_id) == True:
            user_token = include.create_token()
            include.add_token(user_token, directly_id)
        else:
            user_token = include.get_token_by_user(directly_id)
        data = include.get_user_by_id(directly_id)
        if data != False:
            return make_response(
                jsonify({
                    "Message": 'OK',
                    'data': {
                        'token': user_token,
                        'user': data
                    }
                }), 201)
        else:
            return error.badRequest(10002,
                                    "Identifiant ou mot de passe invalide !")
Esempio n. 6
0
 def put(self, user_id):
     if error.ifId(user_id) is False:
        return error.notFound()
     token_head = include.header()
     if error.ifToken(token_head) is True:
         if include.get_user_id_by_token(token_head) != int(user_id):
             return error.forbidden()
         parser = reqparse.RequestParser()
         parser.add_argument('username', type=str, help='Username to update user')
         parser.add_argument('email', type=str, help='Email address to update user')
         parser.add_argument('pseudo', type=str, help='Pseudo to update user')
         parser.add_argument('password', type=str, help='Password to update user')
         args = parser.parse_args()
         _userUsername = args['username']
         _userEmail = args['email']
         _userPseudo = args['pseudo']
         _userPassword = args['password']
         data = include.get_user_by_id(user_id)
         user = mod.User.query.get(user_id)
         stack = error.tchek_form_put_user(_userPassword, _userEmail, _userUsername, data['username'], data['email'] )
         if stack  != "":
             return error.badRequest(10034, stack)
         else:
             if _userUsername != "":
                 user.username = _userUsername
             if _userEmail != "":
                 user.email = _userEmail
             if _userPassword != "":
                 include.send_email("password", user.email, user.username)
                 user.password = _userPassword
             if _userPseudo != "":
                 user.pseudo = _userPseudo
         if user is not None:
             logging.info("Info logging: USER is NOT NONE")
             mod.db.session.commit()
             return self.get(user_id)
         else:
             logging.info("Info logging: User is NONE")
             return error.badRequest(10005, "erreur dans le traitement de la requête")
     else:
         return error.unauthorized()
Esempio n. 7
0
 def get(self, user_id):
     if error.ifId(user_id) is False:
         return error.notFound()
     token_head = include.header()
     if include.get_user_id_by_token(token_head) == int(user_id):
         getUser = mod.User.query.get(user_id)
         data = user_schema.dump(getUser).data
     else:
         data = include.get_user_by_id(user_id)
     if data != False:
         return make_response(jsonify({'Message': 'OK', 'data': data}))
     else:
         return error.badRequest(10005, "erreur dans le traitement de la requête")
Esempio n. 8
0
 def delete(self, video_id):
     if error.ifId_video(video_id) is False:
         return error.notFound()
     token_head = include.header()
     if error.ifToken(token_head) is True:
         if include.get_user_id_by_token(token_head) != include.get_user_id_by_video_id(video_id):
             return error.forbidden()
         include.delete_com_form_by_video_id(video_id)
         video = mod.Video.query.get(video_id)
         if video is not None:
             mod.db.session.delete(video)
             mod.db.session.commit()
             return {}, 204
         else:
             return error.badRequest(10005, "erreur dans le traitement de la requête")
     else:
         return error.unauthorized()
Esempio n. 9
0
    def delete(self, user_id):
        if error.ifId(user_id) is False:
           return error.notFound()
        token_head = include.header()

        logging.info("\n\nUSER_ID_token :: {} \n".format(include.get_user_id_by_token(token_head)))

        if error.ifToken(token_head) is True:
            if include.get_user_id_by_token(token_head) != int(user_id):
               return error.forbidden()
            include.delete_all_by_user_id(user_id)
            include.delete_token(user_id)
            user = mod.User.query.get(user_id)
            if user is not None:
                mod.db.session.delete(user)
                mod.db.session.commit()
                return {}, 204
            else:
                return error.badRequest(10005, "erreur dans le traitement de la requête")
        else:
            logging.info("TOKEN HEAD :: {}\n".format(token_head))
            logging.info("ifToken :: {}\n".format(error.ifToken(token_head)))
            return error.unauthorized()
Esempio n. 10
0
    def get(self, video_id):
        parser = reqparse.RequestParser()
        parser.add_argument('page', type=str)
        parser.add_argument('perPage', type=str)
        args = parser.parse_args()

        _page = int(args['page']) if args['page'] and args[
            'page'] is not "0" and args['page'].isdigit() else 1
        _perPage = int(args['perPage']) if args['perPage'] and args[
            'perPage'] is not "0" and args['perPage'].isdigit() else 50

        start_index = _perPage * _page - _perPage

        list = []

        all_comments = mod.Comment.query.filter_by(video_id=video_id).all()
        if all_comments is not None:
            result = comments_schema.dump(all_comments)
            result = result.data[start_index:start_index + _perPage]
            for row in result:
                listTmp = {
                    'id': row['id'],
                    'body': row['body'],
                    'user': include.get_user_by_id(row['user_id'])
                }
                list.append(listTmp)
            return make_response(
                jsonify({
                    'Message ': 'OK',
                    'data': list,
                    'pager': {
                        'current': _page,
                        'total': len(result)
                    }
                }))
        else:
            return error.badRequest(10005)
Esempio n. 11
0
    def post(self, user_id):
        # TODO: Filename de la video à gerer
        if error.ifId(user_id) is False:
            return error.notFound()
        token_head = include.header()
        if error.ifToken(token_head) is True:
            id_user = include.get_user_id_by_token(token_head)

            """ user_id is received as str, so we need to cast it in int """
            if id_user != int(user_id):
                return error.forbidden()
            parser = reqparse.RequestParser()
            parser.add_argument('name', type=str, help='name of the video')
            parser.add_argument('source', type=str, help='file of the video')
            args = parser.parse_args()

            uploader = upload.Upload()

            _name = args['name']
            _source = uploader.upload_file()

            header = {'Authorization':token_head}

            # Got the response from the encoder by rabbit
            rabb = rabbitSender.SenderClient()
            response = rabb.call(_source)

            if _name and _source is not None and _name and _source is not "":
                new_video = mod.Video(_name, 300, id_user, _source, 0, 1)
                mod.db.session.add(new_video)
                mod.db.session.commit()
                result = video_schema.dump(new_video).data
            
                # Setting all paths to video_format
                tmp_path = _source.split('.')[0]
                output_path = ''

                probe = ffmpeg.probe(_source)
                height = 0
                definition = [1080, 720, 480, 360, 240]            
                for metadata in probe['streams']:
                    for (key, value) in metadata.items():
                        if key == "height":
                            height = value

                logging.info("\n\nprinting source :: {} \t P {}\n\n".format(tmp_path, _source))

                startIndex = 0

                #Récupère l'index de départ par rapport à la vidéo input
                for i in range(len(definition)):
                    if definition[i] == height:
                        startIndex = i
                        break


                for value in range(startIndex, len(definition)):
                    file_format = str(definition[value])
                    output_path = tmp_path+"_"+file_format+'p.mp4'
                    logging.info("PRINTING OUTPUT FILE :: {}\t FILE FORMAT :: {} \n\n".format(output_path, file_format))
                    # Redirect to the patch route to encode video
                    patch_response = requests.patch("http://localhost:5000/video/{}".format(result['id']), data = {'file':output_path, 'format':file_format}, headers=header)
                    if patch_response.status_code != 201:
                        return error.badRequest(10010, "Something went wrong")

                return make_response(jsonify({'Message': 'OK', 'data': result}), 201)
            else:
                return error.badRequest(10001, "Veuillez remplir tous les champs obligatoire!")
        else:
            return error.unauthorized()