Revision 75380308 snf-astakos-app/astakos/im/views.py

b/snf-astakos-app/astakos/im/views.py
109 109
    PAGINATE_BY_ALL,
110 110
    ACTIVATION_REDIRECT_URL,
111 111
    MODERATION_ENABLED)
112
from astakos.im.presentation import RESOURCES_PRESENTATION_DATA
112
from astakos.im import presentation
113 113
from astakos.im.api import get_services_dict
114 114
from astakos.im import settings as astakos_settings
115 115
from astakos.im.api.callpoint import AstakosCallpoint
......
1042 1042
            populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.attname))
1043 1043
        return response
1044 1044

  
1045

  
1046
def _resources_catalog(request):
1047
    """
1048
    `resource_catalog` contains a list of tuples. Each tuple contains the group
1049
    key the resource is assigned to and resources list of dicts that contain
1050
    resource information.
1051
    `resource_groups` contains information about the groups
1052
    """
1053
    # presentation data
1054
    resource_groups = presentation.RESOURCES.get('groups', {})
1055
    resource_catalog = ()
1056

  
1057
    # resources in database
1058
    result = callpoint.list_resources()
1059
    if not result.is_success:
1060
        messages.error(request, 'Unable to retrieve system resources: %s' %
1061
                                result.reason)
1062
    else:
1063
        # initialize resource_catalog to contain all group/resource information
1064
        for r in result.data:
1065
            if not r.get('group') in resource_groups:
1066
                resource_groups[r.get('group')] = {'icon': 'unknown'}
1067

  
1068
        resource_keys = [r.get('str_repr') for r in result.data]
1069
        resource_catalog = [[g, filter(lambda r: r.get('group', '') == g,
1070
                                       result.data)] for g in resource_groups]
1071

  
1072
    # order groups, also include unknown groups
1073
    groups_order = presentation.RESOURCES.get('groups_order')
1074
    for g in resource_groups.keys():
1075
        if not g in groups_order:
1076
            groups_order.append(g)
1077

  
1078
    # order resources, also include unknown resources
1079
    resources_order = presentation.RESOURCES.get('resources_order')
1080
    for r in resource_keys:
1081
        if not r in resources_order:
1082
            resources_order.append(r)
1083

  
1084
    # sort catalog groups
1085
    resource_catalog = sorted(resource_catalog,
1086
                              key=lambda g: groups_order.index(g[0]))
1087

  
1088
    # sort groups
1089
    def groupindex(g):
1090
        return groups_order.index(g[0])
1091
    resource_groups_list = sorted([(k, v) for k, v in resource_groups.items()],
1092
                                  key=groupindex)
1093
    resource_groups = OrderedDict(resource_groups_list)
1094

  
1095
    # sort resources
1096
    def resourceindex(r):
1097
        return resources_order.index(r['str_repr'])
1098
    for index, group in enumerate(resource_catalog):
1099
        resource_catalog[index][1] = sorted(resource_catalog[index][1],
1100
                                            key=resourceindex)
1101
        if len(resource_catalog[index][1]) == 0:
1102
            resource_catalog.pop(index)
1103
            for gindex, g in enumerate(resource_groups):
1104
                if g[0] == group[0]:
1105
                    resource_groups.pop(gindex)
1106

  
1107
    return resource_catalog, resource_groups
1108

  
1109

  
1045 1110
@require_http_methods(["GET", "POST"])
1046 1111
@valid_astakos_user_required
1047 1112
def project_add(request):
1048

  
1049 1113
    user = request.user
1050 1114
    reached, limit = reached_pending_application_limit(user.id)
1051 1115
    if not user.is_project_admin() and reached:
......
1055 1119
        next = restrict_next(next, domain=COOKIE_DOMAIN)
1056 1120
        return redirect(next)
1057 1121

  
1058
    resource_groups = RESOURCES_PRESENTATION_DATA.get('groups', {})
1059
    resource_catalog = ()
1060
    result = callpoint.list_resources()
1061
    details_fields = [
1062
        "name", "homepage", "description","start_date","end_date", "comments"]
1063
    membership_fields =[
1064
        "member_join_policy", "member_leave_policy", "limit_on_members_number"]
1065
    if not result.is_success:
1066
        messages.error(
1067
            request,
1068
            'Unable to retrieve system resources: %s' % result.reason
1069
    )
1070
    else:
1071
        resource_catalog = [
1072
            [g, filter(lambda r: r.get('group', '') == g, result.data)] \
1073
                for g in resource_groups]
1074

  
1075
    # order resources
1076
    groups_order = RESOURCES_PRESENTATION_DATA.get('groups_order')
1077
    resources_order = RESOURCES_PRESENTATION_DATA.get('resources_order')
1078
    resource_catalog = sorted(resource_catalog, key=lambda g:groups_order.index(g[0]))
1079

  
1080
    resource_groups_list = sorted([(k,v) for k,v in resource_groups.items()],
1081
                                  key=lambda f:groups_order.index(f[0]))
1082
    resource_groups = OrderedDict(resource_groups_list)
1083
    for index, group in enumerate(resource_catalog):
1084
        resource_catalog[index][1] = sorted(resource_catalog[index][1],
1085
                                            key=lambda r: resources_order.index(r['str_repr']))
1086

  
1087

  
1122
    details_fields = ["name", "homepage", "description", "start_date",
1123
                      "end_date", "comments"]
1124
    membership_fields = ["member_join_policy", "member_leave_policy",
1125
                         "limit_on_members_number"]
1126
    resource_catalog, resource_groups = _resources_catalog(request)
1088 1127
    extra_context = {
1089
        'resource_catalog':resource_catalog,
1090
        'resource_groups':resource_groups,
1091
        'show_form':True,
1092
        'details_fields':details_fields,
1093
        'membership_fields':membership_fields}
1128
        'resource_catalog': resource_catalog,
1129
        'resource_groups': resource_groups,
1130
        'show_form': True,
1131
        'details_fields': details_fields,
1132
        'membership_fields': membership_fields}
1094 1133

  
1095 1134
    response = None
1096 1135
    with ExceptionHandler(request):
......
1186 1225
        next = restrict_next(next, domain=COOKIE_DOMAIN)
1187 1226
        return redirect(next)
1188 1227

  
1189
    resource_groups = RESOURCES_PRESENTATION_DATA.get('groups', {})
1190
    resource_catalog = ()
1191
    result = callpoint.list_resources()
1192
    details_fields = [
1193
        "name", "homepage", "description","start_date","end_date", "comments"]
1194
    membership_fields =[
1195
        "member_join_policy", "member_leave_policy", "limit_on_members_number"]
1196
    if not result.is_success:
1197
        messages.error(
1198
            request,
1199
            'Unable to retrieve system resources: %s' % result.reason
1200
    )
1201
    else:
1202
        resource_catalog = [
1203
            (g, filter(lambda r: r.get('group', '') == g, result.data)) \
1204
                for g in resource_groups]
1228
    details_fields = ["name", "homepage", "description", "start_date",
1229
                      "end_date", "comments"]
1230
    membership_fields = ["member_join_policy", "member_leave_policy",
1231
                         "limit_on_members_number"]
1232
    resource_catalog, resource_groups = _resources_catalog(request)
1205 1233
    extra_context = {
1206
        'resource_catalog':resource_catalog,
1207
        'resource_groups':resource_groups,
1208
        'show_form':True,
1209
        'details_fields':details_fields,
1234
        'resource_catalog': resource_catalog,
1235
        'resource_groups': resource_groups,
1236
        'show_form': True,
1237
        'details_fields': details_fields,
1210 1238
        'update_form': True,
1211
        'membership_fields':membership_fields}
1239
        'membership_fields': membership_fields
1240
    }
1212 1241

  
1213 1242
    response = None
1214 1243
    with ExceptionHandler(request):
1215
        response =_update_object(
1244
        response = _update_object(
1216 1245
            request,
1217 1246
            object_id=application_id,
1218 1247
            template_name='im/projects/projectapplication_form.html',
1219
            extra_context=extra_context, post_save_redirect=reverse('project_list'),
1248
            extra_context=extra_context,
1249
            post_save_redirect=reverse('project_list'),
1220 1250
            form_class=ProjectApplicationForm,
1221
            msg = _("The %(verbose_name)s has been received and "
1222
                    "is under consideration."),
1223
            )
1251
            msg=_("The %(verbose_name)s has been received and is under "
1252
                  "consideration."))
1224 1253

  
1225 1254
    if response is not None:
1226 1255
        return response

Also available in: Unified diff