Browse files

settings page modification

  • Loading branch information...
1 parent 68b2085 commit ceed4cbc16a6ea719eeda80c40c7679ce36be5d5 @varunpant committed Dec 28, 2012
View
66 controllers/admin_controller.py
@@ -1,4 +1,4 @@
-import web,re,base64
+import web, re, base64
from functools import wraps
from datetime import datetime
from services import shared_helper, settings_service, post_service, page_service, tag_service
@@ -16,10 +16,16 @@
global_settings = {'settings': blog_settings }
render = web.template.render('views/admin', base='layout', globals=global_settings)
-
+def assertLength(val, key, length, nullable):
+ val = val.strip()
+ if not nullable and (val is None or len(val) == 0) :
+ raise Exception("Field " + key + " cannot be empty")
+ if len(val) <= length :
+ return val
+ else:
+ raise Exception("Length of field " + key + " is larger than expected " + str(length))
def check_auth(username, password):
- print blog_settings.user_short_name
return username == blog_settings.login and password == blog_settings.password
@@ -28,10 +34,10 @@ def requires_auth(f):
def decorated(*args, **kwargs):
auth = web.ctx.env['HTTP_AUTHORIZATION'] if 'HTTP_AUTHORIZATION' in web.ctx.env else None
if auth:
- auth = re.sub('^Basic ','',auth)
- username,password = base64.decodestring(auth).split(':')
+ auth = re.sub('^Basic ', '', auth)
+ username, password = base64.decodestring(auth).split(':')
if not auth or not check_auth(username, password):
- web.header('WWW-Authenticate','Basic realm="admin"')
+ web.header('WWW-Authenticate', 'Basic realm="admin"')
web.ctx.status = '401 Unauthorized'
return f(*args, **kwargs)
@@ -91,7 +97,7 @@ def POST(self):
assert shared_helper.IsNotNull(content), "Post Content must be present"
assert shared_helper.IsNotNull(published_at), "Published At date must be present!"
except AssertionError, e:
- raise web.notfound("Error while updating post. \n error:[ %s ]"%e)
+ raise web.notfound("Error while updating post. \n error:[ %s ]" % e)
publish_date = datetime.strptime(published_at, "%B %d, %Y")
post_tags = [tag.strip() for tag in tags.split(",")]
@@ -122,7 +128,7 @@ def POST(self, postId):
assert shared_helper.IsNotNull(content), "Post Content must be present"
assert shared_helper.IsNotNull(published_at), "Published At date must be present!"
except AssertionError, e:
- raise web.notfound("Error while updating post. \n error:[ %s ]"%e)
+ raise web.notfound("Error while updating post. \n error:[ %s ]" % e)
publish_date = datetime.strptime(published_at, "%B %d, %Y")
post_tags = [tag.strip() for tag in tags.split(",")]
@@ -174,7 +180,7 @@ def POST(self):
assert shared_helper.IsNotNull(content), "Page Content must be present"
assert shared_helper.IsNotNull(published_at), "Published at date must be present!"
except AssertionError, e:
- raise web.notfound("Error while updating page. \n error:[ %s ]"%e)
+ raise web.notfound("Error while updating page. \n error:[ %s ]" % e)
publish_date = datetime.strptime(published_at, "%B %d, %Y")
@@ -188,7 +194,7 @@ def GET(self, pageId):
page = page_service.get_by_id(pageId)
return render.page(page)
- def POST(self,pageId):
+ def POST(self, pageId):
id = pageId
form = web.input()
title = form.title
@@ -201,7 +207,7 @@ def POST(self,pageId):
assert shared_helper.IsNotNull(content), "Page Content must be present"
assert shared_helper.IsNotNull(published_at), "Published at date must be present!"
except AssertionError, e:
- raise web.notfound("Error while updating page. \n error:[ %s ]"%e)
+ raise web.notfound("Error while updating page. \n error:[ %s ]" % e)
publish_date = datetime.strptime(published_at, "%B %d, %Y")
@@ -251,6 +257,42 @@ def GET(self):
class Settings:
def GET(self):
settings = settings_service.get_settings()
- return render.settings(settings)
+ return render.settings(settings, False)
+ def POST(self):
+ form = web.input()
+ settings = {}
+ try:
+ settings['bing_app_id'] = assertLength(form.bing_app_id, 'bing_app_id', 64, True)
+ settings['blog_title'] = assertLength(form.blog_title, 'blog_title', 256, False)#not null
+ settings['cache_duration_in_seconds'] = safe_number(form.cache_duration_in_seconds)
+ settings['disqus_short_name'] = assertLength(form.disqus_short_name, 'cache_duration_in_seconds', 512, True)
+ settings['disqus_url'] = assertLength(form.disqus_url, 'disqus_url', 1028, True)
+ settings['feed_burner_url'] = assertLength(form.feed_burner_url, 'feed_burner_url', 1024, True)
+ settings['google_analytics_code'] = assertLength(form.google_analytics_code, 'google_analytics_code', 24, True)
+ settings['items_per_page'] = safe_number(form.items_per_page)
+ settings['login'] = assertLength(form.login, 'login', 128, False)#not null
+ settings['password'] = assertLength(form.password, 'password', 128, False)#not null
+ settings['meta_description'] = assertLength(form.meta_description, 'meta_description', 512, True)
+ settings['meta_keywords'] = assertLength(form.meta_keywords, 'meta_keywords', 512, True)
+ settings['posts_in_home'] = safe_number(form.posts_in_home)#not null
+ if settings['posts_in_home'] < 1:
+ settings['posts_in_home'] = 10 # default to 10
+ settings['root'] = assertLength(form.root, 'root', 1028, False)#not null
+ settings['tag_line'] = assertLength(form.tag_line, 'tag_line', 256, True)
+ settings['user_bio'] = assertLength(form.user_bio, 'user_bio', 2048, True)
+ settings['user_email'] = assertLength(form.user_email, 'user_email', 256, True)
+ settings['user_short_name'] = assertLength(form.user_short_name, 'user_short_name', 256, False)#not null
+ settings['user_full_name'] = assertLength(form.user_full_name, 'user_full_name', 256, True)
+
+ updated_settings = settings_service.update_settings(settings)
+ if updated_settings is None:
+ return render.settings(form, "There was an error saving modified settings")
+ return render.settings(updated_settings, True)
+ except Exception, e:
+ return render.settings(form, e)
+
+
+
+
View
33 controllers/main_controller.py
@@ -94,8 +94,9 @@ def GET(self, page):
if count < 0:
return notfound("No archived posts were found.")
offset = start_index(page, blog_settings.items_per_page)
- posts = post_service.get_published(offset, blog_settings.items_per_page)
- page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
+ limit = blog_settings.items_per_page
+ posts = post_service.get_published(offset, limit)
+ page_count = total_page(count, limit)
nextLink = previousLink = None
if page < page_count:
nextLink = "/archives/" + str(page + 1)
@@ -115,9 +116,10 @@ def GET(self, page, year):
count = post_service.count_published()
if count < 0:
return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page)
- posts = post_service.find_published_by_year(year, offset, blog_settings.items_per_page)
- page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
+ offset = start_index(page, blog_settings.items_per_page)
+ limit = blog_settings.items_per_page
+ posts = post_service.find_published_by_year(year, offset, limit)
+ page_count = total_page(count,limit)
nextLink = previousLink = None
if page < page_count:
nextLink = "/archives/" + str(page + 1)
@@ -139,9 +141,10 @@ def GET(self, page, year, month):
count = post_service.count_published()
if count < 0:
return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page)
- posts = post_service.find_published_by_year_and_month(safe_year, safe_month, offset, blog_settings.items_per_page)
- page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
+ offset = start_index(page, blog_settings.items_per_page)
+ limit = blog_settings.items_per_page
+ posts = post_service.find_published_by_year_and_month(safe_year, safe_month, offset, limit)
+ page_count = total_page(count, limit)
nextLink = previousLink = None
if page < page_count:
nextLink = "/archives/" + str(page + 1)
@@ -166,9 +169,10 @@ def GET(self, page, year, month, day):
count = post_service.count_published()
if count < 0:
return notfound("No archived posts were found.")
- offset = start_index(page, blog_settings.items_per_page)
- posts = post_service.find_published_by_year_month_and_day(safe_year, safe_month, safe_day, offset, blog_settings.items_per_page)
- page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
+ offset = start_index(page, blog_settings.items_per_page)
+ limit = blog_settings.items_per_page
+ posts = post_service.find_published_by_year_month_and_day(safe_year, safe_month, safe_day, offset,limit)
+ page_count = total_page(count, limit)
nextLink = previousLink = None
if page < page_count:
nextLink = "/archives/" + str(page + 1)
@@ -213,14 +217,15 @@ class Search:
def GET(self):
q = web.input().q
page = safe_number(web.input(page="1").page)
- offset = start_index(page, blog_settings.items_per_page)
+ offset = start_index(page, blog_settings.items_per_page)
+ limit = blog_settings.items_per_page
count = safe_number(search_service.getCount(q)[0])
result = []
msg = None
nextLink = previousLink = None
if count > 0:
- result = search_service.search(q, offset, blog_settings.items_per_page)
- page_count = total_page(count - blog_settings.posts_in_home, blog_settings.items_per_page)
+ result = search_service.search(q, offset, limit)
+ page_count = total_page(count, limit)
if page < page_count:
nextLink = "/search?q=%s&page=%s" % (q, str(page + 1))
if page > 1 :
View
BIN db/app.db
Binary file not shown.
View
BIN db/app.db.tar.gz
Binary file not shown.
View
41 services/settings_service.py
@@ -1,7 +1,7 @@
###############################################################################
# Settings Querying Service #######################################################
###############################################################################
-from models import getSession,Setting
+from models import getSession , Setting
class CacheFetcher:
@@ -23,4 +23,41 @@ def get_settings():
return CacheFetcher().fetch()
def reset():
- CacheFetcher().clear()
+ CacheFetcher().clear()
+
+def update_settings(settings):
+ session = getSession()
+ current_settings = session.query(Setting).scalar()
+
+ current_settings.blog_title = settings["blog_title"]
+ current_settings.tag_line = settings["tag_line"]
+ current_settings.meta_keywords = settings["meta_keywords"]
+ current_settings.meta_description = settings["meta_description"]
+ current_settings.items_per_page = settings["items_per_page"]
+ current_settings.posts_in_home = settings["posts_in_home"]
+ current_settings.cache_duration_in_seconds = settings["cache_duration_in_seconds"]
+ current_settings.login = settings["login"]
+ current_settings.password = settings["password"]
+ current_settings.user_full_name = settings["user_full_name"]
+ current_settings.user_email = settings["user_email"]
+ current_settings.user_bio = settings["user_bio"]
+ current_settings.user_short_name = settings["user_short_name"]
+ current_settings.root = settings["root"]
+ current_settings.google_analytics_code = settings["google_analytics_code"]
+ current_settings.feed_burner_url = settings["feed_burner_url"]
+ current_settings.bing_app_id = settings["bing_app_id"]
+ current_settings.disqus_short_name = settings["disqus_short_name"]
+ current_settings.disqus_url = settings["disqus_url"]
+ try:
+ session.add(current_settings)
+ session.commit()
+ reset();
+ return get_settings()
+ except Exception, e:
+ print e
+ return None
+ pass
+
+
+
+
View
10 tests/settings_service.py
@@ -80,6 +80,16 @@ def test_get_settings(self):
for k, v in vars(settings).items():
if k in self.enteries:
self.assertEqual(self.enteries[k], v, 'one of the entries did not match' + str(v))
+
+ def test_update_settings(self):
+ self.enteries["blog_title"] = "updated blog settings"
+ settings = self.service.update_settings(self.enteries)
+ self.assertIsNotNone(settings, "Failed to retrive settings object")
+ for k, v in vars(settings).items():
+ if k in self.enteries:
+ self.assertEqual(self.enteries[k], v, 'one of the entries did not match' + str(v))
+
+
if __name__ == '__main__':
unittest.main()
View
22 views/admin/settings.html
@@ -1,8 +1,13 @@
-$def with (settings)
+$def with (settings,msg)
$var title ="Blog Settings"
-<div class="span12">
+ <div class="span12">
+ $if msg == True:
+ <div class="alert alert-success">Settings successfully updated</div>
+ $elif msg is not False:
+ <div class="alert alert-error">$msg</div>
- <form id="edit-profile" class="form-horizontal">
+
+ <form id="edit-profile" class="form-horizontal" method="POST">
<div class="row">
<div class="span6">
<legend>
@@ -90,7 +95,7 @@
<div class="control-group">
<label class="control-label" for="user_bio">Biography</label>
<div class="controls">
- <textarea tabindex="6" class="span4" name="user_bio" id="user_bio" rows="10" cols="100" maxlength ="2048" placeholder = " Bio..." value = "$:settings.user_bio" ></textarea>
+ <textarea tabindex="6" class="span4" name="user_bio" id="user_bio" rows="10" cols="100" maxlength ="2048" placeholder = " Bio..." >$:settings.user_bio</textarea>
</div>
</div>
</div>
@@ -122,7 +127,7 @@
<div class="control-group">
<label class="control-label" for="bing_app_id">App Id</label>
<div class="controls">
- <input type="text" class="span2" name="bing_app_id" id="bing_app_id" maxlength = "64" placeholder="App Id..." value = "$:settings.bing_app_id" required/>
+ <input type="text" class="span2" name="bing_app_id" id="bing_app_id" maxlength = "64" placeholder="App Id..." value = "$:settings.bing_app_id" />
</div>
</div>
@@ -144,7 +149,12 @@
<label class="control-label" for="disqus_short_name">Short name</label>
<div class="controls">
<input type="text" class="span2" name="disqus_short_name" id="disqus_short_name" maxlength = "256" placeholder="Short name..." value = "$:settings.disqus_short_name" />
-
+ </div>
+ </div>
+ <div class="control-group">
+ <label class="control-label" for="disqus_url">Discuss Url</label>
+ <div class="controls">
+ <input type="text" class="span2" name="disqus_url" id="disqus_url" maxlength = "256" placeholder="Discuss Url..." value = "$:settings.disqus_url" />
</div>
</div>
View
2 views/themes/light/base.html
@@ -55,7 +55,7 @@
<footer>
<div class="container first span14">
<p>
- © 2012. All rights reserved.
+ © 2013. All rights reserved.
</p>
<p>
<a href="https://twitter.com/" class="twitter-follow-button" data-show-count="false">Follow @bhaguli</a>

0 comments on commit ceed4cb

Please sign in to comment.