def select_userinfo(userid, config=None): if config is None: [query] = d.engine.execute(""" SELECT pr.config, ui.birthday, ui.gender, ui.country FROM profile pr INNER JOIN userinfo ui USING (userid) WHERE pr.userid = %(userid)s """, userid=userid) else: [query] = d.engine.execute(""" SELECT %(config)s, birthday, gender, country FROM userinfo WHERE userid = %(userid)s """, userid=userid, config=config) user_links = d.engine.execute(""" SELECT link_type, ARRAY_AGG(link_value) FROM user_links WHERE userid = %(userid)s GROUP BY link_type """, userid=userid).fetchall() show_age = "b" in query[0] or d.get_userid() in staff.MODS return { "birthday": query[1], "age": d.convert_age(query[1]) if show_age else None, "show_age": "b" in query[0], "gender": query[2], "country": query[3], "user_links": {r[0]: r[1] for r in user_links}, "sorted_user_links": sort_user_links(user_links), }
def select_userinfo(userid, config): query = d.engine.execute(""" SELECT birthday, gender, country FROM userinfo WHERE userid = %(userid)s """, userid=userid).first() user_links = d.engine.execute(""" SELECT link_type, ARRAY_AGG(link_value ORDER BY link_value) FROM user_links WHERE userid = %(userid)s GROUP BY link_type """, userid=userid).fetchall() show_age = "b" in config or d.get_userid() in staff.MODS return { "birthday": query.birthday, "age": d.convert_age(query.birthday) if show_age else None, "show_age": "b" in config, "gender": query.gender, "country": query.country, "user_links": {r[0]: r[1] for r in user_links}, "sorted_user_links": sort_user_links(user_links), }
def do_manage(my_userid, userid, username=None, full_name=None, catchphrase=None, birthday=None, gender=None, country=None, remove_social=None, permission_tag=None): """Updates a user's information from the admin user management page. After updating the user it records all the changes into the mod notes. If an argument is None it will not be updated. Args: my_userid (int): ID of user making changes to other user. userid (int): ID of user to modify. username (str): New username for user. Defaults to None. full_name (str): New full name for user. Defaults to None. catchphrase (str): New catchphrase for user. Defaults to None. birthday (str): New birthday for user, in format for convert_inputdate. Defaults to None. gender (str): New gender for user. Defaults to None. country (str): New country for user. Defaults to None. remove_social (list): Items to remove from the user's social/contact links. Defaults to None. permission_tag (bool): New tagging permission for user. Defaults to None. Returns: Does not return. """ updates = [] # Username if username is not None: if not d.get_sysname(username): raise WeasylError("usernameInvalid") elif d.execute( "SELECT EXISTS (SELECT 0 FROM login WHERE login_name = '%s')", [d.get_sysname(username)], ["bool"]): raise WeasylError("usernameExists") elif d.execute( "SELECT EXISTS (SELECT 0 FROM useralias WHERE alias_name = '%s')", [d.get_sysname(username)], ["bool"]): raise WeasylError("usernameExists") elif d.execute( "SELECT EXISTS (SELECT 0 FROM logincreate WHERE login_name = '%s')", [d.get_sysname(username)], ["bool"]): raise WeasylError("usernameExists") d.execute("UPDATE login SET login_name = '%s' WHERE userid = %i", [d.get_sysname(username), userid]) d._get_display_name.invalidate(userid) d.execute("UPDATE profile SET username = '******' WHERE userid = %i", [username, userid]) updates.append('- Username: %s' % (username, )) # Full name if full_name is not None: d.execute("UPDATE profile SET full_name = '%s' WHERE userid = %i", [full_name, userid]) updates.append('- Full name: %s' % (full_name, )) # Catchphrase if catchphrase is not None: d.execute("UPDATE profile SET catchphrase = '%s' WHERE userid = %i", [catchphrase, userid]) updates.append('- Catchphrase: %s' % (catchphrase, )) # Birthday if birthday is not None and d.convert_inputdate(birthday): unixtime = d.convert_inputdate(birthday) age = d.convert_age(unixtime) d.execute("UPDATE userinfo SET birthday = %i WHERE userid = %i", [unixtime, userid]) if age < ratings.EXPLICIT.minimum_age: max_rating = ratings.GENERAL.code rating_flag = "" else: max_rating = ratings.EXPLICIT.code if d.get_rating(userid) > max_rating: d.execute( """ UPDATE profile SET config = REGEXP_REPLACE(config, '[ap]', '', 'g') || '%s' WHERE userid = %i """, [rating_flag, userid]) d._get_config.invalidate(userid) updates.append('- Birthday: %s' % (birthday, )) # Gender if gender is not None: d.execute("UPDATE userinfo SET gender = '%s' WHERE userid = %i", [gender, userid]) updates.append('- Gender: %s' % (gender, )) # Location if country is not None: d.execute("UPDATE userinfo SET country = '%s' WHERE userid = %i", [country, userid]) updates.append('- Country: %s' % (country, )) # Social and contact links if remove_social: for social_link in remove_social: d.engine.execute( "DELETE FROM user_links WHERE userid = %(userid)s AND link_type = %(link)s", userid=userid, link=social_link) updates.append('- Removed social link for %s' % (social_link, )) # Permissions if permission_tag is not None: if permission_tag: query = ( "UPDATE profile SET config = replace(config, 'g', '') " "WHERE userid = %(user)s AND position('g' in config) != 0") else: query = ("UPDATE profile SET config = config || 'g' " "WHERE userid = %(user)s AND position('g' in config) = 0") if d.engine.execute(query, user=userid).rowcount != 0: updates.append('- Permission to tag: ' + ('yes' if permission_tag else 'no')) d._get_config.invalidate(userid) if updates: from weasyl import moderation moderation.note_about(my_userid, userid, 'The following fields were changed:', '\n'.join(updates))
def get_user_age(userid): assert userid return d.convert_age( d.execute("SELECT birthday FROM userinfo WHERE userid = %i", [userid], ["element"]))
def do_manage(my_userid, userid, username=None, full_name=None, catchphrase=None, birthday=None, gender=None, country=None, remove_social=None, permission_tag=None): """Updates a user's information from the admin user management page. After updating the user it records all the changes into the mod notes. If an argument is None it will not be updated. Args: my_userid (int): ID of user making changes to other user. userid (int): ID of user to modify. username (str): New username for user. Defaults to None. full_name (str): New full name for user. Defaults to None. catchphrase (str): New catchphrase for user. Defaults to None. birthday (str): New birthday for user, in HTML5 date format (ISO 8601 yyyy-mm-dd). Defaults to None. gender (str): New gender for user. Defaults to None. country (str): New country for user. Defaults to None. remove_social (list): Items to remove from the user's social/contact links. Defaults to None. permission_tag (bool): New tagging permission for user. Defaults to None. Returns: Does not return. """ updates = [] # Username if username is not None: login.change_username( acting_user=my_userid, target_user=userid, bypass_limit=True, new_username=username, ) updates.append('- Username: %s' % (username, )) # Full name if full_name is not None: d.engine.execute( "UPDATE profile SET full_name = %(full_name)s WHERE userid = %(user)s", full_name=full_name, user=userid) updates.append('- Full name: %s' % (full_name, )) # Catchphrase if catchphrase is not None: d.engine.execute( "UPDATE profile SET catchphrase = %(catchphrase)s WHERE userid = %(user)s", catchphrase=catchphrase, user=userid) updates.append('- Catchphrase: %s' % (catchphrase, )) # Birthday if birthday is not None: # HTML5 date format is yyyy-mm-dd split = birthday.split("-") if len(split) != 3 or d.convert_unixdate( day=split[2], month=split[1], year=split[0]) is None: raise WeasylError("birthdayInvalid") unixtime = d.convert_unixdate(day=split[2], month=split[1], year=split[0]) age = d.convert_age(unixtime) d.execute("UPDATE userinfo SET birthday = %i WHERE userid = %i", [unixtime, userid]) if age < ratings.EXPLICIT.minimum_age: max_rating = ratings.GENERAL.code rating_flag = "" else: max_rating = ratings.EXPLICIT.code if d.get_rating(userid) > max_rating: d.engine.execute( """ UPDATE profile SET config = REGEXP_REPLACE(config, '[ap]', '', 'g') || %(rating_flag)s WHERE userid = %(user)s """, rating_flag=rating_flag, user=userid, ) d._get_all_config.invalidate(userid) updates.append('- Birthday: %s' % (birthday, )) # Gender if gender is not None: d.engine.execute( "UPDATE userinfo SET gender = %(gender)s WHERE userid = %(user)s", gender=gender, user=userid) updates.append('- Gender: %s' % (gender, )) # Location if country is not None: d.engine.execute( "UPDATE userinfo SET country = %(country)s WHERE userid = %(user)s", country=country, user=userid) updates.append('- Country: %s' % (country, )) # Social and contact links if remove_social: for social_link in remove_social: d.engine.execute( "DELETE FROM user_links WHERE userid = %(userid)s AND link_type = %(link)s", userid=userid, link=social_link) updates.append('- Removed social link for %s' % (social_link, )) # Permissions if permission_tag is not None: if permission_tag: query = ( "UPDATE profile SET config = replace(config, 'g', '') " "WHERE userid = %(user)s AND position('g' in config) != 0") else: query = ("UPDATE profile SET config = config || 'g' " "WHERE userid = %(user)s AND position('g' in config) = 0") if d.engine.execute(query, user=userid).rowcount != 0: updates.append('- Permission to tag: ' + ('yes' if permission_tag else 'no')) d._get_all_config.invalidate(userid) if updates: from weasyl import moderation moderation.note_about(my_userid, userid, 'The following fields were changed:', '\n'.join(updates))
def get_user_age(userid): assert userid return d.convert_age( d.engine.scalar( "SELECT birthday FROM userinfo WHERE userid = %(user)s", user=userid))
def do_manage(my_userid, userid, username=None, full_name=None, catchphrase=None, birthday=None, gender=None, country=None, remove_social=None, permission_tag=None): """Updates a user's information from the admin user management page. After updating the user it records all the changes into the mod notes. If an argument is None it will not be updated. Args: my_userid (int): ID of user making changes to other user. userid (int): ID of user to modify. username (str): New username for user. Defaults to None. full_name (str): New full name for user. Defaults to None. catchphrase (str): New catchphrase for user. Defaults to None. birthday (str): New birthday for user, in format for convert_inputdate. Defaults to None. gender (str): New gender for user. Defaults to None. country (str): New country for user. Defaults to None. remove_social (list): Items to remove from the user's social/contact links. Defaults to None. permission_tag (bool): New tagging permission for user. Defaults to None. Returns: Does not return. """ updates = [] # Username if username is not None: if not d.get_sysname(username): raise WeasylError("usernameInvalid") elif d.execute("SELECT EXISTS (SELECT 0 FROM login WHERE login_name = '%s')", [d.get_sysname(username)], ["bool"]): raise WeasylError("usernameExists") elif d.execute("SELECT EXISTS (SELECT 0 FROM useralias WHERE alias_name = '%s')", [d.get_sysname(username)], ["bool"]): raise WeasylError("usernameExists") elif d.execute("SELECT EXISTS (SELECT 0 FROM logincreate WHERE login_name = '%s')", [d.get_sysname(username)], ["bool"]): raise WeasylError("usernameExists") d.execute("UPDATE login SET login_name = '%s' WHERE userid = %i", [d.get_sysname(username), userid]) d._get_display_name.invalidate(userid) d.execute("UPDATE profile SET username = '******' WHERE userid = %i", [username, userid]) updates.append('- Username: %s' % (username,)) # Full name if full_name is not None: d.execute("UPDATE profile SET full_name = '%s' WHERE userid = %i", [full_name, userid]) updates.append('- Full name: %s' % (full_name,)) # Catchphrase if catchphrase is not None: d.execute("UPDATE profile SET catchphrase = '%s' WHERE userid = %i", [catchphrase, userid]) updates.append('- Catchphrase: %s' % (catchphrase,)) # Birthday if birthday is not None and d.convert_inputdate(birthday): unixtime = d.convert_inputdate(birthday) age = d.convert_age(unixtime) d.execute("UPDATE userinfo SET birthday = %i WHERE userid = %i", [unixtime, userid]) if age < ratings.MODERATE.minimum_age: max_rating = ratings.GENERAL.code rating_flag = "" elif age < ratings.EXPLICIT.minimum_age: max_rating = ratings.MODERATE.code rating_flag = "m" else: max_rating = ratings.EXPLICIT.code if d.get_rating(userid) > max_rating: d.execute( """ UPDATE profile SET config = REGEXP_REPLACE(config, '[map]', '', 'g') || '%s' WHERE userid = %i """, [rating_flag, userid] ) d._get_config.invalidate(userid) updates.append('- Birthday: %s' % (birthday,)) # Gender if gender is not None: d.execute("UPDATE userinfo SET gender = '%s' WHERE userid = %i", [gender, userid]) updates.append('- Gender: %s' % (gender,)) # Location if country is not None: d.execute("UPDATE userinfo SET country = '%s' WHERE userid = %i", [country, userid]) updates.append('- Country: %s' % (country,)) # Social and contact links if remove_social: for social_link in remove_social: d.engine.execute("DELETE FROM user_links WHERE userid = %(userid)s AND link_type = %(link)s", userid=userid, link=social_link) updates.append('- Removed social link for %s' % (social_link,)) # Permissions if permission_tag is not None: if permission_tag: query = ( "UPDATE profile SET config = replace(config, 'g', '') " "WHERE userid = %(user)s AND position('g' in config) != 0") else: query = ( "UPDATE profile SET config = config || 'g' " "WHERE userid = %(user)s AND position('g' in config) = 0") if d.engine.execute(query, user=userid).rowcount != 0: updates.append('- Permission to tag: ' + ('yes' if permission_tag else 'no')) d._get_config.invalidate(userid) if updates: from weasyl import moderation moderation.note_about(my_userid, userid, 'The following fields were changed:', '\n'.join(updates))
def get_user_age(userid): assert userid return d.convert_age(d.execute("SELECT birthday FROM userinfo WHERE userid = %i", [userid], ["element"]))