Parent: [7a2ad9] (diff)

Child: [024bd4] (diff)

Download this file

main.py    208 lines (172 with data), 6.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#-*- python -*-
import logging
# Non-stdlib imports
import pkg_resources
from tg import tmpl_context
from tg import expose, validate, redirect
from pylons import g, c, request
from formencode import validators
from pymongo.bson import ObjectId
from ming.orm.base import mapper
# Pyforge-specific imports
from pyforge.app import Application, ConfigOption, SitemapEntry
from pyforge.lib.helpers import push_config
from pyforge.lib.search import search
from pyforge.lib.decorators import audit, react
from pyforge.lib.security import require, has_artifact_access
from pyforge.model import ProjectRole
# Local imports
from forgetracker import model
from forgetracker import version
# from forgetracker.widgets.issue_form import create_issue_form
log = logging.getLogger(__name__)
class ForgeTrackerApp(Application):
__version__ = version.__version__
permissions = ['configure', 'read', 'write', 'comment']
config_options = Application.config_options + [
ConfigOption('some_str_config_option', str, 'some_str_config_option'),
ConfigOption('some_int_config_option', int, 42),
]
def __init__(self, project, config):
Application.__init__(self, project, config)
self.root = RootController()
@audit('Issues.#')
def auditor(self, routing_key, data):
log.info('Auditing data from %s (%s)',
routing_key, self.config.options.mount_point)
@react('Issues.#')
def reactor(self, routing_key, data):
log.info('Reacting to data from %s (%s)',
routing_key, self.config.options.mount_point)
@property
def sitemap(self):
menu_id = 'ForgeTracker (%s)' % self.config.options.mount_point
with push_config(c, app=self):
return [
SitemapEntry(menu_id, '.')[self.sidebar_menu()] ]
def sidebar_menu(self):
return [
SitemapEntry('Home', '.'),
SitemapEntry('Search', 'search'),
]
@property
def templates(self):
return pkg_resources.resource_filename('forgetracker', 'templates')
def install(self, project):
'Set up any default permissions and roles here'
self.uninstall(project)
# Give the installing user all the permissions
pr = c.user.project_role()
for perm in self.permissions:
self.config.acl[perm] = [ pr._id ]
self.config.acl['read'].append(
ProjectRole.m.get(name='*anonymous')._id)
self.config.acl['comment'].append(
ProjectRole.m.get(name='*authenticated')._id)
self.config.m.save()
globals = model.Globals.make({
'project_id':c.project._id,
'last_issue_num':0
})
globals.commit()
def uninstall(self, project):
"Remove all the plugin's artifacts from the database"
id = c.project._id
mapper(model.Issue).remove({'project_id':id})
mapper(model.Comment).remove({'project_id':id})
mapper(model.Attachment).remove({'project_id':id})
mapper(model.Globals).remove({'project_id':id})
class RootController(object):
@expose('forgetracker.templates.index')
def index(self):
return dict()
@expose('forgetracker.templates.search')
@validate(dict(q=validators.UnicodeString(if_empty=None),
history=validators.StringBool(if_empty=False)))
def search(self, q=None, history=None):
'local plugin search'
results = []
count=0
if not q:
q = ''
else:
search_query = '''%s
AND is_history_b:%s
AND mount_point_s:%s''' % (
q, history, c.app.config.options.mount_point)
results = search(search_query)
if results: count=results.hits
return dict(q=q, history=history, results=results or [], count=count)
def _lookup(self, issue_num, *remainder):
return IssueController(issue_num), remainder
class IssueController(object):
def __init__(self, issue_num=None):
self.issue_num = issue_num
if issue_num: self.issue = model.Issue.m.get(issue_num=issue_num)
@expose()
def index(self, issue_num, **kw):
return dict()
@expose('forgetracker.templates.new_issue')
def new(self, **kw):
tmpl_context.form = create_issue_form
return dict(modelname='Issue',
page='New Issue')
class ArtifactController(object):
def __init__(self, id):
self.artifact = model.MyArtifact.m.get(_id=ObjectId.url_decode(id))
self.comments = CommentController(self.artifact)
@expose('forgetracker.templates.artifact')
@validate(dict(version=validators.Int()))
def index(self, version=None):
require(has_artifact_access('read', self.artifact))
artifact = self.get_version(version)
if artifact is None:
if version:
redirect('.?version=%d' % (version-1))
elif version <= 0:
redirect('.')
else:
redirect('..')
cur = artifact.version
if cur > 0: prev = cur-1
else: prev = None
next = cur+1
return dict(artifact=artifact,
cur=cur, prev=prev, next=next)
def get_version(self, version):
if not version: return self.artifact
ss = model.MyArtifactHistory.m.get(artifact_id=self.artifact._id,
version=version)
if ss is None: return None
result = deepcopy(self.artifact)
return result.update(ss.data)
class CommentController(object):
def __init__(self, artifact, comment_id=None):
self.artifact = artifact
self.comment_id = comment_id
self.comment = model.MyArtifactComment.m.get(_id=self.comment_id)
@expose()
def reply(self, text):
require(has_artifact_access('comment', self.artifact))
if self.comment_id:
c = self.comment.reply()
c.text = text
else:
c = self.artifact.reply()
c.text = text
c.m.save()
redirect(request.referer)
@expose()
def delete(self):
require(lambda:c.user._id == self.comment.author()._id)
self.comment.text = '[Text deleted by commenter]'
self.comment.m.save()
redirect(request.referer)
def _lookup(self, next, *remainder):
if self.comment_id:
return CommentController(
self.artifact,
self.comment_id + '/' + next), remainder
else:
return CommentController(
self.artifact, next), remainder