Example #1
0
 def post(self, film_id):
     if not film_id:
         return fill_return_packet(0, "Il manque un paramètre", None)
     uuid = check_auth_token(request)
     if not uuid:
         return fill_return_packet(0, "Token invalide", None)
     packet = request.json
     if not packet["comment"]:
         return fill_return_packet(0, "Ce commentaire n'est pas bon", None)
     uuid_binary = userH.getUUIDBinaryFromStr(uuid)
     userInfos = userH.getUserInformations(user_uuid=uuid_binary)
     if not userInfos:
         return fill_return_packet(0, "Cet utilisateur n'existe pas", None)
     result = db.insert(
         "INSERT INTO comments (fk_users, fk_films, comment) VALUES (%s, %s, %s)",
         userInfos["id"], film_id, packet["comment"])
     if not result:
         return fill_return_packet(0, "Une erreur est survenue", None)
     result = db.request("SELECT * FROM comments WHERE fk_films = %s",
                         film_id)
     for comment in result:
         userInfos = db.request("SELECT username FROM users WHERE id = %s",
                                comment["fk_users"])
         if not userInfos:
             pass
         else:
             comment["username"] = userInfos[0]['username']
     return fill_return_packet(1, "OK", result)
Example #2
0
 def get(self, page, genre):
     if not page or not genre:
         return fill_return_packet(0,
                                   "Une page et un genre sont nécessaire",
                                   None)
     uuid = check_auth_token(request)
     if not uuid:
         return fill_return_packet(0, "Token Invalide", None)
     uuid_binary = userH.getUUIDBinaryFromStr(uuid)
     user = userH.getUserInformations(user_uuid=uuid_binary)
     if not user:
         return fill_return_packet(0, "Ce compte n'existe pas", None)
     start = int(page) * self.filmsPerPage
     if not start:
         return fill_return_packet(0, "Une erreur est survenue", None)
     if user["age"] < 18:
         result = db.request(
             "SELECT t1.id, t1.title, t1.release_date, t1.image FROM films AS t1 INNER JOIN films_genres AS t2 WHERE t1.adult = 0 AND t2.fk_films = t1.id AND t2.fk_genres LIKE '%%"
             + str(genre) + "%%' LIMIT " + str(start) + "," +
             str(self.filmsPerPage))
     else:
         result = db.request(
             "SELECT t1.id, t1.title, t1.release_date, t1.image FROM films AS t1 INNER JOIN films_genres AS t2 WHERE t2.fk_films = t1.id AND t2.fk_genres LIKE '%%"
             + str(genre) + "%%' LIMIT " + str(start) + "," +
             str(self.filmsPerPage))
     if not result:
         return fill_return_packet(0, "Pas de film trouvé pour ce genre",
                                   None)
     return fill_return_packet(1, "OK", result)
Example #3
0
 def patch(self, film_id):
     if not film_id:
         return fill_return_packet(0, "Il manque un paramètre", None)
     uuid = check_auth_token(request)
     if not uuid:
         return fill_return_packet(0, "Token invalide", None)
     packet = request.json
     if not packet["comment"] or packet["id_comment"]:
         return fill_return_packet(0, "Ce commentaire n'est pas bon", None)
     uuid_binary = userH.getUUIDBinaryFromStr(uuid)
     userInfos = userH.getUserInformations(user_uuid=uuid_binary)
     if not userInfos:
         return fill_return_packet(0, "Cet utilisateur n'existe pas", None)
     original_comment = db.request(
         "SELECT * FROM comments WHERE id = %s AND fk_users = %s AND fk_films = %s",
         packet["id_comment"], userInfos["id"], film_id)
     if not original_comment:
         return fill_return_packet(0, "Ce commentaire n'existe pas", None)
     db.request("UPDATE comments SET comment = %s WHERE id = %s",
                packet["comment"], packet["id_comment"])
     result = db.request("SELECT * FROM comments WHERE fk_films = %s",
                         film_id)
     for comment in result:
         userInfos = db.request("SELECT username FROM users WHERE id = %s",
                                comment["fk_users"])
         if not userInfos:
             pass
         else:
             comment["username"] = userInfos['username']
     return fill_return_packet(1, "OK", result)
Example #4
0
 def patch(self, film_id, note):
     if not film_id or not note:
         return fill_return_packet(
             0, "Un des parametres est manquant ou invalide", None)
     user_uuid = check_auth_token(request)
     if not user_uuid:
         return fill_return_packet(0, "Invalid Token", None)
     uuid_binary = userH.getUUIDBinaryFromStr(user_uuid)
     user = userH.getUserInformations(user_uuid=uuid_binary)
     if not user:
         return fill_return_packet(0, "Compte inexistant", None)
     checker = db.request(
         "SELECT * FROM users_ratings WHERE fk_users=%s AND fk_films=%s",
         user['id'], film_id)
     if checker:
         db.request("UPDATE users_ratings SET rating=%s WHERE id=%s", note,
                    checker[0]["id"])
     else:
         db.insert(
             "INSERT INTO users_ratings (fk_users, fk_films, rating) VALUES (%s, %s, %s)",
             user['id'], film_id, note)
     if not checker:
         return fill_return_packet(0,
                                   "L'ajout des données a la DB a échoué",
                                   None)
     if (adjust_weight_user(film_id, note, int(user['id'])) == False):
         return fill_return_packet(0, "L'ajustement des poids a échoué",
                                   None)
     return fill_return_packet(1, "OK", None)
Example #5
0
 def get(self):
     user_uuid = check_auth_token(request)
     if not user_uuid:
         return fill_return_packet(0, "Cet utilisateur n'existe pas", None)
     user_binary = userH.getUUIDBinaryFromStr(user_uuid)
     userInfos = userH.getUserInformations(user_uuid=user_binary)
     if not userInfos:
         return fill_return_packet(0, "Cet utilisateur n'existe pas", None)
     filmsSeen = userH.getUserFilmsSeen(userInfos['id'])
     return fill_return_packet(1, "OK", filmsSeen)
Example #6
0
    def get(self):
        user_uuid = check_auth_token(request)
        id_list = []
        weight_list = []
        tag_list = []
        temp_list = []
        list_str = ""
        to_select = ""
        list_suggestions = []
        good_suggestions = ""
        if not user_uuid:
            return fill_return_packet(0, "Invalid Token", None)
        uuid_binary = userH.getUUIDBinaryFromStr(user_uuid)
        user = userH.getUserInformations(user_uuid=uuid_binary)
        if not user:
            return fill_return_packet(0, "Compte inexistant",
                                      self.data_information)
        result = db.request(
            "SELECT fk_genres, weight from users_genres WHERE fk_users=%s",
            user['id'])
        if not result:
            return fill_return_packet(
                0, "Pas de préférences pour cette utilisateur",
                self.data_information)
        for i in range(len(result)):
            id_list.append(result[i]['fk_genres'])
            weight_list.append(result[i]['weight'])
        average = make_average_weight(weight_list)
        for i in range(len(weight_list)):
            if weight_list[i] >= average:
                tag_list.append(id_list[i])
        print(tag_list)
        to_select = "'%%" + str(tag_list[0]) + "%%'"
        cmd = "SELECT fk_films FROM films_genres WHERE fk_genres LIKE " + to_select
        result = db.request(cmd)
        for i in range(10):
            selected = random.randint(1, len(result))
            list_suggestions.append(result[selected])

        for i in range(len(list_suggestions)):
            good_suggestions = good_suggestions + str(
                list_suggestions[i]['fk_films']) + ","
        good_suggestions = good_suggestions[:-1]
        result = db.request("SELECT * from films WHERE id IN(" +
                            good_suggestions + ")")
        if not result:
            return fill_return_packet(0, "Aucune propositions trouvées", None)
        for i in range(len(result)):
            del result[i]['id_tmdb']
        return fill_return_packet(1, "OK", result)
Example #7
0
 def delete(self, film_id):
     if not film_id:
         return fill_return_packet(0, "Il manque un paramètre", None)
     uuid = check_auth_token(request)
     if not uuid:
         return fill_return_packet(0, "Token invalide", None)
     if not film_id:
         return fill_return_packet(0, "Ce commentaire n'existe pas", None)
     uuid_binary = userH.getUUIDBinaryFromStr(uuid)
     userInfos = userH.getUserInformations(user_uuid=uuid_binary)
     if not userInfos:
         return fill_return_packet(0, "Cet utilisateur n'existe pas", None)
     db.request("DELETE FROM comments WHERE id = %s", film_id)
     return fill_return_packet(1, "OK", None)
Example #8
0
 def get(self):
     uuid = check_auth_token(request)
     if not uuid:
         return fill_return_packet(0, "Invalid Token", None)
     uuid_binary = userH.getUUIDBinaryFromStr(uuid)
     user = userH.getUserInformations(user_uuid=uuid_binary)
     if not user:
         return fill_return_packet(0, "Ce compte n'existe pas", None)
     user['genres'] = userH.getUserGenres(user['id'])
     user['uuid'] = uuid
     del user['id']
     self.data_information['userInfos'] = user
     self.data_information['JWT'] = encode_auth_token(uuid)
     return fill_return_packet(1, "OK", self.data_information)
Example #9
0
 def get(self, film_id):
     if not film_id:
         return fill_return_packet(0, "Un ID de film est nécessaire", None)
     uuid = check_auth_token(request)
     if not uuid:
         return fill_return_packet(0, "Token Invalide", None)
     uuid_binary = userH.getUUIDBinaryFromStr(uuid)
     user = userH.getUserInformations(user_uuid=uuid_binary)
     if not user:
         return fill_return_packet(0, "Ce compte n'existe pas", None)
     result = db.request("SELECT * from films where id=%s", film_id)
     if not result:
         return fill_return_packet(0, "Pas de film trouvé a l'id demandé",
                                   None)
     if result[0]["adult"] and user["age"] < 18:
         return fill_return_packet(0, "Film adulte", None)
     return fill_return_packet(1, "OK", result)
Example #10
0
 def get(self, film_id, note=None):
     if not film_id:
         return fill_return_packet(
             0, "Un des parametres est manquant ou invalide", None)
     user_uuid = check_auth_token(request)
     if not user_uuid:
         return fill_return_packet(0, "Invalid Token", None)
     uuid_binary = userH.getUUIDBinaryFromStr(user_uuid)
     user = userH.getUserInformations(user_uuid=uuid_binary)
     if not user:
         return fill_return_packet(0, "Compte inexistant", None)
     result = db.request(
         "SELECT * FROM users_ratings WHERE fk_users=%s AND fk_films=%s",
         user['id'], film_id)
     if not result:
         return fill_return_packet(
             0, "L'utilisateur n'a aucune note sur le film", None)
     return fill_return_packet(1, "OK", result)
Example #11
0
 def post(self):
     packet = request.json
     if not packet or 'id' not in packet:
         return fill_return_packet(0, "La requête est incorrecte", None)
     user_uuid = check_auth_token(request)
     if not user_uuid:
         return fill_return_packet(0, "Cet utilisateur n'existe pas", None)
     user_binary = userH.getUUIDBinaryFromStr(user_uuid)
     userInfos = userH.getUserInformations(user_uuid=user_binary)
     if not userInfos:
         return fill_return_packet(0,
                                   "Pas de données sur cette utilisateur",
                                   None)
     result = db.insert(
         "INSERT INTO users_films (fk_users, fk_films) VALUES (%s, %s)",
         userInfos['id'], packet['id'])
     if not result:
         return fill_return_packet(0, "KO", None)
     return fill_return_packet(1, "OK", None)
Example #12
0
 def get(self, page):
     if not page:
         return fill_return_packet(0, "Un numéro de page est nécessaire",
                                   None)
     uuid = check_auth_token(request)
     if not uuid:
         return fill_return_packet(0, "Token Invalide", None)
     uuid_binary = userH.getUUIDBinaryFromStr(uuid)
     user = userH.getUserInformations(user_uuid=uuid_binary)
     if not user:
         return fill_return_packet(0, "Ce compte n'existe pas", None)
     start = int(page) * self.filmsPerPage
     if user["age"] < 18:
         result = db.request(
             "SELECT id, title, release_date, image from films LIMIT %s, %s WHERE adult = 0",
             start, self.filmsPerPage)
     else:
         result = db.request(
             "SELECT id, title, release_date, image from films LIMIT %s, %s",
             start, self.filmsPerPage)
     if not result:
         return fill_return_packet(0, "Pas de film trouvé a l'id demandé",
                                   None)
     return fill_return_packet(1, "OK", result)
Example #13
0
 def patch(self):
     uuid = check_auth_token(request)
     if not uuid:
         return fill_return_packet(0, "Token Invalide", None)
     uuid_binary = userH.getUUIDBinaryFromStr(uuid)
     user = userH.getUserInformations(user_uuid=uuid_binary)
     if not user:
         return fill_return_packet(0, "Ce compte n'existe pas", None)
     packet = request.json
     if not packet:
         return fill_return_packet(0, "Aucune information à mettre à jour",
                                   None)
     password = userH.hashPassword(
         packet["password"]) if packet["password"] else None
     age = packet["age"] if packet["age"] else user["age"]
     user = userH.updateUserInfos(user["id"], user["username"], password,
                                  age)
     if not user:
         return fill_return_packet(0, "Une erreur est survenue", None)
     user["uuid"] = userH.getUUIDstrFromBinary(user["uuid"])
     self.data_informations["JWT"] = encode_auth_token(user["uuid"])
     del user["id"]
     self.data_informations["userInfos"] = user
     return fill_return_packet(1, None, self.data_informations)