1472 |
1472 |
from wcs.api import ApiFormdefsDirectory
|
1473 |
1473 |
return ApiFormdefsDirectory(self.category)._q_index()
|
1474 |
1474 |
|
1475 |
|
def get_context(self):
|
1476 |
|
from wcs.api import is_url_signed, get_user_from_api_query_string
|
1477 |
|
user = get_user_from_api_query_string() or get_request().user
|
1478 |
|
list_all_forms = (user and user.is_admin) or (is_url_signed() and user is None)
|
1479 |
|
|
1480 |
|
list_forms = []
|
1481 |
|
|
1482 |
|
if self.category:
|
1483 |
|
formdefs = FormDef.select(lambda x: (
|
1484 |
|
str(x.category_id) == str(self.category.id) and (
|
1485 |
|
not x.is_disabled() or x.disabled_redirection)),
|
1486 |
|
order_by='name',
|
1487 |
|
ignore_errors=True,
|
1488 |
|
lightweight=True)
|
1489 |
|
else:
|
1490 |
|
formdefs = FormDef.select(lambda x: not x.is_disabled() or x.disabled_redirection,
|
1491 |
|
order_by='name',
|
1492 |
|
ignore_errors=True,
|
1493 |
|
lightweight=True)
|
1494 |
|
|
1495 |
|
charset = get_publisher().site_charset
|
1496 |
|
|
1497 |
|
for formdef in formdefs:
|
1498 |
|
authentication_required = False
|
1499 |
|
if formdef.roles and not list_all_forms:
|
1500 |
|
if not user:
|
1501 |
|
if not formdef.always_advertise:
|
1502 |
|
continue
|
1503 |
|
authentication_required = True
|
1504 |
|
elif logged_users_role().id not in formdef.roles:
|
1505 |
|
for q in user.roles or []:
|
1506 |
|
if q in formdef.roles:
|
1507 |
|
break
|
1508 |
|
else:
|
1509 |
|
if not formdef.always_advertise:
|
1510 |
|
continue
|
1511 |
|
authentication_required = True
|
1512 |
|
|
1513 |
|
formdict = {'title': unicode(formdef.name, charset),
|
1514 |
|
'slug': formdef.url_name,
|
1515 |
|
'url': formdef.get_url(),
|
1516 |
|
'authentication_required': authentication_required}
|
1517 |
|
|
1518 |
|
formdict['redirection'] = bool(formdef.is_disabled() and
|
1519 |
|
formdef.disabled_redirection)
|
1520 |
|
|
1521 |
|
# we include the count of submitted forms so it's possible to sort
|
1522 |
|
# them by popularity
|
1523 |
|
formdict['count'] = formdef.data_class().count()
|
1524 |
|
|
1525 |
|
if formdef.category:
|
1526 |
|
formdict['category'] = unicode(formdef.category.name, charset)
|
1527 |
|
formdict['category_position'] = (formdef.category.position or 0)
|
1528 |
|
else:
|
1529 |
|
formdict['category_position'] = sys.maxint
|
1530 |
|
|
1531 |
|
list_forms.append(formdict)
|
1532 |
|
|
1533 |
|
list_forms.sort(lambda x, y: cmp(x['category_position'], y['category_position']))
|
1534 |
|
for formdict in list_forms:
|
1535 |
|
del formdict['category_position']
|
1536 |
|
|
1537 |
|
return list_forms
|
1538 |
|
|
1539 |
1475 |
def get_categories(self, user):
|
1540 |
1476 |
result = []
|
1541 |
1477 |
formdefs = FormDef.select(
|