Esempio n. 1
0
def delete_link(db: Session, url: all_schemas.UrlRecovery):

    db.delete(url)
    db.commit()

    log_api.info("deleted information -> " + str(url.__dict__))

    return {
        "Short URL":
        "{short_url} with ID {id} has been deleted succesfully".format(
            short_url=url.short_url, id=url.id)
    }
Esempio n. 2
0
def create_new_url(db: Session, url: all_schemas.UrlCreate):

    short_url_generate = generate_short_url(db, url.original_url)

    db_url = Entity.Url(original_url=url.original_url,
                        short_url=short_url_generate)
    db.add(db_url)
    db.commit()
    db.refresh(db_url)

    log_api.info("New URL data created -> " + str(url))
    return db_url
Esempio n. 3
0
def update_url(db: Session, url: all_schemas.UrlRecovery):

    print("sssss ", url)
    db.query(Entity.Url).filter(Entity.Url.short_url == url.short_url).update(
        {"clicks": url.clicks + 1}, synchronize_session="fetch")
    db.commit()

    log_api.info("There is a new click in the Short URL -> " +
                 str(url.short_url) +
                 " now the URL has {clicks} clicks".format(clicks=url.clicks))
    return db.query(
        Entity.Url).filter(Entity.Url.short_url == url.short_url).first()
Esempio n. 4
0
def generate_short_url(db, original_url):

    url_base = original_url.split("/", 3)
    url_base = url_base[0] + "//" + url_base[2] + "/"

    letters = string.ascii_letters + string.digits
    short_url_generated = url_base + ''.join(
        random.choice(letters) for i in range(5))
    data = True

    while (data):

        if not get_url_by_short_url(db, short_url_generated):
            data = False

    log_api.info("New Short URL was created -> " + str(short_url_generated) +
                 " from original URL -> " + str(original_url))
    return short_url_generated
Esempio n. 5
0
def get_url_by_original_url(db: Session, original_url: str):

    log_api.info("Validate Original URL " + str(original_url))
    return db.query(
        Entity.Url).filter(Entity.Url.original_url == original_url).first()
Esempio n. 6
0
def get_url_by_short_url(db: Session, short_url: str):

    log_api.info("Validate Short URL " + str(short_url))
    return db.query(
        Entity.Url).filter(Entity.Url.short_url == short_url).first()
Esempio n. 7
0
def get_url_by_id(db: Session, id: int):

    log_api.info("Getting URL by ID " + str(id))
    return db.query(Entity.Url).filter(Entity.Url.id == id).first()
Esempio n. 8
0
def get_url_list(db: Session, skip: int = 0, limit: int = 5):

    log_api.info("Getting response for Data list")
    return db.query(Entity.Url).order_by(
        Entity.Url.updated_at.desc()).offset(skip).limit(limit).all()
Esempio n. 9
0
def encoding_url(short_url):

    log_api.info("Encoding Url " + str(short_url))
    return short_url.replace("/", ENCODING_DECODING_WORD)
Esempio n. 10
0
def decoding_url(short_url):
    log_api.info("Decoding Url " + str(short_url))
    return short_url.replace(ENCODING_DECODING_WORD, "/")