~yeliabmas/sloecode/sloecode-template

« back to all changes in this revision

Viewing changes to sloecode/controllers/admin/wiki.py

  • Committer: Sam Bailey
  • Date: 2012-10-30 03:45:30 UTC
  • Revision ID: yeliabmas@gmail.com-20121030034530-1obbt428wynab8lh
Tidied up most of the page template interfaces.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import formencode
20
20
import formencode.htmlfill
21
 
import urllib
22
21
 
23
22
from pylons import request, config
24
23
from pylons.controllers.util import abort, redirect
39
38
class WikiController(BaseController):
40
39
    """Controller for wiki related operations."""
41
40
 
 
41
    def create_page_template(self):
 
42
        return self.render('admin/template-new.html')
 
43
 
 
44
    def process_create_page_template(self):
 
45
        schema = TemplateSchema()
 
46
        try:
 
47
            form_result = schema.to_python(request.params)
 
48
        except formencode.Invalid, e:
 
49
            errors = e.unpack_errors()
 
50
            return formencode.htmlfill.render(self.create_page_template(), defaults=request.params, errors=errors)
 
51
        filename = form_result['name']
 
52
        save_location = form_result['save_location']
 
53
        content = form_result['content']
 
54
        description = form_result['description']
 
55
        old_name = form_result['old_name']
 
56
 
 
57
        return redirect(h.url(controller="admin/wiki",
 
58
                              action="save_page_template",
 
59
                              filename=filename,
 
60
                              name=filename,
 
61
                              save_location=save_location,
 
62
                              content=content,
 
63
                              description=description,
 
64
                              old_name=old_name))
 
65
 
42
66
    def delete_page_template(self, filename):
43
67
        """Deletes a page template."""
44
 
        template = Template.get_one(filename=filename)
 
68
        template = Template.get_one(name=filename)
45
69
        if template is not None:
46
70
            Session.delete(template)
47
71
            Session.commit()
48
 
        return redirect(h.url(controller='admin/wiki', action='list_templates'))
 
72
        return redirect(h.url(controller='admin/wiki', action='list_page_templates'))
49
73
 
50
 
    def edit_template(self, filename):
 
74
    def edit_page_template(self, filename):
51
75
        """Edit page templates."""
52
76
        existing_template = Template.get_one(name=filename)
53
 
        prefix = urllib.unquote(h.url.current())
54
 
        prefix = prefix[1:-len("/+edit")]
55
 
 
56
 
        return self.render('admin/template-edit.html',
57
 
                           {'template': existing_template,
58
 
                            'path': prefix})
59
 
 
60
 
    def save_template(self, filename):
 
77
        if existing_template is not None:
 
78
            save_url = h.url(controller="admin/wiki",
 
79
                             action="save_page_template",
 
80
                             filename=filename)
 
81
 
 
82
            return self.render('admin/template-edit.html',
 
83
                               {'template': existing_template,
 
84
                                'save_url': save_url})
 
85
        abort(404)
 
86
 
 
87
    def save_page_template(self, filename):
61
88
        """Save templates into the database."""
62
 
        schema = TemplateSchema
 
89
        schema = TemplateSchema()
63
90
        try:
64
91
            form_result = schema.to_python(request.params)
65
92
        except formencode.Invalid, e:
66
93
            errors = e.unpack_errors()
67
 
            return formencode.htmlfill.render(self.edit_template(filename), defaults=request.params,
 
94
            return formencode.htmlfill.render(self.edit_page_template(filename), defaults=request.params,
68
95
                                              errors=errors)
69
96
 
70
97
        #If there is already a template in the database with this name then pull it
71
98
        #out and overwrite the values in it, otherwise create a new template.
72
 
 
73
99
        templates = Template.get(name=form_result['old_name'])
74
100
        new_template = templates[0] if templates else Template()
75
101
        new_template.name = form_result['name']
82
108
        if templates == []:
83
109
            Session.add(new_template)
84
110
        Session.commit()
85
 
        return redirect(h.url(controller='admin/wiki', action='view_template',
 
111
        return redirect(h.url(controller='admin/wiki', action='view_page_template',
86
112
                              filename=form_result['name']))
87
113
 
88
 
    def list_templates(self):
 
114
    def list_page_templates(self):
89
115
        """Return a list of all the page templates stored in the database."""
90
116
        template_names = get_template_names()
91
117
        return self.render('/admin/template-viewall.html',
92
118
                           {'template_names': template_names})
93
119
 
94
 
    def view_template(self, filename):
 
120
    def view_page_template(self, filename):
95
121
        """View an individual template."""
96
122
 
97
123
        template = Template.get_one(name=filename)
103
129
                                'formatter': formatter})
104
130
        abort(404)
105
131
 
 
132
    """Page Templates"""
 
133
 
106
134
    def show_project_wikis(self):
107
135
        """Gives an admin the ability to make a wiki branch."""
108
136
 
109
137
        projects = Project.get()
110
138
        return self.render('/admin/create-template.html',
111
 
                           {'projects': projects})        
 
139
                           {'projects': projects})
112
140
 
113
141
    def make_wiki_template(self, project_id):
114
142
        """Takes a wiki and stores in the template directory to be used by all other users."""