def settingsPost(self, **kwargs): form_data = cherrypy.request.body.params validation = Schema({ Required('site_title'): All(str, Length(max=1000)), Required('about_picture'): All(str, Length(max=1000)), Required('about_text'): All(str, Length(max=5000)) }) try: validation(form_data) if not admin.AdminModel().updateKey("site_title", form_data["site_title"]): raise Invalid("Unable to add the user to the database.") if not admin.AdminModel().updateKey("about_picture", form_data["about_picture"]): raise Invalid("Unable to update the picture in the database.") if not admin.AdminModel().updateKey("about_text", form_data["about_text"]): raise Invalid( "Unable to update the about text in the database.") except Exception as err: return self.render("admin/settings.html", error=str(err)) # Key was changed successfully raise cherrypy.HTTPRedirect("/admin/settings")
def index(self): # If no username or password is set on first login # prompt the user to set one for the account. if admin.AdminModel().userExists() == False: return self.render("setup.html") raise cherrypy.HTTPRedirect("/admin")
def login(realm, username, password): row = admin.AdminModel().getUser(username) try: verify = row.fetchone()[1] if verify == admin.AdminModel.computeHash(password): return True except Exception as err: return False
def render(self, page, **kwargs): menu = {"Home": "/", "Blog": "/blog"} # Get site header from the database header = admin.AdminModel().getKey('site_title') pic = admin.AdminModel().getKey('about_picture') text = admin.AdminModel().getKey('about_text') keys = { "template": page, "menu": menu, "header": header, "about_picture": pic, "about_text": text } for key in kwargs: keys[key] = kwargs[key] tmpl = self._env.get_template('partials/front.html') return tmpl.render(keys)
def settings(self): # Get the site title title = admin.AdminModel().getKey("site_title") picture = admin.AdminModel().getKey("about_picture") text = admin.AdminModel().getKey("about_text") # Get all pictures in upload dir... # Might want to find a better way to do this later uploads = list() os.chdir("public/uploads") for file in glob.glob("*.*"): uploads.append(file) # Make sure to change back to the # proper file location or get ready to 500. os.chdir("../..") return self.render("admin/settings.html", site_title=title, about_picture=picture, pictures=uploads, about_text=text)
def register(self, **kwargs): form_data = cherrypy.request.body.params; # Block creation of any extra users beyond the # first one via this route. if admin.AdminModel().userExists() == True: raise cherrypy.HTTPRedirect("/admin") validation = Schema({ Required('username'): All(str, Length(min=4)), Required('password'): All(str, Length(min=8)) }) try: validation(form_data) if (" " in form_data["username"]) == True: raise Invalid("Username must not contain any spaces.") if not admin.AdminModel().addUser(form_data["username"], form_data["password"]): raise Invalid("Unable to add the user to the database.") except Exception as err: return self.render("setup.html", error=str(err)) # Setup was complete successfully redirect to admin raise cherrypy.HTTPRedirect("/admin")