Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / views / util.py @ b10ceccd

History | View | Annotate | Download (10.4 kB)

1
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from django.contrib import messages
35
from django.contrib.auth.views import redirect_to_login
36
from django.core.xheaders import populate_xheaders
37
from django.http import HttpResponse
38
from django.shortcuts import redirect
39
from django.template import RequestContext, loader as template_loader
40
from django.utils.translation import ugettext as _
41
from django.views.generic.create_update import apply_extra_context, \
42
    get_model_and_form_class, lookup_object
43

    
44
from synnefo.lib.ordereddict import OrderedDict
45

    
46
from astakos.im import presentation
47
from astakos.im.util import model_to_dict
48
from astakos.im.models import Resource
49
import astakos.im.messages as astakos_messages
50
import logging
51

    
52
logger = logging.getLogger(__name__)
53

    
54

    
55
class ExceptionHandler(object):
56
    def __init__(self, request):
57
        self.request = request
58

    
59
    def __enter__(self):
60
        pass
61

    
62
    def __exit__(self, exc_type, value, traceback):
63
        if value is not None:  # exception
64
            logger.exception(value)
65
            m = _(astakos_messages.GENERIC_ERROR)
66
            messages.error(self.request, m)
67
            return True  # suppress exception
68

    
69

    
70
def render_response(template, tab=None, status=200, context_instance=None,
71
                    **kwargs):
72
    """
73
    Calls ``django.template.loader.render_to_string`` with an additional
74
    ``tab`` keyword argument and returns an ``django.http.HttpResponse``
75
    with the specified ``status``.
76
    """
77
    if tab is None:
78
        tab = template.partition('_')[0].partition('.html')[0]
79
    kwargs.setdefault('tab', tab)
80
    html = template_loader.render_to_string(
81
        template, kwargs, context_instance=context_instance)
82
    response = HttpResponse(html, status=status)
83
    return response
84

    
85

    
86
def _create_object(request, model=None, template_name=None,
87
                   template_loader=template_loader, extra_context=None,
88
                   post_save_redirect=None, login_required=False,
89
                   context_processors=None, form_class=None, msg=None):
90
    """
91
    Based of django.views.generic.create_update.create_object which displays a
92
    summary page before creating the object.
93
    """
94

    
95
    if extra_context is None:
96
        extra_context = {}
97
    if login_required and not request.user.is_authenticated():
98
        return redirect_to_login(request.path)
99

    
100
    model, form_class = get_model_and_form_class(model, form_class)
101
    extra_context['edit'] = 0
102
    if request.method == 'POST':
103
        form = form_class(request.POST, request.FILES)
104
        if form.is_valid():
105
            verify = request.GET.get('verify')
106
            edit = request.GET.get('edit')
107
            if verify == '1':
108
                extra_context['show_form'] = False
109
                extra_context['form_data'] = form.cleaned_data
110
            elif edit == '1':
111
                extra_context['show_form'] = True
112
            else:
113
                new_object = form.save()
114
                if not msg:
115
                    msg = _(
116
                        "The %(verbose_name)s was created successfully.")
117
                msg = msg % model._meta.__dict__
118
                messages.success(request, msg, fail_silently=True)
119
                return redirect(post_save_redirect, new_object)
120
    else:
121
        form = form_class()
122

    
123
    # Create the template, context, response
124
    if not template_name:
125
        template_name = "%s/%s_form.html" % \
126
            (model._meta.app_label, model._meta.object_name.lower())
127
    t = template_loader.get_template(template_name)
128
    c = RequestContext(request, {
129
        'form': form
130
    }, context_processors)
131
    apply_extra_context(extra_context, c)
132
    return HttpResponse(t.render(c))
133

    
134

    
135
def _update_object(request, model=None, object_id=None, slug=None,
136
                   slug_field='slug', template_name=None,
137
                   template_loader=template_loader, extra_context=None,
138
                   post_save_redirect=None, login_required=False,
139
                   context_processors=None, template_object_name='object',
140
                   form_class=None, msg=None):
141
    """
142
    Based of django.views.generic.create_update.update_object which displays a
143
    summary page before updating the object.
144
    """
145

    
146
    if extra_context is None:
147
        extra_context = {}
148
    if login_required and not request.user.is_authenticated():
149
        return redirect_to_login(request.path)
150

    
151
    model, form_class = get_model_and_form_class(model, form_class)
152
    obj = lookup_object(model, object_id, slug, slug_field)
153

    
154
    if request.method == 'POST':
155
        form = form_class(request.POST, request.FILES, instance=obj)
156
        if form.is_valid():
157
            verify = request.GET.get('verify')
158
            edit = request.GET.get('edit')
159
            if verify == '1':
160
                extra_context['show_form'] = False
161
                extra_context['form_data'] = form.cleaned_data
162
            elif edit == '1':
163
                extra_context['show_form'] = True
164
            else:
165
                obj = form.save()
166
                if not msg:
167
                    msg = _(
168
                        "The %(verbose_name)s was created successfully.")
169
                msg = msg % model._meta.__dict__
170
                messages.success(request, msg, fail_silently=True)
171
                return redirect(post_save_redirect, obj)
172
    else:
173
        form = form_class(instance=obj)
174

    
175
    if not template_name:
176
        template_name = "%s/%s_form.html" % \
177
            (model._meta.app_label, model._meta.object_name.lower())
178
    t = template_loader.get_template(template_name)
179
    c = RequestContext(request, {
180
        'form': form,
181
        template_object_name: obj,
182
    }, context_processors)
183
    apply_extra_context(extra_context, c)
184
    response = HttpResponse(t.render(c))
185
    populate_xheaders(request, response, model,
186
                      getattr(obj, obj._meta.pk.attname))
187
    return response
188

    
189

    
190
def _resources_catalog(for_project=False, for_usage=False):
191
    """
192
    `resource_catalog` contains a list of tuples. Each tuple contains the group
193
    key the resource is assigned to and resources list of dicts that contain
194
    resource information.
195
    `resource_groups` contains information about the groups
196
    """
197
    # presentation data
198
    resources_meta = presentation.RESOURCES
199
    resource_groups = resources_meta.get('groups', {})
200
    resource_catalog = ()
201
    resource_keys = []
202

    
203
    # resources in database
204
    resource_details = map(lambda obj: model_to_dict(obj, exclude=[]),
205
                           Resource.objects.all())
206
    # initialize resource_catalog to contain all group/resource information
207
    for r in resource_details:
208
        if not r.get('group') in resource_groups:
209
            resource_groups[r.get('group')] = {'icon': 'unknown'}
210

    
211
    resource_keys = [r.get('str_repr') for r in resource_details]
212
    resource_catalog = [[g, filter(lambda r: r.get('group', '') == g,
213
                                   resource_details)] for g in resource_groups]
214

    
215
    # order groups, also include unknown groups
216
    groups_order = resources_meta.get('groups_order')
217
    for g in resource_groups.keys():
218
        if not g in groups_order:
219
            groups_order.append(g)
220

    
221
    # order resources, also include unknown resources
222
    resources_order = resources_meta.get('resources_order')
223
    for r in resource_keys:
224
        if not r in resources_order:
225
            resources_order.append(r)
226

    
227
    # sort catalog groups
228
    resource_catalog = sorted(resource_catalog,
229
                              key=lambda g: groups_order.index(g[0]))
230

    
231
    # sort groups
232
    def groupindex(g):
233
        return groups_order.index(g[0])
234
    resource_groups_list = sorted([(k, v) for k, v in resource_groups.items()],
235
                                  key=groupindex)
236
    resource_groups = OrderedDict(resource_groups_list)
237

    
238
    # sort resources
239
    def resourceindex(r):
240
        return resources_order.index(r['str_repr'])
241

    
242
    for index, group in enumerate(resource_catalog):
243
        resource_catalog[index][1] = sorted(resource_catalog[index][1],
244
                                            key=resourceindex)
245
        if len(resource_catalog[index][1]) == 0:
246
            resource_catalog.pop(index)
247
            for gindex, g in enumerate(resource_groups):
248
                if g[0] == group[0]:
249
                    resource_groups.pop(gindex)
250

    
251
    # filter out resources which user cannot request in a project application
252
    exclude = resources_meta.get('exclude_from_usage', [])
253
    for group_index, group_resources in enumerate(list(resource_catalog)):
254
        group, resources = group_resources
255
        for index, resource in list(enumerate(resources)):
256
            if for_project and not resource.get('allow_in_projects'):
257
                resources.remove(resource)
258
            if resource.get('str_repr') in exclude and for_usage:
259
                resources.remove(resource)
260

    
261
    # cleanup empty groups
262
    for group_index, group_resources in enumerate(list(resource_catalog)):
263
        group, resources = group_resources
264
        if len(resources) == 0:
265
            resource_catalog.pop(group_index)
266
            resource_groups.pop(group)
267

    
268
    return resource_catalog, resource_groups