Example #1
0
def long_url_exists(long_url):
    """Pass a long link, check and return if link exists or not.

    :param long_url: str
    :return: bool
    """
    link_manager = LinkManager()
    link = link_manager.find(long_url=long_url)
    if link is None:
        return False
    if link.is_disabled:
        return False
    return True
Example #2
0
def get_links(user_id=None):
    """Get all links that belong to user `user_id`"""
    # TODO: get auth required from settings and get user links by id

    manager = LinkManager()
    schema = LinkSchema()
    if request.method == 'GET':
        user_email = APITokenAuth.get_jwt_identity()
        if not user_email:
            return jsonify(dict(error='Invalid/expired token passed')), 400
        user = UserManager().get_by_email(email=user_email)
        if not user:
            return jsonify(dict(error='Invalid/expired token passed')), 400
        links = manager.get_by_owner(owner_id=user.id)
        if not links:
            return jsonify([]), 200
        result = schema.dump(links, many=True)
        return jsonify(result.data)
Example #3
0
def next_short_code():
    """Pass a long link and it returns the next available
    base62 short code.
    :return: str
    """
    link_manager = LinkManager()
    link = link_manager.latest_default_link()
    # First link
    if link is None:
        base_id = 1
        base_str = HashDigest().shorten(base_id)
    else:
        base_id = HashDigest().decode(link.short_code) + 1
        base_str = HashDigest().shorten(base_id)
        while link_manager.find(short_code=base_str):
            base_id += 1
            base_str = HashDigest().shorten(base_id)
    return base_str
Example #4
0
class LongUrlApi(MethodView):
    """View for handeling long url operations."""
    schema = LinkSchema()
    manager = LinkManager()

    def get(self):
        """Return data if long url already exists."""
        long_url = request.args.get('url')
        is_valid = validate_url(long_url)
        if is_valid is False:
            return jsonify(dict(error='Invalid URL.')), 400
        link = self.manager.get(long_url)
        if self.manager.has_expired():
            return jsonify(dict(error="Link has expired")), 404
        if link is None:
            abort(404)
        result = self.schema.dump(link)
        return jsonify(result.data), 200

    @APITokenAuth.token_optional
    def post(self):
        payload = request.get_json()
        data, errors = self.schema.load(payload)

        if errors:
            log.error('Error in the request payload %s', errors)
            if errors.get('long_url'):
                errors.update({'error': errors.get('long_url')})
            return jsonify(errors), 400

        # if authenticated request check valid user
        user_email = APITokenAuth.get_jwt_identity()
        if user_email:
            user = UserManager().find(email=user_email)
            if not user:
                return jsonify(dict(error='Invalid user')), 400
            data['owner'] = user.id

        long_url = data.pop('long_url')
        log.info('Shortening url %s', long_url)
        link = self.manager.get(long_url)
        if link is None or (data.get('is_custom') or data.get('is_protected')
                            or data.get('expire_after')):
            try:
                link = self.manager.add(long_url, **data)
            except ShortURLUnavailable as e:
                return jsonify(dict(error=str(e))), 400
        result = self.schema.dump(link)
        log.info('Url: %s shortened, response: %s', long_url,
                 result.data.get('short_code'))
        return jsonify(result.data), 201