def comment(username=None,art=None,journal=None,commentID=None): if request.method == 'GET': commentResult = db.db.comments.find_one({"_id" : commentID}) if not commentResult: abort(404) return render_template("comment.html", comment=commentResult) else: if g.loggedInUser: # Filter out broken or incomplete comments if "parent" not in request.form or "commentMap" not in request.form or "content" not in request.form: abort(500) if len(request.form["content"]) < config.minimumCommentLengthInCharacters: return "0" parent = request.form["parent"] commentMap = request.form["commentMap"] # Comment Reply if parent != "" and commentMap != "": try: parent = int(request.form["parent"]) commentMap = util.parseCommentMap(request.form["commentMap"]) except ValueError: abort(500) db.db.comments.update( { "_id" : parent }, { "$push" : { commentMap : { "authorID" : g.loggedInUser["_id"] , "author" : g.loggedInUser["username"] , "content" : request.form["content"] , "codeContent" : usercode.parse(request.form["content"]) , "r" : [] , "date" : datetime.datetime.today() } } }) return "1" # Top Level Comment else: if username: location = "u" userLookup = db.db.users.find_one({"lowername" : username.lower()}) if userLookup: home = userLookup["_id"] else: abort(500) elif art: location = "a" home = art elif journal: location = "j" home = journal else : abort(500) key = db.nextKey("comments") db.db.comments.insert({ "_id" : key , "authorID" : g.loggedInUser["_id"] , "author" : g.loggedInUser["username"] , "content" : request.form["content"] , "codeContent" : usercode.parse(request.form["content"]) , "r" : [] , "home" : home , "homeType" : location , "date" : datetime.datetime.today() }) return "1" else: abort(401)
def comment(username=None,art=None,journal=None,commentID=None): """Handles posting and getting comments and comment threads.""" if request.method == 'GET': comment_result = comments_model.get_one({"_id" : commentID}) if not comment_result: abort(404) return render_template("comment.html", comment=comment_result) else: if g.logged_in_user: # Filter out broken or incomplete comments if "parent" not in request.form or "comment_map" not in request.form or "content" not in request.form: abort(500) if len(request.form["content"]) < config.minimum_comment_length_in_characters: return "0" parent = request.form["parent"] comment_map = request.form["comment_map"] # Comment Reply if parent != "" and comment_map != "": try: parent = int(request.form["parent"]) comment_map = util.parse_comment_map(request.form["comment_map"]) except ValueError: abort(500) comments_model.update( { "_id" : parent }, { comment_map : { "author_ID" : g.logged_in_user["_id"] , "author" : g.logged_in_user["username"] , "content" : request.form["content"] , "code_content" : usercode.parse(request.form["content"]) , "r" : [] , "date" : datetime.datetime.today() } } , "$push") return "1" # Top Level Comment else: if username: location = "u" user_lookup = users_model.get_one({"lowername" : username.lower()}) if user_lookup: home = user_lookup["_id"] else: abort(500) elif art: location = "a" home = art elif journal: location = "j" home = journal else: abort(500) comments_model.insert({ "author_ID" : g.logged_in_user["_id"] , "author" : g.logged_in_user["username"] , "content" : request.form["content"] , "code_content" : usercode.parse(request.form["content"]) , "r" : [] , "home" : home , "home_type" : location , "date" : datetime.datetime.today() }) return "1" else: abort(401)
def manage_journal(): """ Renders a page for posting a new journal, with links to edit past journals. Handles the posting of new journals. """ if g.logged_in_user: if request.method == 'GET': all_journals = journals_model.get({"author_ID" : g.logged_in_user['_id'] }).sort("_id",-1) return render_template("manage_journals.html", all_journals = all_journals) else: if not "journal_title" in request.form or not "journal_content" in request.form or not "journal_mood" in request.form: abort(500) if request.form["journal_title"].strip() == "": return "0" # ERROR: key = keys_model.next("journals") journals_model.insert({ "_id" : key , "title" : request.form["journal_title"] , "content" : request.form["journal_content"] , "code_content" : usercode.parse(request.form["journal_content"]) , "mood" : request.form["journal_mood"] , "author" : g.logged_in_user["username"] , "author_ID" : g.logged_in_user["_id"] , "views" : 0 , "date" : datetime.datetime.today() }) return redirect(url_for('view_journal',journal=key)) else: abort(401)
def submitArt(): if request.method == 'GET': if g.loggedInUser: return render_template("submit.html") else: abort(401) else: if not g.loggedInUser: abort(401) messages = [] # Image if request.form["artType"] == "image": image = request.files['upload'] if not util.allowedFile(image.filename, config.imageExtensions): flash(config.fileTypeError + "The allowed filetypes are " + util.printList(config.imageExtensions) + ". ") elif image.content_length >= config.maxImageSize: flash(config.fileSizeError + "Your image must be at most " + config.maxImageSizeText + ". ") elif not ("title" in request.form and "description" in request.form) : abort(500) elif not request.form["title"]: flash("Your title must not be left blank. ") else: fileType = util.fileType(request.files['upload'].filename) key = db.nextKey("art") db.db.art.insert({ "_id" : key , "title" : request.form["title"] , "description" : request.form["description"] , "codeDesc" : usercode.parse(request.form["description"]) , "author" : g.loggedInUser["username"] , "authorID" : g.loggedInUser["_id"] , "keywords" : filter (lambda keyword: not keyword in ignoredKeywords.commonWords, map(lambda keyword: keyword.lower() , request.form["title"].split() ) ) , "mature" : False , "folder" : "complete" , "favorites" : [] , "favAmount" : 0 , "views" : 0 , "date" : datetime.datetime.today() , "filetype" : fileType , "type" : "image" }) fileLocation = os.path.join(config.artDir, str(key) + "." + fileType) image.save(fileLocation) storage.push(fileLocation, fileLocation) autocrop(key) return redirect(url_for('crop',art=key)) # Audio # Literature # Craft # Cullinary # Performance return redirect(url_for('submitArt'))
def manageJournal(): if g.loggedInUser: if request.method == 'GET': return render_template("manageJournals.html") else: if not "journalTitle" in request.form or not "journalContent" in request.form: abort(500) key = db.nextKey("journals") db.db.journals.insert({ "_id" : key , "title" : request.form["journalTitle"] , "content" : request.form["journalContent"] , "codeContent" : usercode.parse(request.form["journalContent"]) , "mood" : request.form["journalMood"] , "author" : g.loggedInUser["username"] , "authorID" : g.loggedInUser["_id"] , "views" : 0 , "date" : datetime.datetime.today() }) return redirect(url_for('viewJournal',journal=key)) else: abort(401)
def parseUsercode(text): return usercode.parse(text)
def settings(): if request.method == 'GET': if g.loggedInUser: return render_template("settings.html") else: abort(401) elif request.method == 'POST': if g.loggedInUser: # User Icon icon = request.files['iconUpload'] if icon and util.allowedFile(icon.filename,config.iconExtensions): try: os.remove(os.path.join(app.config["iconsDir"], g.loggedInUser['lowername'] + "." + g.loggedInUser["icon"])) except: if config.logging: logging.warning("Error: Couldn't remove user \"" + g.loggedInUser['username']+ "\"'s old icon while attempting to upload a new icon. ") fileName = g.loggedInUser['lowername'] + "." + util.fileType(icon.filename) fileLocation = os.path.join(app.config["iconsDir"], fileName) db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"icon": util.fileType(fileName) }}) icon.save(fileLocation) image = Image.open(fileLocation) resized = image.resize(config.iconSize, Image.ANTIALIAS) try: resized.save(fileLocation, quality=100) except: if config.logging: logging.warning("Error: Couldn't save user \"" + g.loggedInUser['username'] + "\"'s new icon while attempting to upload a new icon. ") # Password if request.form["changePassCurrent"] and request.form["changePassNew1"] and request.form["changePassNew2"]: if system.cryptography.encryptPassword(request.form["changePassCurrent"], True) == g.loggedInUser['password']: if request.form["changePassNew1"] == request.form["changePassNew2"]: hashed = system.cryptography.encryptPassword(request.form['changePassNew1'], True) db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"password": hashed}}) session['password']=hashed # Gender if request.form["changeGender"] != g.loggedInUser["gender"]: db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"gender": request.form["changeGender"] }}) # Profile if request.form["changeProfile"] != g.loggedInUser["profile"]: db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"profile": request.form["changeProfile"], "codeProfile": usercode.parse(request.form["changeProfile"]) } }) # Color Theme if request.form["changeColorTheme"] != g.loggedInUser["theme"]: db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"theme": request.form["changeColorTheme"]} }) return "1" else: abort(401)
def settings(): if request.method == 'GET': if g.loggedInUser: if config.betaKey: betaKeys = db.db.betaPass.find({"owner" : g.loggedInUser["username"] }) else: betaKeys = None return render_template("settings.html", betaKeys = betaKeys) else: abort(401) elif request.method == 'POST': if g.loggedInUser: # User Messages messages = [] # User Icon icon = request.files['iconUpload'] if icon: if not icon.content_length <= config.maxIconSize: flash(config.fileSizeError + "Your icon must be at most " + config.maxIconSizeText + ". ") else: if not util.allowedFile(icon.filename,config.iconExtensions): flash(config.fileTypeError + "The allowed extensions are " + util.printList(config.iconExtensions) + ". ") else: try: os.remove(os.path.join(config.iconsDir, g.loggedInUser['lowername'] + "." + g.loggedInUser["icon"])) except: if config.logging: logging.warning("Couldn't remove user \"" + g.loggedInUser['username']+ "\"'s old icon while attempting to upload a new icon. ") fileName = g.loggedInUser['lowername'] fileType = util.fileType(icon.filename) if fileType.lower() == "jpg": fileType = "jpeg" # Change filetype for PIL (mimetype,i) = mimetypes.guess_type(icon.filename) fileLocation = os.path.join(config.iconsDir, fileName) db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"icon": fileType } } ) icon.save(fileLocation) image = Image.open(fileLocation) resized = image.resize(config.iconSize, Image.ANTIALIAS) resized.save(fileLocation, fileType, quality=100) storage.push(fileLocation, fileLocation, mimetype = mimetype ) messages.append("User Icon") # Password if request.form["changePassCurrent"] and request.form["changePassNew1"] and request.form["changePassNew2"]: if system.cryptography.encryptPassword(request.form["changePassCurrent"], True) != g.loggedInUser['password']: flash("The new password you gave didn't match the one in the database! ):") elif request.form["changePassNew1"] != request.form["changePassNew2"]: flash("The new passwords you gave don't match! Try retyping them carefully. ") else: hashed = system.cryptography.encryptPassword(request.form['changePassNew1'], True) db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"password": hashed}}) session['password']=hashed messages.append("Password") # Gender if request.form["changeGender"] != g.loggedInUser["gender"]: db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"gender": request.form["changeGender"] }}) messages.append("Gender") # Location if request.form["changeLatitude"] != str(g.loggedInUser["latitude"]) or request.form["changeLongitude"] != str(g.loggedInUser["longitude"]): try: latFloat = float(request.form["changeLatitude"]) lonFloat = float(request.form["changeLongitude"]) db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"latitude": latFloat, "longitude": lonFloat } } ) messages.append("Location") except ValueError: flash("The locations you gave were invalid latitude and longitude coordinates! ): ") # Profile if request.form["changeProfile"] != g.loggedInUser["profile"]: db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"profile": request.form["changeProfile"], "codeProfile": usercode.parse(request.form["changeProfile"]) } }) messages.append("Profile") # Color Theme if request.form["changeColorTheme"] != g.loggedInUser["theme"]: db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": {"theme": request.form["changeColorTheme"]} }) messages.append("Color Theme") # Layout l1 = util.urlDecode(request.form["changeLayout"]) l2 = util.urlDecode(request.form["changeLayoutOrder"]) for key in l2: l2[key] = int(l2[key]) # Converts orderings to integers layout = util.concDictValues(l1,l2) if not util.compareDicts(layout, g.loggedInUser["layout"]): if util.compareDictKeys(layout, g.loggedInUser["layout"]): layoutToPush = {} for key in layout: layoutToPush["layout." + key] = layout[key] db.db.users.update({"lowername": g.loggedInUser['lowername']}, {"$set": layoutToPush }) messages.append("Layout") return render_template("settingsSuccess.html",messages=messages,len=len) else: abort(401)
def parse_usercode(text): """Given usercode text, parses it into HTML and returns it.""" return usercode.parse(text)
def edit_journal(journal): """Edits a specific journal given its ID""" if g.logged_in_user: journal_result = journals_model.get_one({"_id" : journal }) if not journal_result: abort(404) if g.logged_in_user["_id"] != journal_result['author_ID']: abort(401) if request.method == 'GET': all_journals = journals_model.get({"author_ID" : g.logged_in_user['_id'] }).sort("_id",-1) return render_template("edit_journal.html", journal=journal_result, all_journals=all_journals) elif request.method == 'POST': if "journal_title" in request.form and "journal_content" in request.form and "journal_mood" in request.form: if request.form["journal_title"].strip() != "": journals_model.update({"_id": journal}, {"title": request.form["journal_title"], "content": request.form["journal_content"], "code_content": usercode.parse(request.form["journal_content"]), "mood": request.form["journal_mood"] }) flash("Journal edited successfully! ") return redirect(url_for('view_journal', journal = journal) ) else: abort(401)
def submit_art(): """Displays the artwork submission page and handles the submission of artworks.""" if g.logged_in_user: if request.method == 'GET': return render_template("submit.html") else: # Form Validation if not ("title" in request.form and "description" in request.form) : abort(500) elif not request.form["title"]: flash("Your title must not be left blank. ") art = request.files['upload'] art_type = None # Image if request.form["art_type"] == "image": if not util.allowed_file(art.filename, config.image_extensions): flash(config.file_type_error + "The allowed filetypes are " + util.print_list(config.image_extensions) + ". ") elif art.content_length >= config.max_image_size: flash(config.file_size_error + "Your image must be at most " + config.max_image_size_text + ". ") else: art_type = "image" # Audio # Literature # Animation if request.form["art_type"] == "animation": if not util.allowed_file(art.filename, config.animation_extensions): flash(config.file_type_error + "Your animation must be a " + util.print_list(config.animation_extensions) + " file. ") elif art.content_length >= config.max_animation_size: flash(config.file_size_error + "Your .swf file must be at most " + config.max_image_size_text + ". ") else: art_type = "animation" # Craft # Cullinary # Performance if art_type: fileType = util.fileType(art.filename) key = keys_model.next("art") file_location = os.path.join(config.art_dir, str(key) + "." + fileType) art.save(file_location) storage.push(file_location, file_location) art_model.insert({ "_id" : key , "title" : request.form["title"] , "description" : request.form["description"] , "code_desc" : usercode.parse(request.form["description"]) , "author" : g.logged_in_user["username"] , "author_ID" : g.logged_in_user["_id"] , "keywords" : filter (lambda keyword: not keyword in ignored_keywords.commonWords, map(lambda keyword: keyword.lower() , request.form["title"].split() ) ) , "mature" : False , "folder" : "complete" , "favorites" : [] , "fav_amount" : 0 , "views" : 0 , "date" : datetime.datetime.today() , "filetype" : fileType , "type" : art_type }) if type == "image": autocrop(key) return redirect(url_for('crop',art=key)) else: return redirect(url_for('view_art',art=key)) return redirect(url_for('submit_art')) return redirect(url_for('submit_art')) else: abort(401)
def settings(): """The user's settings page. Used for changing their profile, account settings, and homepage.""" if g.logged_in_user: if request.method == 'GET': if config.beta_key: beta_keys = beta_pass_model.get({"owner" : g.logged_in_user["username"] }) else: beta_keys = None return render_template("settings.html", beta_keys = beta_keys) elif request.method == 'POST': # User Messages messages = [] # User Icon icon = request.files['icon_upload'] if icon: print icon.content_length if not icon.content_length <= config.max_icon_size: flash(config.file_size_error + "Your icon must be at most " + config.max_icon_size_text + ". ") else: if not util.allowed_file(icon.filename,config.icon_extensions): flash(config.file_type_error + "The allowed extensions are " + util.print_list(config.icon_extensions) + ". ") else: try: os.remove(os.path.join(config.icons_dir, g.logged_in_user['lowername'] + "." + g.logged_in_user["icon"])) except: if config.logging: logging.warning("Couldn't remove user \"" + g.logged_in_user['username']+ "\"'s old icon while attempting to upload a new icon. ") fileName = g.logged_in_user['lowername'] fileType = util.fileType(icon.filename) if fileType.lower() == "jpg": fileType = "jpeg" # Change filetype for PIL (mimetype,i) = mimetypes.guess_type(icon.filename) file_location = os.path.join(config.icons_dir, fileName) users_model.update({"lowername": g.logged_in_user['lowername']}, {"icon": fileType} ) icon.save(file_location) image = Image.open(file_location) resized = image.resize(config.icon_size, Image.ANTIALIAS) resized.save(file_location, fileType, quality=100) storage.push(file_location, file_location, mimetype = mimetype ) messages.append("User Icon") # Password if request.form["change_pass_current"] and request.form["change_pass_new_1"] and request.form["change_pass_new_2"]: if cryptography.encrypt_password(request.form["change_pass_current"], True) != g.logged_in_user['password']: flash("The new password you gave didn't match the one in the database! ):") elif request.form["change_pass_new_1"] != request.form["change_pass_new_2"]: flash("The new passwords you gave don't match! Try retyping them carefully. ") else: hashed = cryptography.encrypt_password(request.form['change_pass_new_1'], True) users_model.update({"_id": g.logged_in_user['_id']}, {"password": hashed} ) session['password']=hashed messages.append("Password") # Gender if request.form["change_gender"] != g.logged_in_user["gender"]: users_model.update({"_id": g.logged_in_user['_id']}, {"gender": request.form["change_gender"] }) messages.append("Gender") # Location if request.form["change_latitude"] != str(g.logged_in_user["latitude"]) or request.form["change_longitude"] != str(g.logged_in_user["longitude"]): try: latFloat = float(request.form["change_latitude"]) lonFloat = float(request.form["change_longitude"]) users_model.update({"_id": g.logged_in_user['_id']}, {"latitude": latFloat, "longitude": lonFloat } ) messages.append("Location") except ValueError: flash("The locations you gave were invalid latitude and longitude coordinates! ): ") # Profile if request.form["change_profile"] != g.logged_in_user["profile"]: users_model.update({"_id": g.logged_in_user['_id']}, {"profile": request.form["change_profile"], "code_profile": usercode.parse(request.form["change_profile"]) }) messages.append("Profile") # Color Theme if request.form["change_color_theme"] != g.logged_in_user["theme"]: users_model.update({"_id": g.logged_in_user['_id']}, {"theme": request.form["change_color_theme"]} ) messages.append("Color Theme") # Layout l1 = util.url_decode(request.form["change_layout"]) l2 = util.url_decode(request.form["change_layout_order"]) for key in l2: l2[key] = int(l2[key]) # Converts orderings to integers layout = util.conc_dict_values(l1,l2) if not util.compare_dicts(layout, g.logged_in_user["layout"]): if util.compare_dict_keys(layout, g.logged_in_user["layout"]): layout_to_push = {} for key in layout: layout_to_push["layout." + key] = layout[key] users_model.update({"_id": g.logged_in_user['_id']}, layout_to_push) messages.append("Layout") return render_template("settings_success.html",messages=messages,len=len) else: abort(401)