~franku/widelands-website/little_navi_changes

« back to all changes in this revision

Viewing changes to mainpage/views.py

  • Committer: Holger Rapp
  • Date: 2015-07-31 13:37:32 UTC
  • mfrom: (396.1.4 widelands)
  • Revision ID: sirver@gmx.de-20150731133732-rir6xrgzudnjx8kb
Fixes for changes to developer list in Widelands r7498.

I tried to implement some checks to the keys of the translators files without
breaking the website if there are some keys wrong. Theses checks are currently
only applied to the translator keys "translator-list" and
"your-language-name-in-english" (the two keys we need for the website). If one
or noth keys are wrong, a little text is placed at the top of the "Development
Team" list.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from django.template import RequestContext
3
3
from settings import WIDELANDS_SVN_DIR
4
4
from templatetags.wl_markdown import do_wl_markdown
5
 
 
 
5
from operator import itemgetter
6
6
import sys
7
 
import re
8
7
import json
9
8
import os
10
9
import os.path
11
10
 
 
11
 
12
12
def mainpage(request):
13
13
    return render_to_response('mainpage.html',
14
 
                context_instance=RequestContext(request))
 
14
                              context_instance=RequestContext(request))
15
15
 
16
16
 
17
17
from forms import RegistrationWithCaptchaForm
20
20
 
21
21
from registration.backends.default import DefaultBackend
22
22
 
 
23
 
23
24
def register(request):
24
 
    """
25
 
    Overwritten view from registration to include a captcha.
26
 
    We only need this because the remote IP addr must be passed
27
 
    to the form; the registration doesn't do this
 
25
    """Overwritten view from registration to include a captcha.
 
26
 
 
27
    We only need this because the remote IP addr must be passed to the
 
28
    form; the registration doesn't do this
 
29
 
28
30
    """
29
31
    remote_ip = request.META['REMOTE_ADDR']
30
32
    if request.method == 'POST':
31
 
        form = RegistrationWithCaptchaForm(remote_ip,data=request.POST,
32
 
                            files=request.FILES)
 
33
        form = RegistrationWithCaptchaForm(remote_ip, data=request.POST,
 
34
                                           files=request.FILES)
33
35
        if form.is_valid():
34
36
            new_user = DefaultBackend().register(request, **form.cleaned_data)
35
37
            return HttpResponseRedirect(reverse('registration_complete'))
36
38
    else:
37
39
        form = RegistrationWithCaptchaForm(remote_ip)
38
40
 
39
 
    return render_to_response("registration/registration_form.html",
40
 
                              { 'registration_form': form },
 
41
    return render_to_response('registration/registration_form.html',
 
42
                              {'registration_form': form},
41
43
                              context_instance=RequestContext(request))
42
44
 
 
45
 
43
46
def developers(request):
44
 
    """
45
 
    This reads out some json files in the SVN directory, and returns it
46
 
    as a wl_markdown_object. This replaces the wiki developers list
47
 
    """
48
 
    
49
 
    # Get locale and translator names from each .json file and 
 
47
    """This reads out some json files in the SVN directory, and returns it as a
 
48
    wl_markdown_object.
 
49
 
 
50
    This replaces the wiki developers list
 
51
 
 
52
    """
 
53
 
 
54
    # Get locale and translator names from each .json file and
50
55
    # store them in one list.
51
 
    txt = ""
 
56
    txt = ''
52
57
    transl_files = []
53
58
    transl_list = []
54
 
    path = os.path.normpath(WIDELANDS_SVN_DIR + "txts/translators/")
 
59
    path = os.path.normpath(WIDELANDS_SVN_DIR + 'i18n/locales/')
55
60
    try:
56
61
        transl_files = os.listdir(path)
57
62
        if transl_files:
58
63
            for fname in transl_files:
59
 
                if fname.endswith(".json"):
60
 
                    with open(path + "/" + fname,"r") as f:
61
 
                        json_data = json.load(f)["locale-translators"]
62
 
    
63
 
                    if json_data["translator-list"] != "translator-credits":
 
64
                if fname.endswith('.json'):
 
65
                    with open(path + '/' + fname, 'r') as f:
 
66
                        json_data = json.load(f)
 
67
                    try:
 
68
                        if json_data['translator-list'] != 'translator-credits':
 
69
                            if not 'your-language-name-in-english' in json_data:
 
70
                                transl_list = ['KeyError']
 
71
                                break
64
72
                            transl_list.append(json_data)
 
73
                    except KeyError:
 
74
                        transl_list = ['KeyError']
 
75
                        break
 
76
 
 
77
            # No KeyError -> Sort the list
 
78
            if 'KeyError' in transl_list:
 
79
                txt = 'Some Translator key is wrong, please contact the Developers.\n'
 
80
            else:
 
81
                transl_list.sort(key=itemgetter(
 
82
                    'your-language-name-in-english'))
 
83
 
65
84
        else:
66
 
            txt = "No files for translators found!"
 
85
            txt = 'No files for translators found!\n'
67
86
    except OSError:
68
 
        txt = txt + "Couldn't find translators directory!"
 
87
        txt = txt + "Couldn't find translators directory!\n"
69
88
 
70
 
               
71
89
    # Get other developers, put in the translators list
72
 
    # at given position and prepaire all for wl_markdown
 
90
    # at given position and prepare all for wl_markdown
73
91
    try:
74
 
        f = open(WIDELANDS_SVN_DIR + "txts/developers.json", "r")
75
 
        json_data = json.load(f)["developers"]
76
 
        f.close()
77
 
    
 
92
        with open(WIDELANDS_SVN_DIR + 'txts/developers.json', 'r') as f:
 
93
            json_data = json.load(f)['developers']
 
94
 
78
95
        for head in json_data:
79
96
            # Add first header
80
 
            txt = txt + "##" + head["heading"] + "\n"
81
 
            # Inserting Translators
82
 
            if head["heading"] == "Translators":
 
97
            txt = txt + '##' + head['heading'] + '\n'
 
98
            # Inserting Translators if there was no error
 
99
            if head['heading'] == 'Translators' and 'KeyError' not in transl_list:
83
100
                for values in (transl_list):
84
101
                    # Add subheader for locale
85
 
                    txt = txt + "### " + values["your-language-name"] + "\n"
 
102
                    txt = txt + '### ' + \
 
103
                        values['your-language-name-in-english'] + '\n'
86
104
                    # Prepaire the names for wl_markdown
87
 
                    txt = txt + "* " + values["translator-list"].replace('\n', '\n* ') + "\n"
88
 
                    
 
105
                    txt = txt + '* ' + \
 
106
                        values['translator-list'].replace('\n', '\n* ') + '\n'
 
107
 
89
108
            # Add a subheader or/and the member(s)
90
 
            for entry in head["entries"]:
91
 
                if "subheading" in entry.keys():
92
 
                    txt = txt + "###" + entry["subheading"] + "\n"
93
 
                if "members" in entry.keys():
94
 
                    for name in entry["members"]:
95
 
                        txt = txt + "* " + name + "\n"
 
109
            for entry in head['entries']:
 
110
                if 'subheading' in entry.keys():
 
111
                    txt = txt + '###' + entry['subheading'] + '\n'
 
112
                if 'members' in entry.keys():
 
113
                    for name in entry['members']:
 
114
                        txt = txt + '* ' + name + '\n'
96
115
    except IOError:
97
116
        txt = txt + "Couldn't find developer file!"
98
 
                    
99
 
    txt = do_wl_markdown(txt,custom=False)
100
 
 
101
 
    return render_to_response("mainpage/developers.html",
102
 
                              {"developers": txt},
 
117
 
 
118
    txt = do_wl_markdown(txt, custom=False)
 
119
 
 
120
    return render_to_response('mainpage/developers.html',
 
121
                              {'developers': txt},
103
122
                              context_instance=RequestContext(request)
104
 
    )
105
 
 
 
123
                              )
106
124
 
107
125
 
108
126
def changelog(request):
109
 
    """
110
 
    This reads out the changelog in the SVN directory, and returns it
111
 
    as a wl_markdown_object. This replaces the wiki changelog
112
 
    """
113
 
    data = open(WIDELANDS_SVN_DIR + "ChangeLog", "r").read()
114
 
    return render_to_response("mainpage/changelog.html",
115
 
                              {"changelog": data},
 
127
    """This reads out the changelog in the SVN directory, and returns it as a
 
128
    wl_markdown_object.
 
129
 
 
130
    This replaces the wiki changelog
 
131
 
 
132
    """
 
133
    data = open(WIDELANDS_SVN_DIR + 'ChangeLog', 'r').read()
 
134
    return render_to_response('mainpage/changelog.html',
 
135
                              {'changelog': data},
116
136
                              context_instance=RequestContext(request)
117
 
    )
118
 
 
 
137
                              )