cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
context_processors.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.utils.context_processors
22 #
23 # @author Piotr W√≥jcik
24 # @date 24.10.2011
25 #
26 
27 import logging
28 
29 from django.conf import settings
30 from django.contrib import messages
31 from django.utils.translation import ugettext_lazy as _
32 
33 from common.states import cluster_states
34 from wi.utils.exceptions import RestErrorException
35 from wi.utils.views import prep_data
36 
37 
38 ##
39 #
40 # Context processor for attaching CM list and other data to every request.
41 #
42 def add_variables(request):
43  if request.session.get('user') == None:
44  return {}
45 
46  try:
47  rest_data = prep_data('guest/cluster/list_names/', request.session)
48  except RestErrorException as ex:
49  messages.error(request, ex.value)
50  wi_logger = logging.getLogger('wi_logger')
51  wi_logger.error('%s' % ex.value)
52  return {}
53 
54  cm_list = rest_data
55 
56  none_available = True
57  for item in cm_list:
58  if item['state'] == cluster_states['ok']:
59  none_available = False
60  break
61 
62  if none_available:
63  messages.error(request, _('No CM available.'))
64 
65  admin_cm_name = None
66  for item in cm_list:
67  if request.session['user'].is_logged_admin_cm and request.session['user'].cm_id == item['cluster_id']:
68  admin_cm_name = item['name']
69 
70  return {'cm_list': cm_list,
71  'cluster_states': cluster_states,
72  'admin_cm_name': admin_cm_name,
73  'VERSION': settings.VERSION}
74