def select_view_api(userid, journalid, anyway=False, increment_views=False): rating = d.get_rating(userid) journal = _select_journal_and_check( userid, journalid, rating=rating, ignore=anyway, anyway=anyway, increment_views=increment_views) content = files.read(files.make_resource(userid, journalid, 'journal/submit')) return { 'journalid': journalid, 'title': journal['title'], 'owner': journal['username'], 'owner_login': d.get_sysname(journal['username']), 'owner_media': api.tidy_all_media( media.get_user_media(journal['userid'])), 'content': text.markdown(content), 'tags': searchtag.select(journalid=journalid), 'link': d.absolutify_url('/journal/%d/%s' % (journalid, text.slug_for(journal['title']))), 'type': 'journal', 'rating': ratings.CODE_TO_NAME[journal['rating']], 'views': journal['page_views'], 'favorites': favorite.count(journalid, 'journal'), 'comments': comment.count(journalid, 'journal'), 'favorited': favorite.check(userid, journalid=journalid), 'friends_only': 'f' in journal['settings'], 'posted_at': d.iso8601(journal['unixtime']), }
def offer(userid, submitid, otherid): query = d.execute("SELECT userid, rating, settings FROM submission WHERE submitid = %i", [submitid], options="single") if not query or "h" in query[2]: raise WeasylError("Unexpected") elif userid != query[0]: raise WeasylError("Unexpected") # Check collection acceptability if otherid: rating = d.get_rating(otherid) if rating < query[1]: raise WeasylError("collectionUnacceptable") if "f" in query[2]: raise WeasylError("collectionUnacceptable") if ignoreuser.check(otherid, userid): raise WeasylError("IgnoredYou") if ignoreuser.check(userid, otherid): raise WeasylError("YouIgnored") if blocktag.check(otherid, submitid=submitid): raise WeasylError("collectionUnacceptable") try: d.execute("INSERT INTO collection (userid, submitid, unixtime) VALUES (%i, %i, %i)", [otherid, submitid, d.get_time()]) except PostgresError: raise WeasylError("collectionExists") welcome.collectoffer_insert(userid, otherid, submitid)
def select_view_api(userid, charid, anyway=False, increment_views=False): rating = define.get_rating(userid) query = _select_character_and_check( userid, charid, rating=rating, ignore=anyway, anyway=anyway, increment_views=increment_views) login = define.get_sysname(query.username) return { 'charid': charid, 'owner': query.username, 'owner_login': login, 'owner_media': api.tidy_all_media(media.get_user_media(query.userid)), 'posted_at': define.iso8601(query.unixtime), 'title': query.char_name, 'age': query.age, 'gender': query.gender, 'height': query.height, 'weight': query.weight, 'species': query.species, 'content': text.markdown(query.content), 'rating': ratings.CODE_TO_NAME[query.rating], 'favorited': favorite.check(userid, charid=charid), 'views': query.page_views, 'friends_only': 'f' in query.settings, 'favorites': favorite.count(charid, 'character'), 'comments': comment.count(charid, 'character'), 'media': fake_media_items(charid, query.userid, login, query.settings, absolutify=True), 'tags': searchtag.select(charid=charid), 'type': 'character', 'link': define.absolutify_url('/character/%d/%s' % (charid, text.slug_for(query.char_name))), }
def select_view_api(userid, submitid, anyway=False, increment_views=False): rating = d.get_rating(userid) db = d.connect() sub = db.query(orm.Submission).get(submitid) if sub is None or 'hidden' in sub.settings: raise WeasylError("submissionRecordMissing") sub_rating = sub.rating.code if 'friends-only' in sub.settings and not frienduser.check(userid, sub.userid): raise WeasylError("submissionRecordMissing") elif sub_rating > rating and userid != sub.userid: raise WeasylError("RatingExceeded") elif not anyway and ignoreuser.check(userid, sub.userid): raise WeasylError("UserIgnored") elif not anyway and blocktag.check(userid, submitid=submitid): raise WeasylError("TagBlocked") description = sub.content embedlink = None if 'embedded-content' in sub.settings: embedlink, _, description = description.partition('\n') elif 'gdocs-embed' in sub.settings: embedlink = sub.google_doc_embed.embed_url views = sub.page_views if increment_views and d.common_view_content(userid, submitid, 'submit'): views += 1 return { 'submitid': submitid, 'title': sub.title, 'owner': sub.owner.profile.username, 'owner_login': sub.owner.login_name, 'owner_media': api.tidy_all_media(media.get_user_media(sub.userid)), 'media': api.tidy_all_media(media.get_submission_media(submitid)), 'description': text.markdown(description), 'embedlink': embedlink, 'folderid': sub.folderid, 'folder_name': sub.folder.title if sub.folderid else None, 'posted_at': d.iso8601(sub.unixtime), 'tags': searchtag.select(submitid=submitid), 'link': d.absolutify_url("/submission/%d/%s" % (submitid, text.slug_for(sub.title))), 'type': 'submission', 'subtype': m.CATEGORY_PARSABLE_MAP[sub.subtype // 1000 * 1000], 'rating': sub.rating.name, 'views': views, 'favorites': favorite.count(submitid), 'comments': comment.count(submitid), 'favorited': favorite.check(userid, submitid=submitid), 'friends_only': 'friends-only' in sub.settings, }
def template_fields(userid): config = d.get_config(userid) rating = d.get_rating(userid) submissions = list(filter_submissions(userid, recent_submissions())) ret = partition_submissions(submissions) return ret + [ # Recent site news update siteupdate.select(), # Recent critique submissions submission.select_list(userid, rating, 4, options=["critique"], config=config), # Currently streaming users profile.select_streaming(userid, rating, 4), # Recently popular submissions list(itertools.islice(filter_submissions(userid, submission.select_recently_popular(), incidence_limit=1), 11)), ]
def request(userid, submitid, otherid): query = d.engine.execute( "SELECT userid, rating, settings " "FROM submission WHERE submitid = %(submission)s", submission=submitid).first() rating = d.get_rating(userid) if not query or "h" in query.settings: raise WeasylError("Unexpected") if otherid != query.userid: raise WeasylError("Unexpected") # not checking for blocktags here because if you want to collect # something with a tag you don't like that's your business if rating < query.rating: raise WeasylError("RatingExceeded") if "f" in query.settings: raise WeasylError("collectionUnacceptable") if ignoreuser.check(otherid, userid): raise WeasylError("IgnoredYou") if ignoreuser.check(userid, otherid): raise WeasylError("YouIgnored") if _check_throttle(userid, otherid): raise WeasylError("collectionThrottle") settings = d.get_profile_settings(otherid) if not settings.allow_collection_requests: raise WeasylError("Unexpected") request_settings = "r" try: d.engine.execute( "INSERT INTO collection (userid, submitid, unixtime, settings) " "VALUES (%(userid)s, %(submitid)s, %(now)s, %(settings)s)", userid=userid, submitid=submitid, now=d.get_time(), settings=request_settings) except PostgresError: raise WeasylError("collectionExists") welcome.collectrequest_insert(userid, otherid, submitid)
def template_fields(userid): config = d.get_config(userid) rating = d.get_rating(userid) submissions = list(filter_submissions(userid, recent_submissions())) ret = partition_submissions(submissions) return ret + [ # Recent site news update siteupdate.select(), # Recent critique submissions submission.select_list( userid, rating, 4, options=["critique"], config=config), # Currently streaming users profile.select_streaming(userid, rating, 4), # Recently popular submissions list( itertools.islice( filter_submissions(userid, submission.select_recently_popular(), incidence_limit=1), 11)), ]
def request(userid, submitid, otherid): query = d.engine.execute("SELECT userid, rating, settings " "FROM submission WHERE submitid = %(submission)s", submission=submitid).first() rating = d.get_rating(userid) if not query or "h" in query.settings: raise WeasylError("Unexpected") if otherid != query.userid: raise WeasylError("Unexpected") # not checking for blocktags here because if you want to collect # something with a tag you don't like that's your business if rating < query.rating: raise WeasylError("RatingExceeded") if "f" in query.settings: raise WeasylError("collectionUnacceptable") if ignoreuser.check(otherid, userid): raise WeasylError("IgnoredYou") if ignoreuser.check(userid, otherid): raise WeasylError("YouIgnored") if _check_throttle(userid, otherid): raise WeasylError("collectionThrottle") settings = d.get_profile_settings(otherid) if not settings.allow_collection_requests: raise WeasylError("Unexpected") request_settings = "r" try: d.engine.execute("INSERT INTO collection (userid, submitid, unixtime, settings) " "VALUES (%(userid)s, %(submitid)s, %(now)s, %(settings)s)", userid=userid, submitid=submitid, now=d.get_time(), settings=request_settings) except PostgresError: raise WeasylError("collectionExists") welcome.collectrequest_insert(userid, otherid, submitid)
def filter_submissions(userid, submissions, incidence_limit=3): """ Filters a list of submissions according to the user's preferences and optionally limits the number of items returned from one submitter. :param userid: The userid to use for blocked tags and rating. Usually the viewing user. :type userid: int. :param submissions: A list of submissions with appropriate fields (rating, userid, etc.) :type submissions: A list of submission dictionaries. :param incidence_limit: The maximum number of submissions to permit from one creator. Set to 0 or None for no limit. :type incidence_limit: int. :return: An iterator over the submissions after filtering by the user's settings and incidence limit. """ blocked_tags = ignored_users = set() rating = ratings.GENERAL.code if userid: rating = d.get_rating(userid) blocked_tags = blocktag.cached_select(userid) ignored_users = set(ignoreuser.cached_list_ignoring(userid)) submitter_incidence = collections.defaultdict(int) for s in submissions: if incidence_limit and submitter_incidence[ s['userid']] >= incidence_limit: continue if s['rating'] > rating: continue if s['userid'] in ignored_users: continue tags = set(s['tags']) if blocktag.check_list(s['rating'], tags, blocked_tags): continue submitter_incidence[s['userid']] += 1 yield s
def filter_submissions(userid, submissions, incidence_limit=3): """ Filters a list of submissions according to the user's preferences and optionally limits the number of items returned from one submitter. :param userid: The userid to use for blocked tags and rating. Usually the viewing user. :type userid: int. :param submissions: A list of submissions with appropriate fields (rating, userid, etc.) :type submissions: A list of submission dictionaries. :param incidence_limit: The maximum number of submissions to permit from one creator. Set to 0 or None for no limit. :type incidence_limit: int. :return: An iterator over the submissions after filtering by the user's settings and incidence limit. """ blocked_tags = ignored_users = set() rating = ratings.GENERAL.code if userid: rating = d.get_rating(userid) blocked_tags = blocktag.cached_select(userid) ignored_users = set(ignoreuser.cached_list_ignoring(userid)) submitter_incidence = collections.defaultdict(int) for s in submissions: if incidence_limit and submitter_incidence[s['userid']] >= incidence_limit: continue if s['rating'] > rating: continue if s['userid'] in ignored_users: continue tags = set(s['tags']) if blocktag.check_list(s['rating'], tags, blocked_tags): continue submitter_incidence[s['userid']] += 1 yield s
def select_view_api(userid, charid, anyway=False, increment_views=False): rating = define.get_rating(userid) query = _select_character_and_check( userid, charid, rating=rating, ignore=anyway, anyway=anyway, increment_views=increment_views) login = define.get_sysname(query['username']) return { 'charid': charid, 'owner': query['username'], 'owner_login': login, 'owner_media': api.tidy_all_media( media.get_user_media(query['userid'])), 'posted_at': define.iso8601(query['unixtime']), 'title': query['char_name'], 'age': query['age'], 'gender': query['gender'], 'height': query['height'], 'weight': query['weight'], 'species': query['species'], 'content': text.markdown(query['content']), 'rating': ratings.CODE_TO_NAME[query['rating']], 'favorited': favorite.check(userid, charid=charid), 'views': query['page_views'], 'friends_only': 'f' in query['settings'], 'favorites': favorite.count(charid, 'character'), 'comments': comment.count(charid, 'character'), 'media': api.tidy_all_media(fake_media_items( charid, query['userid'], login, query['settings'])), 'tags': searchtag.select(charid=charid), 'type': 'character', 'link': define.absolutify_url( '/character/%d/%s' % (charid, text.slug_for(query['char_name']))), }
def do_manage(my_userid, userid, username=None, full_name=None, catchphrase=None, birthday=None, gender=None, country=None): 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,)) if updates: from weasyl import moderation moderation.note_about(my_userid, userid, 'The following fields were changed:', '\n'.join(updates))
def do_manage(my_userid, userid, username=None, full_name=None, catchphrase=None, birthday=None, gender=None, country=None): 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, )) if updates: from weasyl import moderation moderation.note_about(my_userid, userid, 'The following fields were changed:', '\n'.join(updates))
def select_view_api(userid, submitid, anyway=False, increment_views=False): rating = d.get_rating(userid) db = d.connect() sub = db.query(orm.Submission).get(submitid) if sub is None or 'hidden' in sub.settings: raise WeasylError("submissionRecordMissing") sub_rating = sub.rating.code if 'friends-only' in sub.settings and not frienduser.check( userid, sub.userid): raise WeasylError("submissionRecordMissing") elif sub_rating > rating and userid != sub.userid: raise WeasylError("RatingExceeded") elif not anyway and ignoreuser.check(userid, sub.userid): raise WeasylError("UserIgnored") elif not anyway and blocktag.check(userid, submitid=submitid): raise WeasylError("TagBlocked") description = sub.content embedlink = None if 'embedded-content' in sub.settings: embedlink, _, description = description.partition('\n') elif 'gdocs-embed' in sub.settings: embedlink = sub.google_doc_embed.embed_url views = sub.page_views if increment_views and d.common_view_content(userid, submitid, 'submit'): views += 1 return { 'submitid': submitid, 'title': sub.title, 'owner': sub.owner.profile.username, 'owner_login': sub.owner.login_name, 'owner_media': api.tidy_all_media(media.get_user_media(sub.userid)), 'media': api.tidy_all_media(media.get_submission_media(submitid)), 'description': text.markdown(description), 'embedlink': embedlink, 'folderid': sub.folderid, 'folder_name': sub.folder.title if sub.folderid else None, 'posted_at': d.iso8601(sub.unixtime), 'tags': searchtag.select(submitid=submitid), 'link': d.absolutify_url("/submission/%d/%s" % (submitid, text.slug_for(sub.title))), 'type': 'submission', 'subtype': m.CATEGORY_PARSABLE_MAP[sub.subtype // 1000 * 1000], 'rating': sub.rating.name, 'views': views, 'favorites': favorite.count(submitid), 'comments': comment.count(submitid), 'favorited': favorite.check(userid, submitid=submitid), 'friends_only': 'friends-only' in sub.settings, }