Exemple #1
0
    def all_giphies(cls, users_id: "int") -> "List[Giphies]":
        """ Returns user's giphies
        Params:
            user_id (int): User ID

        Returns List[Giphies]
        """
        giphies: "List[Dict]" = cls.query.filter_by(users_id=users_id).all()
        logger.info(f"Giphies list {giphies}")
        return giphies
Exemple #2
0
    def all_tags(cls, giphies_id: "int") -> "List[Tags]":
        """ All tags retrieved for Giphy
        Params:
            giphies_id (int): Giphy ID

        Returns List[Tags]
        """
        tags: "List[Tags]" = cls.query.filter_by(giphies_id=giphies_id).all()
        logger.info(f"Tags found: {tags}")
        return tags
Exemple #3
0
    def url(self, endpoint: "str") -> "str":
        """ Builds URL
        Params:
            endpoint (str): The endpoint we want to execute against

        Returns str
        """
        host: "str" = "/".join(
            [config.giphy_host, config.giphy_version, endpoint])
        full_url: "str" = f"{config.giphy_schema}://{host}"
        logger.info(f"URL is {full_url}")
        return full_url
Exemple #4
0
    def first_giphy(cls, users_id: "int", giphy: "str") -> "Giphies":
        """ Grabs the giphy for the user
        Params:
            user_id (int): User's ID
            giphy (str): The giphy ID provided by GIPHY

        Returns Giphies
        """
        giphy: "Giphies" = cls.query.filter_by(users_id=users_id,
                                               giphy=giphy).first()
        success = True if giphy is not None else False
        logger.info(f"User's Giphy: {giphy}")
        return {"success": success, "giphy": giphy}
Exemple #5
0
    def save_giphy(cls, users_id: int, giphy: str) -> "Dict":
        """ Save Giphy for user
        Params:
            user_id (int): User's ID
            giphy (str): The giphy ID provided by GIPHY

        Returns Dict
        """
        try:
            giphy = Giphies(users_id=users_id, giphy=giphy)
            db_session.add(giphy)
            db_session.commit()
            logger.info("Saved giphy")
            return {"success": True, "msg": "Saved Giphy", "giphy": giphy}
        except Exception as e:
            return {
                "success": False,
                "msg": f"Could not save {giphy} for {users_id}: {e}",
            }
Exemple #6
0
    def save_tag(cls, giphies_id: "int", tag: "str") -> "Dict":
        """ Save tag
        Params:
            giphies_id (int): Giphy ID
            tag (str): The tag

        Returns Dict
        """
        try:
            new_tag = Tags(**{"giphies_id": giphies_id, "tag": tag})
            db_session.add(new_tag)
            db_session.commit()
            logger.info("Saved giphy")
            return {
                "success": True,
                "msg": f"Saved tag {tag} for giphy id {giphies_id}",
            }
        except Exception as e:
            return {
                "success": False,
                "msg": f"Could not save {tag} for {giphies_id}: {e}",
            }
Exemple #7
0
    def register(cls, email: "str", name: "str", password: "******") -> "Dict":
        """ Registers a user
        Params:
            email (str): User's email
            name (str): User's name
            password (str): User's password

        Returns Dict
        """
        try:
            user: "******" = Users(email=email, password=password, name=name)
            db_session.add(user)
            db_session.commit()
            logger.info(f"User registered")
            new_user: "******" = cls.query.filter_by(email=email).first()
            return {
                "success": True,
                "msg": "User Registered",
                "user": new_user
            }
        except Exception as e:
            msg: "str" = f"Could not register {e}"
            logger.error(msg)
            return {"success": False, "msg": msg}
Exemple #8
0
 def to_dict(self) -> "Dict":
     """ Converts model to dict """
     result = dict(id=self.id, email=self.email, name=self.name)
     logger.info(f"User Dict {result}")
     return result
Exemple #9
0
 def to_dict(self):
     """ Converts model to dict """
     result = dict(id=self.id, giphies_id=self.giphies_id, tag=self.tag)
     logger.info(f"Giphy Dict {result}")
     return result
Exemple #10
0
 def to_dict(self) -> "Dict":
     """ Converts model to dict """
     result = dict(id=self.id, users_id=self.users_id, giphy=self.giphy)
     logger.info(f"Giphy Dict {result}")
     return result
Exemple #11
0
def drop_db():
    """ Drops the database """
    import save_the_giphies.database.models  # noqa: ignore=F401

    logger.info("Dropping DB")
    Base.metadata.drop_all(bind=engine)
Exemple #12
0
def init_db():
    """ Initiallizes the database """
    import save_the_giphies.database.models  # noqa: ignore=F401

    logger.info("Initializing DB")
    Base.metadata.create_all(bind=engine)