cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
__init__.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
22 #
23 # @author Piotr Wójcik
24 # @date 24.03.2011
25 #
26 
27 import logging
28 import os
29 from time import time
30 
31 from django.conf import settings
32 from django.utils.translation import ugettext_lazy as _
33 
34 from common.utils import ServerProxy
35 from wi.utils.exceptions import RestErrorException
36 from wi.utils.messages_ajax import error, success
37 from wi.utils.messages_codes import get_error, auth_error_text
38 
39 
40 REDIRECT_FIELD_NAME = 'next'
41 CLM = ServerProxy(settings.CLOUD_MANAGER_ADDRESS)
42 
43 
44 ##
45 #
46 # Checks status of response response and throws appropriate error.
47 #
48 def check_response_errors(response, session):
49  if response['status'] != 'ok':
50  error_code = response['status']
51  error_msg = get_error(error_code)
52 
53 # from wi.utils.auth import logout
54 # if response['status'] == 'user_auth':
55 # logout(session)
56 # error_msg = auth_error_text
57 
58  raise RestErrorException(error_msg)
59 
60  return response
61 
62 
63 ##
64 #
65 # Returns dictionary with key: @prm{key} equal to @prm{key_value} from a
66 # list of dictionaries: @prm{list_of_dicts}.
67 #
68 def get_dict_from_list(list_of_dicts, key_value, key='id'):
69  for dictionary in list_of_dicts:
70  if dictionary.get(key) == None:
71  raise Exception("No key: " + key + " in dictionary.")
72  if dictionary.get(key) == key_value:
73  return dictionary
74  return None
75 
76 
77 ##
78 #
79 # Returns list of dictionaries with keys: @prm{key} equal to one from list
80 # @prm{list_of_key_values} from a list of dictionaries: @prm{list_of_dicts}.
81 #
82 def get_dicts_from_list(list_of_dicts, list_of_key_values, key='id'):
83  ret = []
84  for dictionary in list_of_dicts:
85  if dictionary.get(key) == None:
86  raise Exception("No key: " + key + " in dictionary.")
87  if dictionary.get(key) in list_of_key_values:
88  ret.append(dictionary)
89  return ret
90