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 reupload(userid, charid, submitdata): submitsize = len(submitdata) if not submitsize: raise WeasylError("submitSizeZero") elif submitsize > 10 * _MEGABYTE: raise WeasylError("submitSizeExceedsLimit") # Select character data query, = define.engine.execute(""" SELECT userid, settings FROM character WHERE charid = %(character)s AND settings !~ 'h' """, character=charid) if userid != query.userid: raise WeasylError("Unexpected") im = image.from_string(submitdata) submitextension = image.image_extension(im) # Check invalid file data if not submitextension: raise WeasylError("submitType") # Make submission file submitfile = files.make_resource(userid, charid, "char/submit", submitextension) files.ensure_file_directory(submitfile) im.write(submitfile) # Make cover file image.make_cover( submitfile, files.make_resource(userid, charid, "char/cover", submitextension)) # Update settings settings = re.sub(r'[~=].', '', query.settings) settings += files.typeflag("submit", submitextension) settings += files.typeflag("cover", submitextension) define.engine.execute(""" UPDATE character SET settings = %(settings)s WHERE charid = %(character)s """, settings=settings, character=charid)
def reupload(userid, charid, submitdata): submitsize = len(submitdata) if not submitsize: raise WeasylError("submitSizeZero") elif submitsize > 10 * _MEGABYTE: raise WeasylError("submitSizeExceedsLimit") # Select character data query, = define.engine.execute(""" SELECT userid, settings FROM character WHERE charid = %(character)s AND settings !~ 'h' """, character=charid) if userid != query.userid: raise WeasylError("Unexpected") im = image.from_string(submitdata) submitextension = image.image_extension(im) # Check invalid file data if not submitextension: raise WeasylError("submitType") # Make submission file submitfile = files.make_resource(userid, charid, "char/submit", submitextension) files.ensure_file_directory(submitfile) im.write(submitfile) # Make cover file image.make_cover( submitfile, files.make_resource(userid, charid, "char/cover", submitextension)) # Update settings settings = re.sub(r'[~=].', '', query.settings) settings += files.typeflag("submit", submitextension) settings += files.typeflag("cover", submitextension) define.engine.execute(""" UPDATE character SET settings = %(settings)s WHERE charid = %(character)s """, settings=settings, character=charid)
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 select_view(userid, rating, journalid, ignore=True, anyway=None): journal = _select_journal_and_check(userid, journalid, rating=rating, ignore=ignore, anyway=anyway == "true") return { 'journalid': journalid, 'userid': journal['userid'], 'username': journal['username'], 'user_media': media.get_user_media(journal['userid']), 'mine': userid == journal['userid'], 'unixtime': journal['unixtime'], 'title': journal['title'], 'content': files.read(files.make_resource(userid, journalid, 'journal/submit')), 'rating': journal['rating'], 'settings': journal['settings'], 'page_views': journal['page_views'], 'reported': report.check(journalid=journalid), 'favorited': favorite.check(userid, journalid=journalid), 'friends_only': 'f' in journal['settings'], 'hidden_submission': 'h' in journal['settings'], 'fave_count': favorite.count(journalid, 'journal'), 'tags': searchtag.select(journalid=journalid), 'comments': comment.select(userid, journalid=journalid), }
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 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.scalar( jo.insert().returning(jo.c.journalid), { "userid": userid, "title": journal.title, "rating": journal.rating.code, "unixtime": arrow.now(), "settings": settings, }) # 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 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.scalar(jo.insert().returning(jo.c.journalid), { "userid": userid, "title": journal.title, "rating": journal.rating.code, "unixtime": arrow.now(), "settings": settings, }) # 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, 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 select_view(userid, rating, journalid, ignore=True, anyway=None): journal = _select_journal_and_check( userid, journalid, rating=rating, ignore=ignore, anyway=anyway == "true") return { 'journalid': journalid, 'userid': journal['userid'], 'username': journal['username'], 'user_media': media.get_user_media(journal['userid']), 'mine': userid == journal['userid'], 'unixtime': journal['unixtime'], 'title': journal['title'], 'content': files.read(files.make_resource(userid, journalid, 'journal/submit')), 'rating': journal['rating'], 'settings': journal['settings'], 'page_views': journal['page_views'], 'reported': report.check(journalid=journalid), 'favorited': favorite.check(userid, journalid=journalid), 'friends_only': 'f' in journal['settings'], 'hidden_submission': 'h' in journal['settings'], 'fave_count': favorite.count(journalid, 'journal'), 'tags': searchtag.select(journalid=journalid), 'comments': comment.select(userid, journalid=journalid), }
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.write(tempthumb, thumbfile) thumbextension = files.get_extension_for_category( thumbfile, macro.ART_SUBMISSION_CATEGORY) else: thumbextension = None # Write temporary submission file if submitsize: files.write(tempsubmit, submitfile) submitextension = files.get_extension_for_category( submitfile, macro.ART_SUBMISSION_CATEGORY) else: submitextension = None # Check invalid file data if not submitsize: files.clear_temporary(userid) raise WeasylError("submitSizeZero") elif submitsize > _MAIN_IMAGE_SIZE_LIMIT: 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.scalar(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, }) except PostgresError: files.clear_temporary(userid) raise # Assign search tags searchtag.associate(userid, tags, charid=charid) # Make submission file files.make_character_directory(charid) 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(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 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, macro.ART_SUBMISSION_CATEGORY) else: thumbextension = None # Write temporary submission file if submitsize: files.easyupload(tempsubmit, submitfile, "image") submitextension = files.get_extension_for_category( submitfile, macro.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.scalar(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, }) 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