def sanitize_fields(form): """ Display errors for the following: 1. Strip whitespaces from all the fields 2. Remove @ from the HackerEarth 3. Lowercase the handles 4. Fill the institute field with "Other" if empty 5. Email address entered is from a valid domain 6. Email address instead of handles 7. Spoj follows a specific convention for handle naming 8. stopstalk_handle is alphanumeric 9. Country field is compulsory 10. Only positive ints allowed in Timus field 11. HackerRank handle should not be containing hr_r=1 @param form (FORM): Registration / Add Custom friend form """ from re import match if form.vars.stopstalk_handle: # 8. if not utilities.is_valid_stopstalk_handle(form.vars.stopstalk_handle): form.errors.stopstalk_handle = T( "Expected alphanumeric (Underscore allowed)") def _remove_at_symbol(site_name): if site_name in current.SITES: field = site_name.lower() + "_handle" if form.vars[field] and form.vars[field][0] == "@": form.errors[field] = T("@ symbol not required") def _valid_spoj_handle(handle): try: return match("[a-z]+[0-9a-z_]*", handle).group() == handle except AttributeError: return False handle_fields = ["stopstalk"] handle_fields.extend([x.lower() for x in current.SITES.keys()]) # 1, 6 and 11 for field in handle_fields: field_handle = field + "_handle" if form.vars[field_handle]: if field != "uva" and form.vars[field_handle].__contains__(" "): form.errors[field_handle] = T("White spaces not allowed") elif IS_EMAIL(error_message="check")( form.vars[field_handle])[1] != "check": form.errors[field_handle] = T( "Email address instead of handle") elif field == "hackerrank" and form.vars[ field_handle].__contains__("hr_r=1"): form.errors[field_handle] = T("Please enter only the handle") # 2. _remove_at_symbol("HackerEarth") # 7. if "Spoj" in current.SITES: if form.vars["spoj_handle"] and \ not _valid_spoj_handle(form.vars["spoj_handle"]): form.errors["spoj_handle"] = T( "Handle should only contain lower case letters 'a'-'z', underscores '_', digits '0'-'9', and must start with a letter!" ) # 3. for site in handle_fields: site_handle = site + "_handle" if site in ["hackerrank", "uva", "stopstalk", "atcoder"]: continue if form.vars[site_handle] and \ form.vars[site_handle] != form.vars[site_handle].lower(): form.vars[site_handle] = form.vars[site_handle].lower() # 4. if form.vars.institute == "": form.errors.institute = T("Please select an institute or Other") # 9. if form.vars.country == "": form.errors.country = T("Country required") # 5. if form.vars.email: if validate_email(form.vars.email) is False: form.errors.email = T("Invalid email address") # 10. if form.vars.timus_handle: try: timus_id = int(form.vars.timus_handle) if timus_id <= 0: form.errors.timus_handle = "Timus handle / ID should be a number" except ValueError: form.errors.timus_handle = "Timus handle / ID should be a number" if form.errors: response.flash = T("Form has errors")
def add_custom_friend(): """ Add an already existing custom user to some other user """ post_vars = request.post_vars atable = db.auth_user cftable = db.custom_friend stopstalk_handle = "cus_" + post_vars["stopstalk_handle"] # Modify (Prepare) this dictionary for inserting it again current_row = json.loads(post_vars["row"]) original_handle = current_row["stopstalk_handle"] if not utilities.is_valid_stopstalk_handle(original_handle): # @ToDo: Change the message when cus_ is the reason of invalid handle session.flash = T("Expected alphanumeric (Underscore allowed)") redirect(URL("user", "profile", args=original_handle)) return stopstalk_handles = [] rows = db(atable).select(atable.stopstalk_handle) rows = [x.stopstalk_handle for x in rows] stopstalk_handles.extend(rows) rows = db(cftable).select(cftable.stopstalk_handle) rows = [x.stopstalk_handle for x in rows] stopstalk_handles.extend(rows) for temp_handle in stopstalk_handles: if stopstalk_handle.lower() == temp_handle.lower(): session.flash = T("Handle already taken") redirect(URL("user", "profile", args=original_handle)) # The total referrals by the logged-in user query = (atable.referrer == session.handle) # User should not enter his/her own # stopstalk handle as referrer handle query &= (atable.stopstalk_handle != session.handle) total_referrals = db(query).count() # Retrieve the total allowed custom users from auth_user table query = (atable.id == session.user_id) row = db(query).select(atable.referrer, atable.allowed_cu).first() default_allowed = row.allowed_cu referrer = 0 # If a valid referral is applied then award 1 extra CU if row.referrer and row.referrer != session.handle: referrer = db(atable.stopstalk_handle == row.referrer).count() # 3 custom friends allowed plus one for each 3 invites allowed_custom_friends = total_referrals / 3 + default_allowed + referrer # Custom users already created current_count = db(db.custom_friend.user_id == session.user_id).count() if current_count >= allowed_custom_friends: session.flash = T("Sorry!! All custom users used up!") redirect(URL("user", "profile", args=original_handle)) # ID of the custom user original_id = long(current_row["id"]) # Delete this id as this will be incremented # automatically on insert del current_row["id"] # Set duplicate_cu for this new user current_row["duplicate_cu"] = original_id # Set the user_id as current logged in user current_row["user_id"] = session.user_id # Update the stopstalk handle current_row["stopstalk_handle"] = stopstalk_handle # Insert a new Custom friend for the logged-in user custom_user_id = cftable.insert(**current_row) current.create_next_retrieval_record(cftable(custom_user_id), True) session.flash = T("Custom user added!!") redirect(URL("user", "profile", args=stopstalk_handle))