def insert(userid, tagid=None, title=None, rating=None): if rating not in ratings.CODE_MAP: rating = ratings.GENERAL.code profile.check_user_rating_allowed(userid, rating) if tagid: tag = int(tagid) try: d.engine.execute("INSERT INTO blocktag VALUES (%s, %s, %s)", userid, tag, rating) except PostgresError: return elif title: tag_name = d.get_search_tag(title) try: d.engine.execute(""" INSERT INTO blocktag (userid, tagid, rating) VALUES ( %(user)s, (SELECT tagid FROM searchtag WHERE title = %(tag_name)s), %(rating)s ) """, user=userid, tag_name=tag_name, rating=rating) except PostgresError: try: tag = searchtag.create(title) except PostgresError: return d.engine.execute("INSERT INTO blocktag VALUES (%s, %s, %s)", userid, tag, rating) cached_select.invalidate(userid)
def insert(userid, title, rating): if rating not in ratings.CODE_MAP: rating = ratings.GENERAL.code profile.check_user_rating_allowed(userid, rating) d.engine.execute( 'INSERT INTO blocktag (userid, tagid, rating) VALUES (%(user)s, %(tag)s, %(rating)s) ON CONFLICT DO NOTHING', user=userid, tag=searchtag.get_or_create(title), rating=rating) select_ids.invalidate(userid)
def create_generic(userid, submission, **kwargs): tags = kwargs['tags'] if submission.subtype not in valid_types: submission.subtype = expected_type * 1000 + 999 if not submission.title: raise WeasylError("titleInvalid") elif not submission.rating: raise WeasylError("ratingInvalid") elif len(tags) < 2: raise WeasylError("notEnoughTags") elif not folder.check(userid, submission.folderid): raise WeasylError("Unexpected") profile.check_user_rating_allowed(userid, submission.rating) return create_specific(userid=userid, submission=submission, **kwargs)
def create_generic(userid, submission, **kwargs): tags = kwargs['tags'] if submission.subtype not in valid_types: submission.subtype = expected_type * 1000 + 999 if not submission.title: raise WeasylError("titleInvalid") elif not submission.rating: raise WeasylError("ratingInvalid") elif len(tags) < 2: raise WeasylError("notEnoughTags") elif not folder.check(userid, submission.folderid): raise WeasylError("Unexpected") profile.check_user_rating_allowed(userid, submission.rating) return create_specific( userid=userid, submission=submission, **kwargs)
def create(userid, journal, friends_only=False, tags=None): # Check invalid arguments if not journal.title: raise WeasylError("titleInvalid") elif not journal.content: raise WeasylError("contentInvalid") elif not journal.rating: raise WeasylError("ratingInvalid") profile.check_user_rating_allowed(userid, journal.rating) # Assign settings settings = "f" if friends_only else "" # Create journal jo = d.meta.tables["journal"] journalid = d.engine.execute(jo.insert().returning(jo.c.journalid), { "userid": userid, "title": journal.title, "rating": journal.rating.code, "unixtime": arrow.now(), "settings": settings, }).scalar() # Write journal file files.make_path(journalid, "journal") files.write(files.make_resource(userid, journalid, "journal/submit"), journal.content) # Assign search tags searchtag.associate(userid, tags, journalid=journalid) # Create notifications if "m" not in settings: welcome.journal_insert(userid, journalid, rating=journal.rating.code, settings=settings) d.metric('increment', 'journals') return journalid
def edit(userid, character, friends_only): query = define.execute("SELECT userid, settings FROM character WHERE charid = %i", [character.charid], options="single") if not query or "h" in query[1]: raise WeasylError("Unexpected") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") elif not character.char_name: raise WeasylError("characterNameInvalid") elif not character.rating: raise WeasylError("Unexpected") profile.check_user_rating_allowed(userid, character.rating) # Assign settings settings = [query[1].replace("f", "")] settings.append("f" if friends_only else "") settings = "".join(settings) if "f" in settings: welcome.character_remove(character.charid) define.execute( """ UPDATE character SET (char_name, age, gender, height, weight, species, content, rating, settings) = ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %i, '%s') WHERE charid = %i """, [character.char_name, character.age, character.gender, character.height, character.weight, character.species, character.content, character.rating.code, settings, character.charid]) if userid != query[0]: from weasyl import moderation moderation.note_about( userid, query[0], 'The following character was edited:', '- ' + text.markdown_link(character.char_name, '/character/%s?anyway=true' % (character.charid,)))
def edit(userid, journal, friends_only=False): if not journal.title: raise WeasylError("titleInvalid") elif not journal.content: raise WeasylError("contentInvalid") elif not journal.rating: raise WeasylError("ratingInvalid") profile.check_user_rating_allowed(userid, journal.rating) query = d.execute("SELECT userid, settings FROM journal WHERE journalid = %i", [journal.journalid], options="single") if not query or "h" in query[1]: raise WeasylError("Unexpected") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") settings = [query[1].replace("f", "")] settings.append("f" if friends_only else "") settings = "".join(settings) if "f" in settings: welcome.journal_remove(journal.journalid) # TODO(kailys): use ORM d.execute("UPDATE journal SET (title, rating, settings) = ('%s', %i, '%s') WHERE journalid = %i", [journal.title, journal.rating.code, settings, journal.journalid]) # Write journal file files.write(files.make_resource(userid, journal.journalid, "journal/submit"), journal.content) if userid != query[0]: from weasyl import moderation moderation.note_about( userid, query[0], 'The following journal was edited:', '- ' + text.markdown_link(journal.title, '/journal/%s?anyway=true' % (journal.journalid,)))
def edit(userid, submission, embedlink=None, friends_only=False, critique=False): query = d.execute( "SELECT userid, subtype, settings FROM submission WHERE submitid = %i", [submission.submitid], ["single"]) if not query or "h" in query[2]: raise WeasylError("Unexpected") elif "a" in query[2] and userid not in staff.MODS: raise WeasylError("AdminLocked") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") elif not submission.title: raise WeasylError("titleInvalid") elif not submission.rating: raise WeasylError("Unexpected") elif not folder.check(query[0], submission.folderid): raise WeasylError("Unexpected") elif submission.subtype / 1000 != query[1] / 1000: raise WeasylError("Unexpected") elif 'v' in query[2] and not embed.check_valid(embedlink): raise WeasylError("embedlinkInvalid") elif 'D' in query[2]: check_google_doc_embed_data(embedlink) profile.check_user_rating_allowed(userid, submission.rating) # Assign settings settings = [query[2].replace("f", "").replace("q", "")] settings.append("f" if friends_only else "") settings.append("q" if critique else "") settings = "".join(settings) if "v" in settings: submission.content = "%s\n%s" % (embedlink, submission.content) if "f" in settings: welcome.submission_became_friends_only(submission.submitid, userid) # TODO(kailys): maintain ORM object db = d.connect() su = d.meta.tables['submission'] q = (su.update().values( folderid=submission.folderid, title=submission.title, content=submission.content, subtype=submission.subtype, rating=submission.rating, settings=settings, ).where(su.c.submitid == submission.submitid)) db.execute(q) if 'D' in settings: db = d.connect() gde = d.meta.tables['google_doc_embeds'] q = (gde.update().values(embed_url=embedlink).where( gde.c.submitid == submission.submitid)) db.execute(q) if userid != query[0]: from weasyl import moderation moderation.note_about( userid, query[0], 'The following submission was edited:', '- ' + text.markdown_link( submission.title, '/submission/%s?anyway=true' % (submission.submitid, )))
def create(userid, character, friends, tags, thumbfile, submitfile): # Make temporary filenames tempthumb = files.get_temporary(userid, "thumb") tempsubmit = files.get_temporary(userid, "char") # Determine filesizes thumbsize = len(thumbfile) submitsize = len(submitfile) # Check invalid title or rating if not character.char_name: raise WeasylError("characterNameInvalid") elif not character.rating: raise WeasylError("ratingInvalid") profile.check_user_rating_allowed(userid, character.rating) # Write temporary thumbnail file if thumbsize: files.easyupload(tempthumb, thumbfile, "image") thumbextension = files.get_extension_for_category( thumbfile, m.ART_SUBMISSION_CATEGORY) else: thumbextension = None # Write temporary submission file if submitsize: files.easyupload(tempsubmit, submitfile, "image") submitextension = files.get_extension_for_category( submitfile, m.ART_SUBMISSION_CATEGORY) else: submitextension = None # Check invalid file data if not submitsize: files.clear_temporary(userid) raise WeasylError("submitSizeZero") elif submitsize > 10 * _MEGABYTE: files.clear_temporary(userid) raise WeasylError("submitSizeExceedsLimit") elif thumbsize > 10 * _MEGABYTE: files.clear_temporary(userid) raise WeasylError("thumbSizeExceedsLimit") elif submitextension not in [".jpg", ".png", ".gif"]: files.clear_temporary(userid) raise WeasylError("submitType") elif thumbsize and thumbextension not in [".jpg", ".png", ".gif"]: files.clear_temporary(userid) raise WeasylError("thumbType") # Assign settings settings = [] settings.append("f" if friends else "") settings.append(files.typeflag("submit", submitextension)) settings.append(files.typeflag("cover", submitextension)) settings = "".join(settings) # Insert submission data ch = define.meta.tables["character"] try: charid = define.engine.execute(ch.insert().returning(ch.c.charid), { "userid": userid, "unixtime": arrow.now(), "char_name": character.char_name, "age": character.age, "gender": character.gender, "height": character.height, "weight": character.weight, "species": character.species, "content": character.content, "rating": character.rating.code, "settings": settings, }).scalar() except PostgresError: files.clear_temporary(userid) raise # Assign search tags searchtag.associate(userid, tags, charid=charid) # Make submission file files.make_path(charid, "char") files.copy(tempsubmit, files.make_resource(userid, charid, "char/submit", submitextension)) # Make cover file image.make_cover(tempsubmit, files.make_resource(userid, charid, "char/cover", submitextension)) # Make thumbnail selection file if thumbsize: image.make_cover( tempthumb, files.make_resource(userid, charid, "char/.thumb")) thumbnail.create(userid, 0, 0, 0, 0, charid=charid, remove=False) # Create notifications welcome.character_insert(userid, charid, rating=character.rating.code, settings=settings) # Clear temporary files files.clear_temporary(userid) define.metric('increment', 'characters') return charid
def edit(userid, submission, embedlink=None, friends_only=False, critique=False): query = d.execute( "SELECT userid, subtype, settings FROM submission WHERE submitid = %i", [submission.submitid], ["single"]) if not query or "h" in query[2]: raise WeasylError("Unexpected") elif "a" in query[2] and userid not in staff.MODS: raise WeasylError("AdminLocked") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") elif not submission.title: raise WeasylError("titleInvalid") elif not submission.rating: raise WeasylError("Unexpected") elif not folder.check(query[0], submission.folderid): raise WeasylError("Unexpected") elif submission.subtype / 1000 != query[1] / 1000: raise WeasylError("Unexpected") elif 'v' in query[2] and not embed.check_valid(embedlink): raise WeasylError("embedlinkInvalid") elif 'D' in query[2]: check_google_doc_embed_data(embedlink) profile.check_user_rating_allowed(userid, submission.rating) # Assign settings settings = [query[2].replace("f", "").replace("q", "")] settings.append("f" if friends_only else "") settings.append("q" if critique else "") settings = "".join(settings) if "v" in settings: submission.content = "%s\n%s" % (embedlink, submission.content) if "f" in settings: welcome.submission_became_friends_only(submission.submitid, userid) # TODO(kailys): maintain ORM object db = d.connect() su = d.meta.tables['submission'] q = ( su.update() .values( folderid=submission.folderid, title=submission.title, content=submission.content, subtype=submission.subtype, rating=submission.rating, settings=settings, ) .where(su.c.submitid == submission.submitid)) db.execute(q) if 'D' in settings: db = d.connect() gde = d.meta.tables['google_doc_embeds'] q = (gde.update() .values(embed_url=embedlink) .where(gde.c.submitid == submission.submitid)) db.execute(q) if userid != query[0]: from weasyl import moderation moderation.note_about( userid, query[0], 'The following submission was edited:', '- ' + text.markdown_link(submission.title, '/submission/%s?anyway=true' % (submission.submitid,)))