~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to pybb/util.py

  • Committer: franku
  • Date: 2016-07-02 12:38:06 UTC
  • mfrom: (404.2.56 widelands)
  • Revision ID: somal@arcor.de-20160702123806-q69u3d48s1prrxds
merged the django1_8 branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from datetime import datetime
2
1
import os.path
3
2
import random
 
3
import traceback
 
4
import json
 
5
 
4
6
from BeautifulSoup import BeautifulSoup
5
 
import traceback
6
 
 
 
7
from datetime import datetime
7
8
from django.shortcuts import render_to_response
8
9
from django.template import RequestContext
9
10
from django.http import HttpResponse
10
11
from django.utils.functional import Promise
11
 
from django.utils.translation import force_unicode, check_for_language
12
 
from django.utils.simplejson import JSONEncoder
 
12
from django.utils.translation import check_for_language
 
13
from django.utils.encoding import force_unicode
13
14
from django import forms
14
15
from django.template.defaultfilters import urlize as django_urlize
15
16
from django.core.paginator import Paginator, EmptyPage, InvalidPage
16
17
from django.conf import settings
17
 
 
18
18
from pybb import settings as pybb_settings
19
19
 
20
20
 
32
32
            if not isinstance(output, dict):
33
33
                return output
34
34
            kwargs = {'context_instance': RequestContext(request)}
 
35
 
 
36
            # NOCOMM: 'MIME_TYPE' is never in output as i can see for now.
 
37
            # But if, this should maybe 'content_type' instead
35
38
            if 'MIME_TYPE' in output:
36
39
                kwargs['mimetype'] = output.pop('MIME_TYPE')
37
40
            if 'TEMPLATE' in output:
38
41
                template = output.pop('TEMPLATE')
39
42
            else:
40
43
                template = template_path
 
44
                
41
45
            return render_to_response(template, output, **kwargs)
42
46
        return wrapper
43
47
 
106
110
    return wrapper
107
111
 
108
112
 
109
 
class LazyJSONEncoder(JSONEncoder):
 
113
class LazyJSONEncoder(json.JSONEncoder):
110
114
    """
111
115
    This fing need to save django from crashing.
112
116
    """
122
126
    """
123
127
    HttpResponse subclass that serialize data into JSON format.
124
128
    """
125
 
 
 
129
    # NOCOMM: The mimetype argument maybe must be replaced with content_type
126
130
    def __init__(self, data, mimetype='application/json'):
127
131
        json_data = LazyJSONEncoder().encode(data)
128
132
        super(JsonResponse, self).__init__(
129
 
            content=json_data, mimetype=mimetype)
 
133
            content=json_data, content_type=mimetype)
130
134
 
131
135
        
132
136
def build_form(Form, _request, GET=False, *args, **kwargs):
159
163
                    islink = True
160
164
                    break
161
165
                ptr = ptr.parent
162
 
 
163
166
            if not islink:
164
 
                chunk = chunk.replaceWith(django_urlize(unicode(chunk)))
 
167
                # Using unescape to prevent conversation of f.e. > to >
 
168
                chunk = chunk.replaceWith(django_urlize(unicode(unescape(chunk))))
165
169
 
166
170
        return unicode(soup)
167
171
 
240
244
    return page, paginator
241
245
 
242
246
 
 
247
# NOCOMM: This function is never used AFAIK
 
248
# 'django_language' isn't available since django 1.8
243
249
def set_language(request, language):
244
250
    """
245
251
    Change the language of session of authenticated user.
246
252
    """
247
 
 
248
253
    if language and check_for_language(language):
249
254
        if hasattr(request, 'session'):
250
255
            request.session['django_language'] = language