def shortenurl(): body = request.get_json() long_url = None brand = None cleaned_url = None # The URL we want to shorten try: long_url = body['long_url'] except KeyError: return error_response(400, 'Please provide a URL!') except: return error_response(500, "We encountered a problem.") # Optional custom branded link try: brand = body['brand'] except: pass if brand is not None: maybe_existing_url = db.shortened_urls.find_one({"short_code": brand}) if maybe_existing_url is not None: error_response(409, 'That brand is already taken!') try: cleaned_url = clean_url(long_url) except ValueError, e: return error_response(400, 'Please provide a valid URL.')
def getlinks(user_id): if get_user_id(request) != user_id: return error_response(403, "You aren't allowed to access that link!") # TODO: try: query = db.shortened_urls.find({"user_id": user_id}) clicks = [click for click in query] except Exception, e: print e.message return error_response(500, "We encountered an problem!")
def getclicks(link_id): user_id = get_user_id(request) # Make sure they own this link link = db.shortened_urls.find_one({"_id": ObjectId(link_id)}) if link is None: return error_response(404, "That link could not be found.") elif link["user_id"] != user_id: return error_response(403, "You are not allowed to access that link.") # TODO: Make sure they belong to the user making the request! try: clicksquery = db.clicks.find({"link_id": link_id}) clicks = [click for click in clicksquery] except Exception, e: return error_response(500, "We encountered a problem.")
def link_stats(link_id): user_id = get_user_id(request) link = db.shortened_urls.find_one({"_id": ObjectId(link_id)}) if link is None: return error_response(404, 'That link could not be found.') if link["user_id"] != user_id: return error_response(403, "You aren't allowed to access that linke!") results = {} statsquery = db.linkstats.find({"link_id": link_id}) stats = [stat for stat in statsquery] for stat in stats: results[stat['type']] = stat return BSONEncoder().encode(results)
def user_stats(user_id): actual_user_id = get_user_id(request) if user_id != actual_user_id: return error_response(403, "You don't have permission to view that user's stats!") results = {} statsquery = db.userstats.find({ "user_id": actual_user_id }) stats = [stat for stat in statsquery] for stat in stats: results[stat['type']] = clean(stat) return BSONEncoder().encode(results)
def signup(): body = request.get_json() username = None password = None email = None try: username = body['username'] password = body['password'] email = body['email'] except: return error_response(400, 'Please fill out all required fields.') # TODO: Make sure email address is valid # Make sure username isnt taken try: existingusername = db.users.find_one({'username': username}) except Exception, e: print e
try: brand = body['brand'] except: pass if brand is not None: maybe_existing_url = db.shortened_urls.find_one({"short_code": brand}) if maybe_existing_url is not None: error_response(409, 'That brand is already taken!') try: cleaned_url = clean_url(long_url) except ValueError, e: return error_response(400, 'Please provide a valid URL.') except Exception, e: return error_response(500, "We encountered a problem.") latest_code_record = db.latest_short_code.find_one( {'code': { '$exists': True }}) last_code = latest_code_record[ "code"] if latest_code_record is not None else None next_short_code = next_code(last_code) # Make sure we havent generated a short code that # someone has already used as a custom brand while db.shortened_urls.find_one({"short_code": next_short_code}) is not None: next_short_code = next_code(next_short_code)
try: username = body['username'] password = body['password'] email = body['email'] except: return error_response(400, 'Please fill out all required fields.') # TODO: Make sure email address is valid # Make sure username isnt taken try: existingusername = db.users.find_one({'username': username}) except Exception, e: print e if existingusername is not None: return error_response(409, 'That username is already taken ;(') try: existingemail = db.users.find_one({'email': email}) except Exception, e: print e if existingemail is not None: return error_response( 409, "An account is aleady registered for this email address. Contact the Dev to ask him to reset your password or build a 'Forgot password feature'." ) # TODO: create user as pending and send confirmation email pwhash = hashlib.sha512(password + PASSWORD_SALT).hexdigest()