def post_newcategory(name): user.login_check() connection = MongoClient('localhost', 27017) db = connection.blog categories = db.categories category = categories.find_one({'name': name}) if category != None: return ("Category already exists") if (name == ""): errors = "Category must contain name." print "Category: category contained error..returning form with errors" return (errors) else: try: category = {"name": name} categories.insert(category) except: print "Could not update the collection, error" print "Unexpected error:", sys.exc_info()[0] print "categories: added the category....redirecting...." # TODO: pretty redirect bottle.redirect("/welcome")
def show_post(permalink="notfound"): user.login_check() connection = MongoClient('localhost',27017) db = connection.blog posts = db.posts permalink = cgi.escape(permalink) # determina requisica do json path_re = re.compile(r"^([^\.]+).json$") print "about to query on permalink = ", permalink post = posts.find_one({'permalink':permalink}) if post == None: bottle.redirect("/post_not_found") print "date of entry is ", post['date'] # Formata Data post['date'] = post['date'].strftime("%A, %B %d %Y at %I:%M%p") #inicializacao dos campos do formulario para adicionar de comentarios comment = {} comment['name'] = "" comment['email'] = "" comment['body'] = "" return dict(post=post, username="******", errors="", comment=comment)
def post_newcomment(name, email, body, permalink): user.login_check() connection = MongoClient('localhost', 27017) db = connection.blog posts = db.posts permalink = cgi.escape(permalink) post = posts.find_one({'permalink': permalink}) if post == None: bottle.redirect("/post_not_found") errors = "" if (name == "" or body == ""): # Formata data post['date'] = post['date'].strftime("%A, %B %d %Y at %I:%M%p") # Inicializa Comentarios comment = {} comment['name'] = name comment['email'] = email comment['body'] = body errors = "Post must contain your name and an actual comment." print "newcomment: comment contained error..returning form with errors" #TODO: MOSTRAR ERRO # return dict(post=post, username="******", errors=errors, comment=comment)) else: comment = {} comment['author'] = name if (email != ""): comment['email'] = email comment['body'] = body try: last_error = posts.update({'permalink': permalink}, {'$push': { 'comments': comment }}, upsert=False) print "about to update a blog post with a comment" except: print "Could not update the collection, error" print "Unexpected error:", sys.exc_info()[0] print "newcomment: added the comment....redirecting to post" bottle.redirect("/post/" + permalink)
def posts_by_category(category="notfound"): user.login_check() connection = MongoClient('localhost',27017) db = connection.blog posts = db.posts category = cgi.escape(category) cursor = posts.find({'category':category}).sort('date', direction=-1).limit(10) l=[] for post in cursor: post['date'] = post['date'].strftime("%A, %B %d %Y at %I:%M%p") if ('tags' not in post): post['tags'] = [] if ('comments' not in post): post['comments'] = [] l.append({'title':post['title'], 'body':post['body'], 'post_date':post['date'],'permalink':post['permalink'],'tags':post['tags'],'author':post['author'],'comments':post['comments'], 'category':post['category']}) return dict(myposts=l,username="******")
def login(self, params, session): if params.has_key("name") and params.has_key("password"): name = params["name"] password = params["password"] if user.login_check(name, password): if self.find_user(name) == None: _user = user.get_user_by_name(name) _user.session = session self.online_users[session] = _user # Add mapping # enter hall self.hall.addSubscriber(session) return True,{"name": _user.name, "screen_name":_user.screen_name} else: return False, {"reason":"%s has been login!" % name} else: return False,{"reason":"name or password is wrong!"} else: return False,{"reason":"name and password cannot be empty!"}
def newpost(title, content, tags, category): username = user.login_check() if (title == "" or content == ""): errors="Post must contain a title and blog entry" return bottle.template("newpost_template", dict(subject=cgi.escape(title, quote=True),body=cgi.escape(content, quote=True), tags=tags, errors=errors)) # Extraindo tags tags = cgi.escape(tags) tags_array = extract_tags(tags) # Entrada de dados, insira SCAPE escaped_post = cgi.escape(content, quote=True) # substituir alguns <p> para as quebras de paragrafo newline = re.compile('\r?\n') formatted_post = newline.sub("<p>",escaped_post) return insert_entry(title, formatted_post, tags_array, username, category)
def get_posts(): username = user.login_check() connection = MongoClient('localhost',27017) db = connection.blog posts = db.posts cursor = posts.find().sort('date', direction=-1).limit(10) l=[] for postinstance in cursor: print postinstance['title'] postinstance['date'] = postinstance['date'].strftime("%A, %B %d %Y at %I:%M%p") # formatando data if ('comments' not in postinstance): postinstance['comments'] = [] if ('tags' not in postinstance): postinstance['tags'] = [] l.append({'title':postinstance['title'], 'body':postinstance['body'], 'post_date':postinstance['date'],'permalink':postinstance['permalink'],'tags':postinstance['tags'],'author':postinstance['author'],'comments':postinstance['comments'], 'category':postinstance['category']}) return dict(myposts=l,username=username)
def post_not_found(): user.login_check() return "Desculpe, post nao encontrado"
def present_newcategory(): username = user.login_check() return bottle.template("newcategory_template", dict(name="", username=username))
def present_welcome(): # check for a cookie, if present, then extract value username = user.login_check() return bottle.template("welcome_template", {'username': username})