23 ] |
23 ] |
24 |
24 |
25 |
25 |
26 from django.utils.translation import ugettext_lazy |
26 from django.utils.translation import ugettext_lazy |
27 |
27 |
28 from soc.views import helper |
|
29 |
|
30 import soc.views.helper.responses |
|
31 import soc.views.helper.templates |
|
32 |
|
33 |
28 |
34 class Error(Exception): |
29 class Error(Exception): |
35 """Base exception for out-of-band responses raised by logic or views. |
30 """Base exception for out-of-band responses raised by logic or views. |
36 """ |
31 """ |
37 TEMPLATE_NAME = 'error.html' |
32 TEMPLATE_NAME = 'error.html' |
43 Args: |
38 Args: |
44 message_fmt: format string, when combined with a context supplied to |
39 message_fmt: format string, when combined with a context supplied to |
45 the response() method, produces the message to display on the |
40 the response() method, produces the message to display on the |
46 response page; this can be a simple string containing *no* named |
41 response page; this can be a simple string containing *no* named |
47 format specifiers |
42 format specifiers |
48 context: see response() |
43 context: see soc.views.helper.responses.errorResponse() |
49 **response_args: keyword arguments that are supplied directly to |
44 **response_args: keyword arguments that are supplied directly to |
50 django.http.HttpResponse; the most commonly used is 'status' to |
45 django.http.HttpResponse; the most commonly used is 'status' to |
51 set the HTTP status code for the response |
46 set the HTTP status code for the response |
52 """ |
47 """ |
53 self.message_fmt = message_fmt |
48 self.message_fmt = message_fmt |
54 self._context = context |
49 self.context = context |
55 self._response_args = response_args |
50 self.response_args = response_args |
56 |
|
57 def response(self, request, template=None, context=None): |
|
58 """Creates an HTTP response from the OutOfBandResponse exception. |
|
59 |
|
60 Args: |
|
61 request: a Django HTTP request |
|
62 template: the "sibling" template (or a search list of such templates) |
|
63 from which to construct the actual template name (or names) |
|
64 context: optional context dict supplied to the template, which is |
|
65 modified (so supply a copy if such modification is not acceptable) |
|
66 """ |
|
67 if not context: |
|
68 context = self._context |
|
69 |
|
70 if not context: |
|
71 context = helper.responses.getUniversalContext(request) |
|
72 |
|
73 if not template: |
|
74 template = [] |
|
75 |
|
76 # make a list of possible "sibling" templates, then append a default |
|
77 templates = helper.templates.makeSiblingTemplatesList(template, |
|
78 self.TEMPLATE_NAME, default_template=self.DEF_TEMPLATE) |
|
79 |
|
80 context['status'] = self._response_args.get('status') |
|
81 |
|
82 if not context.get('message'): |
|
83 # supplied context did not explicitly override the message |
|
84 context['message'] = self.message_fmt % context |
|
85 |
|
86 return helper.responses.respond(request, templates, context=context, |
|
87 response_args=self._response_args) |
|
88 |
51 |
89 |
52 |
90 class LoginRequest(Error): |
53 class LoginRequest(Error): |
91 """Out of band error raised when login is requested. |
54 """Out of band error raised when login is requested. |
92 """ |
55 """ |
98 |
61 |
99 def __init__(self, message_fmt=None, **response_args): |
62 def __init__(self, message_fmt=None, **response_args): |
100 """Constructor used to set response message and HTTP response arguments. |
63 """Constructor used to set response message and HTTP response arguments. |
101 |
64 |
102 Args: |
65 Args: |
103 message_fmt: same as Error.__init__() message_fmt, with the addition |
66 message_fmt: same as Error.__init__() message_fmt, with the addition of |
104 of a default value of None, in which case self.DEF_LOGIN_MSG_FMT is |
67 a default value of None, in which case self.DEF_LOGIN_MSG_FMT is used |
105 used |
|
106 **response_args: see Error.__init__() |
68 **response_args: see Error.__init__() |
107 """ |
69 """ |
108 if not message_fmt: |
70 if not message_fmt: |
109 message_fmt = self.DEF_LOGIN_MSG_FMT |
71 message_fmt = self.DEF_LOGIN_MSG_FMT |
110 |
72 |