def become_creator(): # Get user from session user = User.get_from_db(session.get("USERNAME")) user_id = user.get('_id') User.update_to_creator(user.get('username')) # Get information about the future creator result = request.get_json().get('result') # Tuple with user's information values = (None, user_id, result.get('country_of_residence'), result.get('country_for_shipping'), result.get('full_name'), result.get('address'), result.get('suite'), result.get('city'), result.get('state'), result.get('postal_code'), result.get('phone_number'), result.get('facebook'), result.get('twitter'), result.get('instagram'), result.get('webtoon'), result.get('twitch'), result.get('youtube'), result.get('bio'), result.get('working_on'), None, None) Info(*values).update() # Tuple with user's creator specific information values = (None, user_id, None, None, result.get('content_type')) CreatorSpecific(*values).create() info_log.info("%s became a creator" % session.get("USERNAME")) return jsonify(success=True, message="%s became a creator" % session.get("USERNAME"))
def user_profile(username): searchedUser = User.get_from_db(username) searchedUser = json.loads(json_util.dumps(searchedUser)) searchedUser_id = searchedUser.get('_id').get('$oid') activeUser = User.get_from_db(session.get("USERNAME")) activeUser = json.loads(json_util.dumps(activeUser)) activeUser_id = activeUser.get('_id').get('$oid') if searchedUser.get('role') == "creator": # Get info is user is creator info = Info.find_by_user_id(searchedUser_id) info = json.loads(json_util.dumps(info)) tiers = Tier.find_all_by_user_id(searchedUser_id) tiers = json.loads(json_util.dumps(tiers)) tiers = sorted(tiers, key=lambda tier: int(tier['price'])) posts = Post.find_posts_by_user_id(searchedUser_id) posts = json.loads(json_util.dumps(posts)) posts = sorted(posts, key=lambda post: post['date']['$date'], reverse=True) surveys = Survey.find_surveys_by_creator_id(searchedUser_id) surveys = json.loads(json_util.dumps(surveys)) surveys = sorted(surveys, key=lambda survey: survey['date']['$date'], reverse=True) giveaways = Giveaway.find_giveaways_by_creator_id(searchedUser_id) giveaways = json.loads(json_util.dumps(giveaways)) giveaways = sorted(giveaways, key=lambda giveaway: giveaway['date']['$date'], reverse=True) if Info.is_patreon(activeUser_id, searchedUser_id): # Get tier id of chosen tier tier_id = Info.get_tier_id(activeUser_id, searchedUser_id) else: tier_id = -1 return jsonify(success=True, user=searchedUser, info=info, tiers=tiers, tier_id=str(tier_id), posts=posts, surveys=surveys, giveaways=giveaways) return jsonify(success=True, user=searchedUser)
def get_usernames(): # Get current input state value = request.get_json().get("value") usernames = User.get_searched_usernames(value, session.get("USERNAME")) return jsonify(success=True, usernames=usernames)
def is_following(): # Get user from session user = User.get_from_db(session.get("USERNAME")) user_id = user.get('_id') # Get information about the profile to be followed result = request.get_json().get('result') profile_name = result.get('profile_name') user_to_follow = User.get_from_db(profile_name) profile_id = user_to_follow.get('_id') # Update in DB if Info.is_following(user_id, profile_id): return jsonify(following=True) return jsonify(following=False)
def check_creator(): # Check if the current user is creator if session.get("LOGGED_IN"): if User.get_from_db(session.get("USERNAME")).get("role") == "creator": return jsonify(is_creator = True) return jsonify(is_creator = False)
def login(): # Get information about user and try to find him username = request.json.get("username") password = request.json.get("password") user = User.find_by_username(username) # Validate user if not user or not user.verify_password(password): error_log.error("Login failed!") return jsonify( success=False, message="Incorrect username or password"), 403 # forbidden # Check if user is verified if user.verified == False: error_log.error("Login failed!") return jsonify(success=False, message="Account is not verified!"), 403 # forbidden # Update session session["LOGGED_IN"] = True session["USERNAME"] = username info_log.info("%s logged in successfully" % username) return jsonify(success=True, message="%s logged in successfully" % username)
def unfollow(): # Get user from session user = User.get_from_db(session.get("USERNAME")) user_id = user.get('_id') # Get information about the profile to be followed result = request.get_json().get('result') profile_name = result.get('profile_name') user_to_follow = User.get_from_db(profile_name) profile_id = user_to_follow.get('_id') # Update in DB Info.unfollow(user_id,profile_id) # Log info_log.info("%s stopped following %s" % (user.get('username'), profile_name)) return jsonify(success=True, message="%s stopped following %s" % (user.get('username'), profile_name))
def test_04_unsubscribe_to_tier_success(self): creator_id = User.get_from_db( '_FpCerpd9Z7SIbjmN81Jy_test_profile').get('_id') tier_id = Tier.find_by_name(creator_id, 'Test Tier 1').get('_id') response = self.unsubscribe(tier_id, creator_id) self.assertEqual(response.status_code, 200) self.assertIn( b'_FpCerpd9Z7SIbjmN81Jy_test_profile2 unsubscribed from tier Test Tier 1', response.data)
def create_giveaway(): # Get user from session username = session.get("USERNAME") user = User.get_from_db(username) user_id = user.get('_id') # Get image from request image = request.files["image"] # Check image type description = request.args.get("description") required_id = request.args.get("required_id") # Get time of post creation now = datetime.datetime.now() # Validate image if allowed_image(image.filename): values = ( None, user_id, None, now, # date now.strftime("%m-%d-%Y-%H-%M-%S"), # image_path description, required_id, True, None, # deadline None) Giveaway(*values).create() # Define path where the image will go ("public/images/{user"s username}/) path = os.path.join(upload_folder, username) # Name for the picture filename = now # Check if path exists and create one if it doesn"t if not os.path.exists(path): os.makedirs(path) # Save image image.save(os.path.join(path, now.strftime("%m-%d-%Y-%H-%M-%S"))) info_log.info("%s added a new giveaway" % username) return jsonify(success=True, message="%s added a new giveaway" % username) else: error_log.error("Image extension is not allowed or doesn't exist!") return jsonify( success=False, message="Allowed extensions: 'pdf', 'png', 'jpeg', 'jpg', 'gif'" ), 403
def check_username(): # Get username from request username = request.get_json().get('username') # If user with that username exists, return duplication error user = User.find_by_username(username) if user: return jsonify(success=False, message='Username already taken!') return jsonify(success=True, message='Username %s is not taken' % username)
def check_email(): # Get email from request email = request.get_json().get('email') # If user with that email exists, return duplication error user = User.find_by_email(email) if user: return jsonify(success=False, message='Email already used!') return jsonify(success=True, message='Email %s is not taken' % email)
def create_survey(): # Get user from session username = session.get("USERNAME") user = User.get_from_db(username) user_id = user.get('_id') # Get image from request image = request.files["image"] # Get information about the future survey result = json.loads(request.form["data"]) # Get time of post creation now = datetime.datetime.now() # Validate image if allowed_image(image.filename): # Tuple with user's information values = ( None, #_id user_id, None, #votes now, #date now.strftime("%m-%d-%Y-%H-%M-%S"), # image_path result.get('description'), result.get('required_id'), result.get('options'), None, #open/closed None, #deadline None #winner ) Survey(*values).create() # Define path where the image will go ("public/images/{user"s username}/) path = os.path.join(upload_folder, username) # Name for the picture filename = now # Check if path exists and create one if it doesn"t if not os.path.exists(path): os.makedirs(path) # Save image image.save(os.path.join(path, now.strftime("%m-%d-%Y-%H-%M-%S"))) info_log.info("%s added a new survey" % username) return jsonify(success=True, message='Successfully created a new survey!') else: error_log.error("Image extension is not allowed or doesn't exist!") return jsonify(success=False, message="Allowed extensions: 'pdf', 'png', 'jpeg', 'jpg', 'gif'"), 403
def register(): # Get information about user username = request.get_json().get("username") password = request.get_json().get("password") name = request.get_json().get("name") email = request.get_json().get("email") # Put information about user in a tuple values = (None, username, User.hash_password(password), name, email, None, False) try: # Create user and update session User(*values).create() user_id = User.get_from_db(username).get('_id') Info.create(user_id) info_log.info("User %s registered successfully." % username) if main.app.config['TESTING'] == False: msg = Message('Testing Email Verification', recipients=[email], html=render_template('activation_email.html', username=username, host=FLASK_RUN_HOST, port=FLASK_RUN_PORT)) main.mail.send(msg) info_log.info("Sent activation email to %s" % username) return jsonify(success=True, message="Registration successful") except pymongo.errors.DuplicateKeyError as e: # Catch pymongo exception key = list(e.details.get('keyValue').keys())[0] value = e.details.get('keyValue').get(key) error_log.error("Duplicate %s: %s" % (key, value)) return jsonify(success=False, message="Duplicate %s: %s" % (key, value)), 403 # forbidden
def can_view_giveaway(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the post giveaway_id = request.get_json().get('giveaway_id') if Giveaway.can_view(activeUser_id, giveaway_id): return jsonify(view=True) return jsonify(view=False)
def has_voted_on_survey(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the survey survey_id = request.get_json().get("survey_id") if Survey.has_voted(activeUser_id, survey_id): return jsonify(voted=True) return jsonify(voted=False)
def has_joined_giveaway(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the giveaway giveaway_id = request.get_json().get('giveaway_id') if Giveaway.has_joined(activeUser_id, giveaway_id): return jsonify(joined=True) return jsonify(joined=False)
def can_view_post(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the post post_id = request.get_json().get("post_id") if Post.can_view(activeUser_id, post_id): return jsonify(view=True) return jsonify(view=False)
def get_winning_option(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the survey survey_id = request.get_json().get("survey_id") Survey.get_wining_option(survey_id) win = Survey.get_wining_option(survey_id) info_log.info("Successfully option %s won survey with id: %s" % (activeUser_id, survey_id)) return jsonify(success=True, message='Successfully anounced winning option!', winner = win)
def join_giveaway(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the giveaway giveaway_id = request.get_json().get('giveaway_id') Giveaway.join_giveaway(activeUser_id, giveaway_id) info_log.info("%s joined giveaway with id: %s." % (session.get("USERNAME"), giveaway_id)) return jsonify(success=True, message='Successfully joined giveaway!')
def check_tier_name(): # Get current user user_id = User.get_from_db(session.get("USERNAME")).get('_id') # Get tier name from request tier_name = request.get_json().get('tier_name') # If tier with that such name exists, return duplication error tier = Tier.find_by_name(user_id, tier_name) if tier: return jsonify(success=False, message='Tier name already used!') return jsonify(success=True, message='Tier name %s is not taken' % tier_name)
def vote_on_survey(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the survey survey_id = request.get_json().get("survey_id") option_id = request.get_json().get("option_id") Survey.vote(activeUser_id, survey_id, option_id) info_log.info("%s voted on survey with id: %s." % (session.get("USERNAME"), survey_id)) return jsonify(success=True, message='Successfully voted on survey!')
def choose_winner(): # Get information about the survey winner_id = request.get_json().get('winner_id') giveaway_id = request.get_json().get('giveaway_id') winner = User.find_by_id(winner_id) Giveaway.choose_winner(giveaway_id, winner.get('username')) info_log.info("Successfully announced winner %s on giveaway with id: %s" % (winner.get('username'), giveaway_id)) return jsonify(success=True, message='Successfully announced winner!', winner=winner.get('username'))
def edit_profile(): # Returned message message = "" # Get user from session username = session.get("USERNAME") # Get user"s new email and password email = request.get_json().get("email") password = request.get_json().get("password") # Change email if email: User.change_email(username, email) info_log.info("Changed email for %s" % username) message += "Changed email for %s " % username # Change password if password: User.change_password(username, password) info_log.info("Changed password for %s" % username) message += "Changed password for %s " % username return jsonify(success=True, message=message)
def like_post(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the post post_id = request.get_json().get('post_id') Post.add_like(activeUser_id, post_id) info_log.info("%s liked post with id: %s." % (session.get("USERNAME"), post_id)) return jsonify(success=True, message='%s liked post with id: %s.' % (session.get("USERNAME"), post_id))
def my_profile(): print(session.get("USERNAME")) if session.get("LOGGED_IN"): # Get user from session and information about the user from the database user = User.get_from_db(session.get("USERNAME")) user = json.loads(json_util.dumps(user)) if user.get('role') == "creator": info = Info.find_by_user_id(user.get("_id").get("$oid")) info = json.loads(json_util.dumps(info)) tiers = Tier.find_all_by_user_id(user.get("_id").get("$oid")) tiers = json.loads(json_util.dumps(tiers)) tiers = sorted(tiers, key=lambda tier: int(tier['price'])) posts = Post.find_posts_by_user_id(user.get("_id").get("$oid")) posts = json.loads(json_util.dumps(posts)) posts = sorted(posts, key=lambda post: post['date']['$date'], reverse=True) surveys = Survey.find_surveys_by_creator_id( user.get("_id").get("$oid")) surveys = json.loads(json_util.dumps(surveys)) surveys = sorted(surveys, key=lambda survey: survey['date']['$date'], reverse=True) giveaways = Giveaway.find_giveaways_by_creator_id( user.get('_id').get('$oid')) giveaways = json.loads(json_util.dumps(giveaways)) giveaways = sorted(giveaways, key=lambda giveaway: giveaway['date']['$date'], reverse=True) return jsonify(success=True, user=user, info=info, tiers=tiers, posts=posts, surveys=surveys, giveaways=giveaways) return jsonify(success=True, user=user) return jsonify(success=False)
def test_03_subscribe_to_tier_success(self): # User self.register('_FpCerpd9Z7SIbjmN81Jy_test_profile2', '12345678', 'Ognian Baruh', '*****@*****.**') self.verify('_FpCerpd9Z7SIbjmN81Jy_test_profile2') self.login('_FpCerpd9Z7SIbjmN81Jy_test_profile2', '12345678') creator_id = User.get_from_db( '_FpCerpd9Z7SIbjmN81Jy_test_profile').get('_id') tier_id = Tier.find_by_name(creator_id, 'Test Tier 1').get('_id') response = self.subscribe(tier_id, creator_id) self.assertEqual(response.status_code, 200) self.assertIn( b'_FpCerpd9Z7SIbjmN81Jy_test_profile2 subscribed to tier Test Tier 1', response.data)
def unsubscribe_from_tier(): # Get user from session activeUser = User.get_from_db(session.get("USERNAME")) activeUser_id = activeUser.get('_id') # Get information about the future creator result = request.get_json().get('result') Info.unsubscribe_from_tier(activeUser_id, result.get('creator_id'), result.get('tier_id')) tier_name = Tier.find_by_id(result.get('tier_id')).get('name') info_log.info("%s unsubscribed from tier %s" % (session.get("USERNAME"), tier_name)) return jsonify(success=True, message="%s unsubscribed from tier %s" % (session.get("USERNAME"), tier_name))
def create_tier(): # Get user from session user = User.get_from_db(session.get("USERNAME")) user_id = user.get('_id') # Get information about the future creator result = request.get_json().get('result') # Tuple with user's information values = ( None, user_id, result.get('benefits'), result.get('price'), result.get('name'), ) Tier(*values).create() info_log.info("%s added a new tier named %s" % (session.get("USERNAME"), result.get('name'))) return jsonify(success=True, message="%s added a new tier named %s" % (session.get("USERNAME"), result.get('name')))
def get_user_id(self, username): user = User.get_from_db(username) return user.get('_id')
def verify_account(username): User.verify_account(username) return redirect(CLIENT_URL + "/login")