Ejemplo n.º 1
0
 def add_single_card_to_collection(self,
                                   single_card=Card(),
                                   card_inside=None) -> bool:
     if single_card not in self.instance.cards:
         single_card.cardsCollection = self.instance
         if card_inside is not None:
             single_card.cardInside = card_inside
         Session.add_and_commit(single_card)
         return True
     return False
Ejemplo n.º 2
0
    def add_new_user(username: str, email: str, password_hash: str):
        user = User()
        user.userName = username
        user.userEmail = email
        user.userPasswordHash = password_hash

        Session.add_and_commit(user)
        user_context = UserContext(user)

        return user_context
Ejemplo n.º 3
0
 def __init__(self, collection=None, holder_instance=None):
     super(CardsCollectionContext, self).__init__()
     if collection is None:
         if holder_instance is not None:
             collection = CardsCollection()
             collection.user = holder_instance
             Session.add_and_commit(collection)
         else:
             logging.error("Can't create object without a ref")
     self.instance = collection
Ejemplo n.º 4
0
 def __init__(self, collection, card=None, card_inside=None):
     super(CardContext, self).__init__()
     if card is None:
         if card_inside is None:
             card = Card()
             card_inside = CardInside()
             card.cardsCollection = collection
         card.cardInside = card_inside.json()
         Session.add_and_commit(card)
     self.instance = card
Ejemplo n.º 5
0
    def get_user_collections_json(user_id, page: int, size: int):
        data = Session.query(CardsCollection) \
            .filter(CardsCollection.holderID == user_id) \
            .paginate(page=page, per_page=size, max_per_page=size)

        res = {"total": data.pages, "items": []}
        for obj in data.items:
            res["items"].append(obj.json())
        return res
Ejemplo n.º 6
0
    def get_all_collections_json(page: int, size: int):
        data = Session.query(CardsCollection).paginate(page=page,
                                                       per_page=size,
                                                       max_per_page=size)

        res = {"total": data.pages, "items": []}
        for obj in data.items:
            res["items"].append(obj.json())
        return res
Ejemplo n.º 7
0
    def get_all_collections_with_field_liked(user_id, page: int, size: int):
        data = Session.query(CardsCollection).paginate(page=page,
                                                       per_page=size)

        list_of_liked = []
        liked = CardsCollectionContext.get_user_liked_collections(user_id)
        for collection in liked:
            list_of_liked.append(collection.collectionID)

        res = {"total": data.pages, "items": []}
        for obj in data.items:
            obj_json = obj.json()
            if obj.collectionID in list_of_liked:
                obj_json["is_liked"] = True
            else:
                obj_json['is_liked'] = False

            res["items"].append(obj_json)
        return res
Ejemplo n.º 8
0
 def get_single_card_by_id(card_id: int) -> Union[CardContext, bool]:
     card = Session.query(Card) \
         .filter(Card.cardID == card_id) \
         .first()
     return CardContext(card.collectionID, card) \
         if card is not None else False
Ejemplo n.º 9
0
 def get_only_watched_cards_of_collection(self, user_id):
     return Session.query(Card).join(CardWatched) \
         .filter(Card.collectionID == self.instance.collectionID,
                 CardWatched.userID == user_id).all()
Ejemplo n.º 10
0
 def get_user_instance_by_id(uid: int):
     user = Session.query(User).filter(User.userID == uid).first()
     return UserContext(user) if user else None
Ejemplo n.º 11
0
 def exists_by_username(user_name: str) -> bool:
     return Session.query(User).filter(User.userName == user_name).first() is not None
Ejemplo n.º 12
0
 def set_user_info(self, info: str):
     self.instance.userInfo = info
     Session.commit()
Ejemplo n.º 13
0
 def set_name(self, collection_name: str):
     self.instance.collectionName = collection_name
     Session.commit()
Ejemplo n.º 14
0
 def set_description(self, description: str):
     self.instance.collectionDescription = description
     Session.commit()
Ejemplo n.º 15
0
 def delete_from_user_favorite(user_id, collection_id):
     user_liked = Session.query(UserLikedCollection).filter(
         UserLikedCollection.collectionID == collection_id).filter(
             UserLikedCollection.userID == user_id).first()
     Session.del_and_commit(user_liked)
Ejemplo n.º 16
0
 def add_to_user_favorite(user_id, collection_id):
     user_liked_collection = UserLikedCollection()
     user_liked_collection.collectionID = collection_id
     user_liked_collection.userID = user_id
     Session.add_and_commit(user_liked_collection)
Ejemplo n.º 17
0
 def delete_collection(collection_id):
     Session.del_and_commit(
         CardsCollectionContext.get_collection_by_id(collection_id))
     return True
Ejemplo n.º 18
0
 def get_collection_by_id(collection_id: int):
     collection = Session.query(CardsCollection) \
         .filter(CardsCollection.collectionID == collection_id) \
         .first()
     return collection
Ejemplo n.º 19
0
 def set_username(self, username: str):
     self.instance.userName = username
     Session.commit()
Ejemplo n.º 20
0
 def get_collection_by_id_json(collection_id):
     collection = Session.query(CardsCollection) \
         .filter(CardsCollection.collectionID == collection_id) \
         .first()
     return collection.json() or None
Ejemplo n.º 21
0
 def set_password_hash(self, new_password_hash: str):
     self.instance.userPasswordHash = new_password_hash
     Session.commit()
Ejemplo n.º 22
0
 def get_user_instance_by_email(email: str):
     user = Session.query(User).filter(User.userEmail == email).first()
     return UserContext(user) if user else None
Ejemplo n.º 23
0
 def exists_by_email(email: str) -> bool:
     return Session.query(User).filter(User.userEmail == email).first() is not None
Ejemplo n.º 24
0
 def add_card_to_watched(self, user: User):
     if user in User.query.all():
         card_watched = CardWatched()
         card_watched.cardID = self.instance.cardID
         card_watched.userID = user.userID
         Session.add_and_commit(card_watched)
Ejemplo n.º 25
0
 def get_user_instance_by_username(username: str):
     user = Session.query(User).filter(User.userName == username).first()
     return UserContext(user) if user else None
Ejemplo n.º 26
0
 def get_card_by_id(card_id):
     return Session.query(Card).filter(Card.cardID == card_id).first()
Ejemplo n.º 27
0
 def delete_single_card_from_collection(single_card_id):
     Session.del_and_commit(CardContext.get_card_by_id(single_card_id))
Ejemplo n.º 28
0
 def change_card_inside(self, new_inside: dict) -> bool:
     if new_inside:
         self.instance.cardInside = new_inside
         Session.commit()
     return new_inside is not None