]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blobdiff - webapp/handlers_admin.py
mirrors: Sort by distance (ascending)
[people/shoehn/ipfire.org.git] / webapp / handlers_admin.py
index 7310a43f314eb5a7de47f0757f659cf5239b1f2c..b7a3f9d1bd94b5e86d8a105ad4ea0477d04b5432 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/python
 
+import datetime
 import tornado.web
 
 from handlers_base import *
@@ -7,43 +8,7 @@ from handlers_base import *
 import backend
 
 class AdminBaseHandler(BaseHandler):
-       @property
-       def accounts(self):
-               return backend.Accounts()
-
-       @property
-       def planet(self):
-               return backend.Planet()
-
-       @property
-       def downloads(self):
-               return backend.Downloads()
-
-       def get_current_user(self):
-               return self.get_secure_cookie("account")
-
-
-class AdminLoginHandler(AdminBaseHandler):
-       def get(self):
-               self.render("admin-login.html")
-
-       def post(self):
-               account = self.accounts.search(self.get_argument("name"))
-               if not account:
-                       raise tornado.web.HTTPError(403)
-
-               if account.check_password(self.get_argument("password")):
-                       self.set_secure_cookie("account", account.uid)
-               else:
-                       raise tornado.web.HTTPError(403)
-
-               self.redirect("/")
-
-
-class AdminLogoutHandler(AdminBaseHandler):
-       def get(self):
-               self.clear_cookie("account")
-               self.redirect("/")
+       pass
 
 
 class AdminIndexHandler(AdminBaseHandler):
@@ -61,55 +26,69 @@ class AdminApiPlanetRenderMarkupHandler(AdminBaseHandler):
                output = {
                        "html" : self.planet.render(text),
                }
-               self.write(output)
-               self.finish()
+               self.finish(output)
 
 
 class AdminPlanetHandler(AdminBaseHandler):
        @tornado.web.authenticated
        def get(self):
-               entries = self.planet.get_entries(limit=100)
+               entries = self.planet.get_entries(status=None, limit=50)
 
                self.render("admin-planet.html", entries=entries)
 
 
 class AdminPlanetComposeHandler(AdminBaseHandler):
        @tornado.web.authenticated
-       def get(self, id=None):
-               entry = backend.PlanetEntry(self.planet.db)
+       def get(self, slug=None):
+               entry = None
 
-               if id:
-                       entry = self.planet.get_entry_by_id(id)
+               if slug:
+                       entry = self.planet.get_entry_by_slug(slug)
+                       if not entry:
+                               raise tornado.web.HTTPError(404)
 
                self.render("admin-planet-compose.html", entry=entry)
 
        @tornado.web.authenticated
-       def post(self, id=None):
-               id = self.get_argument("id", id)
+       def post(self):
+               title = self.get_argument("title")
+               markdown = self.get_argument("markdown")
 
-               entry = backend.PlanetEntry(self.planet.db)
+               status = self.get_argument("status", "draft")
+               assert status in ("draft", "published")
 
-               if id:
-                       entry = self.planet.get_entry_by_id(id)
+               entry = self.planet.create(title=title, markdown=markdown,
+                       author=self.current_user, status=status)
 
-               entry.set("title", self.get_argument("title"))
-               entry.set("markdown", self.get_argument("markdown"))
-               entry.set("author_id", self.current_user)
+               self.redirect("/planet")
 
-               if id:
-                       self.planet.update_entry(entry)
-               else:
-                       id = self.planet.save_entry(entry)
-                       entry.id = id
 
-               tags = self.get_argument("tags", "")
-               entry.tags = tags.split()
+class AdminPlanetEditHandler(AdminPlanetComposeHandler):
+       @tornado.web.authenticated
+       def post(self, slug):
+               entry = self.planet.get_entry_by_slug(slug)
+               if not entry:
+                       raise tornado.web.HTTPError(404)
+
+               entry.title = self.get_argument("title")
+               entry.markdown = self.get_argument("markdown")
+
+               entry.status = self.get_argument("status", "draft")
 
                self.redirect("/planet")
 
 
-class AdminPlanetEditHandler(AdminPlanetComposeHandler):
-       pass
+class AdminPlanetPublishHandler(AdminBaseHandler):
+       @tornado.web.authenticated
+       def get(self, slug):
+               entry = self.planet.get_entry_by_slug(slug)
+               if not entry:
+                       raise tornado.web.HTTPError(404)
+
+               entry.status = "published"
+               entry.published = datetime.datetime.utcnow()
+
+               self.redirect("/planet")
 
 
 class AdminAccountsBaseHandler(AdminBaseHandler):
@@ -145,15 +124,13 @@ class AdminAccountsDeleteHandler(AdminAccountsBaseHandler):
 
 
 class AdminMirrorsBaseHandler(AdminBaseHandler):
-       @property
-       def mirrors(self):
-               return backend.Mirrors()
+       pass
 
 
 class AdminMirrorsHandler(AdminMirrorsBaseHandler):
        @tornado.web.authenticated
        def get(self):
-               mirrors = self.mirrors.list()
+               mirrors = self.mirrors.get_all()
 
                self.render("admin-mirrors.html", mirrors=mirrors)
 
@@ -169,7 +146,7 @@ class AdminMirrorsCreateHandler(AdminMirrorsBaseHandler):
        @tornado.web.authenticated
        def get(self, id=None):
                if id:
-                       mirror = self.db.get("SELECT * FROM mirrors WHERE id = '%s'", int(id))
+                       mirror = self.db.get("SELECT * FROM mirrors WHERE id = %s", id)
                else:
                        mirror = tornado.database.Row(
                                id="",
@@ -248,7 +225,7 @@ class AdminNewsBaseHandler(AdminBaseHandler):
 class AdminNewsHandler(AdminNewsBaseHandler):
        @tornado.web.authenticated
        def get(self):
-               news = self.news.list()
+               news = self.news.get_all()
 
                self.render("admin-news.html", news=news)