Parent: [01c20a] (diff)

Child: [588145] (diff)

Download this file

trovecategories.py    192 lines (164 with data), 6.9 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
import logging, string, os
from urllib import urlencode
import bson
from tg import expose, session, flash, redirect, validate, config
from tg.decorators import with_trailing_slash
from pylons import c, g, request, response
from string import digits, lowercase
from allura.lib.security import require_authenticated
from allura import model as M
from allura.lib.decorators import require_post
from allura.controllers import BaseController
from allura.lib.widgets import forms
from allura.model import TroveCategory
class F(object):
remove_category_form = forms.RemoveTroveCategoryForm()
add_category_form = forms.AddTroveCategoryForm()
class TroveCategoryController(BaseController):
@expose()
def _lookup(self, catshortname, *remainder):
cat = M.TroveCategory.query.get(shortname=catshortname)
return TroveCategoryController(category=cat), remainder
def __init__(self, category=None):
self.category = category
super(TroveCategoryController, self).__init__()
@expose('jinja:allura:templates/trovecategories.html')
def index(self, **kw):
require_authenticated()
if self.category:
selected_cat = self.category
l = self.category.subcategories
hierarchy = []
temp_cat = self.category.parent_category
while temp_cat:
hierarchy = [temp_cat] + hierarchy
temp_cat = temp_cat.parent_category
else:
l = M.TroveCategory.query.find(dict(trove_parent_id=0))
selected_cat = None
hierarchy = []
return dict(
categories=l,
selected_cat=selected_cat,
hierarchy=hierarchy)
@expose()
@require_post()
@validate(F.add_category_form, error_handler=index)
def create(self, **kw):
require_authenticated()
name = kw.get('categoryname')
upper_id = int(kw.get('uppercategory_id', 0))
upper = M.TroveCategory.query.get(trove_cat_id=upper_id)
if upper_id == 0:
path = name
show_as_skill = True
elif upper is None:
flash('Invalid upper category.', "error")
redirect('/categories')
return
else:
path = upper.fullpath + " :: " + name
show_as_skill = upper.show_as_skill
newid=max([el.trove_cat_id for el in M.TroveCategory.query.find()]) + 1
shortname=name.replace(" ", "_").lower()
shortname=''.join([(c if (c in digits or c in lowercase) else "_")
for c in shortname])
oldcat=M.TroveCategory.query.get(shortname=shortname)
if oldcat:
flash('Category "%s" already exists.' % name, "error")
else:
category = M.TroveCategory(
trove_cat_id=newid,
trove_parent_id=upper_id,
fullname=name,
shortname=shortname,
fullpath=path,
show_as_skill=show_as_skill)
if category:
flash('Category "%s" successfully created.' % name)
else:
flash('An error occured while crearing the category.', "error")
if upper:
redirect('/categories/%s' % upper.shortname)
else:
redirect('/categories')
@expose()
@require_post()
@validate(F.remove_category_form, error_handler=index)
def remove(self, **kw):
require_authenticated()
cat = M.TroveCategory.query.get(trove_cat_id=int(kw['categoryid']))
if cat.trove_parent_id:
parent=M.TroveCategory.query.get(trove_cat_id=cat.trove_parent_id)
redirecturl = '/categories/%s' % parent.shortname
else:
redirecturl = '/categories'
if len(cat.subcategories) > 0:
m = "This category contains at least one sub-category, "
m = m + "therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if len(M.User.withskill(cat)) > 0:
m = "This category is used as a skill by at least a user, "
m = m + "therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_root_database=cat._id):
m = "This category is used as a database by at least a project, "
m = m + "therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_developmentstatus=cat._id):
m = "This category is used as development status by at least a "
m = m + "project, therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_audience=cat._id):
m = "This category is used as intended audience by at least a "
m = m + "project, therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_license=cat._id):
m = "This category is used as a license by at least a "
m = m + "project, therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_os=cat._id):
m = "This category is used as operating system by at least a "
m = m + "project, therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_language=cat._id):
m = "This category is used as programming language by at least a "
m = m + "project, therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_topic=cat._id):
m = "This category is used as a topic by at least a "
m = m + "project, therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_natlanguage=cat._id):
m = "This category is used as a natural language by at least a "
m = m + "project, therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
if M.Project.query.get(trove_environment=cat._id):
m = "This category is used as an environment by at least a "
m = m + "project, therefore it can't be removed."
flash(m, "error")
redirect(redirecturl)
return
M.TroveCategory.delete(cat)
flash('Category removed.')
redirect(redirecturl)