Skip to content

Commit

Permalink
[#626] Use helper functions to get orgs/groups for dashboard plus cle…
Browse files Browse the repository at this point in the history
…anups

Conflicts:

	ckan/templates/group/snippets/group_item.html
	ckan/templates/organization/snippets/organization_item.html
  • Loading branch information
tobes authored and amercader committed Jul 1, 2013
1 parent 35a461c commit dc0db61
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 50 deletions.
15 changes: 3 additions & 12 deletions ckan/controllers/user.py
Expand Up @@ -603,30 +603,21 @@ def dashboard(self, id=None, offset=0):
return render('user/dashboard.html')

def dashboard_datasets(self):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
context = {'for_view': True}
data_dict = {'user_obj': c.userobj}
self._setup_template_variables(context, data_dict)
return render('user/dashboard_datasets.html')

def dashboard_organizations(self):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
context = {'for_view': True}
data_dict = {'user_obj': c.userobj}
self._setup_template_variables(context, data_dict)
# TODO: Add organizations that user is a member of here. I tried
# h.organizations_available() but that errors in the template
c.organizations = []#h.organizations_available()
return render('user/dashboard_organizations.html')

def dashboard_groups(self):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
context = {'for_view': True}
data_dict = {'user_obj': c.userobj}
self._setup_template_variables(context, data_dict)
# TODO: Add groups that user is a member of here. I tried
# h.groups_available() but that errors in the template
c.groups = []#h.groups_available()
return render('user/dashboard_groups.html')

def follow(self, id):
Expand Down
16 changes: 9 additions & 7 deletions ckan/lib/helpers.py
Expand Up @@ -1269,18 +1269,20 @@ def popular(type_, number, min=1, title=None):
return snippet('snippets/popular.html', title=title, number=number, min=min)


def groups_available():
''' return a list of available groups '''
context = {'model': model, 'session': model.Session,
'user': c.user or c.author}
data_dict = {'available_only': True}
def groups_available(am_member=False):
''' return a list of available groups
:param am_member: only show groups user is a member of
:type am-am: bool
'''
context = {}
data_dict = {'available_only': True, 'am_member': am_member}
return logic.get_action('group_list_authz')(context, data_dict)


def organizations_available(permission='edit_group'):
''' return a list of available organizations '''
context = {'model': model, 'session': model.Session,
'user': c.user}
context = {'user': c.user}
data_dict = {'permission': permission}
return logic.get_action('organization_list_for_user')(context, data_dict)

Expand Down
55 changes: 30 additions & 25 deletions ckan/logic/action/get.py
Expand Up @@ -418,13 +418,18 @@ def group_list_authz(context, data_dict):
(optional, default: ``False``)
:type available_only: boolean
:param am_member: only show groups user is a member of else sys_admins get all
:type am-am: bool
(optional, default: ``False``)
:returns: the names of groups that the user is authorized to edit
:rtype: list of strings
'''
model = context['model']
user = context['user']
available_only = data_dict.get('available_only',False)
available_only = data_dict.get('available_only', False)
am_member = data_dict.get('am_member', False)

_check_access('group_list_authz',context, data_dict)

Expand All @@ -436,7 +441,7 @@ def group_list_authz(context, data_dict):
if not user_id:
return []

if not sysadmin:
if not sysadmin or am_member:
q = model.Session.query(model.Member) \
.filter(model.Member.table_name == 'user') \
.filter(model.Member.capacity.in_(roles)) \
Expand All @@ -452,7 +457,7 @@ def group_list_authz(context, data_dict):
.filter(model.Group.is_organization == False) \
.filter(model.Group.state == 'active')

if not sysadmin:
if not sysadmin or am_member:
q = q.filter(model.Group.id.in_(group_ids))

groups = q.all()
Expand All @@ -462,7 +467,7 @@ def group_list_authz(context, data_dict):
if package:
groups = set(groups) - set(package.get_groups())

return [{'id':group.id,'name':group.name} for group in groups]
return [{'id': group.id, 'name': group.name, 'type': group.type} for group in groups]

def organization_list_for_user(context, data_dict):
'''Return the list of organizations that the user is a member of.
Expand All @@ -485,35 +490,35 @@ def organization_list_for_user(context, data_dict):
.filter(model.Group.is_organization == True) \
.filter(model.Group.state == 'active')

if sysadmin:
# Sysadmins can see all organizations
return [{'id':org.id,'name':org.name,'title':org.title} for org in orgs_q.all()]
if not sysadmin:
# for non-Sysadmins check they have the required permission

permission = data_dict.get('permission', 'edit_group')
permission = data_dict.get('permission', 'edit_group')

roles = ckan.new_authz.get_roles_with_permission(permission)
roles = ckan.new_authz.get_roles_with_permission(permission)

if not roles:
return []
user_id = new_authz.get_user_id_for_username(user, allow_none=True)
if not user_id:
return []
if not roles:
return []
user_id = new_authz.get_user_id_for_username(user, allow_none=True)
if not user_id:
return []

q = model.Session.query(model.Member) \
.filter(model.Member.table_name == 'user') \
.filter(model.Member.capacity.in_(roles)) \
.filter(model.Member.table_id == user_id)
q = model.Session.query(model.Member) \
.filter(model.Member.table_name == 'user') \
.filter(model.Member.capacity.in_(roles)) \
.filter(model.Member.table_id == user_id)

group_ids = []
for row in q.all():
group_ids.append(row.group_id)
group_ids = []
for row in q.all():
group_ids.append(row.group_id)

if not group_ids:
return []
if not group_ids:
return []

q = orgs_q.filter(model.Group.id.in_(group_ids))
orgs_q = orgs_q.filter(model.Group.id.in_(group_ids))

return [{'id':org.id,'name':org.name,'title':org.title} for org in q.all()]
return [{'id': org.id, 'name': org.name, 'title': org.title, 'type': org.type}
for org in orgs_q.all()]

def group_revision_list(context, data_dict):
'''Return a group's revisions.
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/group/snippets/group_item.html
Expand Up @@ -11,7 +11,7 @@
{% endfor %}
</ul>
#}
{% set url = h.url_for(group.type ~ '_read', action='read', id=group.name) %}
{% set url = h.url_for(group.type + '_read', action='read', id=group.name) %}
<li class="media-item">
{% block image %}
<img src="{{ group.image_url or '/base/images/placeholder-group.png' }}" alt="{{ group.name }}" class="media-image">
Expand Down
3 changes: 2 additions & 1 deletion ckan/templates/organization/snippets/organization_item.html
Expand Up @@ -11,7 +11,8 @@
{% endfor %}
</ul>
#}
{% set url = h.url_for(organization.type ~ '_read', action='read', id=organization.name) %}

{% set url = h.url_for(organization.type + '_read', action='read', id=organization.name) %}
<li class="media-item">
{% block image %}
<img src="{{ organization.image_url or '/base/images/placeholder-organization.png' }}" alt="{{ organization.name }}" class="media-image">
Expand Down
5 changes: 3 additions & 2 deletions ckan/templates/user/dashboard_groups.html
Expand Up @@ -10,8 +10,9 @@

{% block primary_content_inner %}
<h2 class="page-heading">{{ _('My Groups') }}</h2>
{% if c.groups %}
{% snippet "group/snippets/group_list.html", groups=c.groups %}
{% set groups = h.groups_available(am_member=True) %}
{% if groups %}
{% snippet "group/snippets/group_list.html", groups=groups %}
{% else %}
<p class="empty">
{{ _('You are not a member of any groups.') }}
Expand Down
5 changes: 3 additions & 2 deletions ckan/templates/user/dashboard_organizations.html
Expand Up @@ -10,8 +10,9 @@

{% block primary_content_inner %}
<h2 class="page-heading">{{ _('My Organizations') }}</h2>
{% if c.organizations %}
{% snippet "organization/snippets/organization_list.html", organizations=c.organizations %}
{% set organizations = h.organizations_available() %}
{% if organizations %}
{% snippet "organization/snippets/organization_list.html", organizations=organizations %}
{% else %}
<p class="empty">
{{ _('You are not a member of any organizations.') }}
Expand Down

0 comments on commit dc0db61

Please sign in to comment.