annotate pylons_app/controllers/admin.py @ 127:20dc7a5eb748

Html changes and cleanups, made folders for html templates, implemented tags and branches pages
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 03 May 2010 19:07:54 +0200
parents 2811259dc12d
children 919b5bcd8630
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
1 import logging
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
2
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
3 from pylons import request, response, session, tmpl_context as c, url, app_globals as g
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
4 from pylons.controllers.util import abort, redirect
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
5
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
6 from pylons_app.lib.base import BaseController, render
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
7 import os
44
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
8 from pylons_app.lib import auth
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
9 from pylons_app.model.forms import LoginForm
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
10 import formencode
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
11 import formencode.htmlfill as htmlfill
62
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
12 from pylons_app.model import meta
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
13 from pylons_app.model.db import Users, UserLogs
78
6f524697f79d Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents: 75
diff changeset
14 from webhelpers.paginate import Page
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
15 from pylons_app.lib.utils import check_repo
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
16 log = logging.getLogger(__name__)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
17
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
18 class AdminController(BaseController):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
19
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
20 def __before__(self):
101
8b06c420491d statics moved to pylons.
Marcin Kuzminski <marcin@python-works.com>
parents: 79
diff changeset
21
52
25e516447a33 implemented autentication
marcink
parents: 47
diff changeset
22 c.admin_user = session.get('admin_user', False)
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
23 c.admin_username = session.get('admin_username')
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
24
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
25 def index(self):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
26 # Return a rendered template
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
27 if request.POST:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
28 #import Login Form validator class
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
29 login_form = LoginForm()
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
30
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
31 try:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
32 c.form_result = login_form.to_python(dict(request.params))
46
9db7782727b3 Static files for production fixed
Marcin Kuzminski <marcin@python-blog.com>
parents: 45
diff changeset
33 if auth.admin_auth(c.form_result['username'], c.form_result['password']):
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
34 session['admin_user'] = True
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
35 session['admin_username'] = c.form_result['username']
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
36 session.save()
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
37 return redirect(url('admin_home'))
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
38 else:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
39 raise formencode.Invalid('Login Error', None, None,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
40 error_dict={'username':'invalid login',
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
41 'password':'invalid password'})
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
42
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
43 except formencode.Invalid, error:
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
44 c.form_result = error.value
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
45 c.form_errors = error.error_dict or {}
127
20dc7a5eb748 Html changes and cleanups, made folders for html templates, implemented tags and branches pages
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
46 html = render('admin/admin.html')
45
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
47
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
48 return htmlfill.render(
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
49 html,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
50 defaults=c.form_result,
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
51 encoding="UTF-8"
a886f5eba757 implemented admin page login
marcink
parents: 44
diff changeset
52 )
62
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
53 if c.admin_user:
4df4c0eac619 Updated admin to show last 5 actions + updated db model
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
54 sa = meta.Session
78
6f524697f79d Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents: 75
diff changeset
55
6f524697f79d Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents: 75
diff changeset
56 users_log = sa.query(UserLogs)\
6f524697f79d Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents: 75
diff changeset
57 .order_by(UserLogs.action_date.desc())
6f524697f79d Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents: 75
diff changeset
58 p = int(request.params.get('page', 1))
6f524697f79d Implemented paging to admin user acion log
Marcin Kuzminski <marcin@python-blog.com>
parents: 75
diff changeset
59 c.users_log = Page(users_log, page=p, items_per_page=10)
127
20dc7a5eb748 Html changes and cleanups, made folders for html templates, implemented tags and branches pages
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
60 c.log_data = render('admin/admin_log.html')
79
9fe23fdab9e9 Implemented AJAH paging
Marcin Kuzminski <marcin@python-blog.com>
parents: 78
diff changeset
61 if request.params.get('partial'):
9fe23fdab9e9 Implemented AJAH paging
Marcin Kuzminski <marcin@python-blog.com>
parents: 78
diff changeset
62 return c.log_data
127
20dc7a5eb748 Html changes and cleanups, made folders for html templates, implemented tags and branches pages
Marcin Kuzminski <marcin@python-works.com>
parents: 125
diff changeset
63 return render('admin/admin.html')
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
64
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
65 def hgrc(self, dirname):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
66 filename = os.path.join(dirname, '.hg', 'hgrc')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
67 return filename
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
68
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
69 def add_repo(self, new_repo):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
70
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
71
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
72 #extra check it can be add since it's the command
44
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
73 if new_repo == '_admin':
d924b931b488 Added managment pages.
marcink
parents: 43
diff changeset
74 c.msg = 'DENIED'
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
75 c.new_repo = ''
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
76 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
77
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
78 new_repo = new_repo.replace(" ", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
79 new_repo = new_repo.replace("-", "_")
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
80
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
81 try:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
82 self._create_repo(new_repo)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
83 c.new_repo = new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
84 c.msg = 'added repo'
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
85 except Exception as e:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
86 c.new_repo = 'Exception when adding: %s' % new_repo
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
87 c.msg = str(e)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
88
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
89 return render('add.html')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
90
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
91
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
92 def _create_repo(self, repo_name):
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
93 if repo_name in [None, '', 'add']:
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
94 raise Exception('undefined repo_name of repo')
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
95
125
2811259dc12d Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
96 if check_repo(repo_name, g.base_path):
43
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
97 log.info('creating repo %s in %s', repo_name, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
98 cmd = """mkdir %s && hg init %s""" \
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
99 % (self.repo_path, self.repo_path)
2e1247e62c5b changed for pylons 0.1 / 1.0
marcink
parents:
diff changeset
100 os.popen(cmd)