~widelands-dev/widelands-website/trunk

120 by Holger Rapp
Initital (unusable) version of wlmaps application
1
#!/usr/bin/env python -tt
2
# encoding: utf-8
3
#
4
532.1.1 by franku
converted to python 3.6 using 2to3 script
5
from .forms import UploadMapForm, EditCommentForm
489.1.1 by franku
url fixes; render_to_response() -> render(); disabled tracking
6
from django.shortcuts import render, get_object_or_404
120 by Holger Rapp
Initital (unusable) version of wlmaps application
7
from django.contrib.auth.decorators import login_required
157 by Holger Rapp
Backend for map voting is completed. 'Only' the UI is missing
8
from django.http import HttpResponseRedirect, HttpResponseNotAllowed, HttpResponse, HttpResponseBadRequest
489.1.13 by franku
django.conf.urlresolvers -> django.urls
9
from django.urls import reverse
530.1.1 by kaputtnik
mv main files into mainpage directory; apply needed changes
10
from django.conf import settings
532.1.1 by franku
converted to python 3.6 using 2to3 script
11
from . import models
530.1.1 by kaputtnik
mv main files into mainpage directory; apply needed changes
12
13
from mainpage.wl_utils import get_real_ip
121 by Holger Rapp
Wlmaps application is now in a usuable state
14
import os
120 by Holger Rapp
Initital (unusable) version of wlmaps application
15
16
17
#########
18
# Views #
19
#########
402.1.2 by franku
add_hint_to_map_views
20
def index(request):
325.1.9 by Shevonar
Maps modul reworked and adjusted to new style.
21
    maps = models.Map.objects.all()
489.1.1 by franku
url fixes; render_to_response() -> render(); disabled tracking
22
    return render(request, 'wlmaps/index.html',
402.1.2 by franku
add_hint_to_map_views
23
                              {'maps': maps,
530.1.1 by kaputtnik
mv main files into mainpage directory; apply needed changes
24
                               'maps_per_page': settings.MAPS_PER_PAGE,
489.1.1 by franku
url fixes; render_to_response() -> render(); disabled tracking
25
                               })
402.1.2 by franku
add_hint_to_map_views
26
27
28
def download(request, map_slug):
29
    """Very simple view that just returns the binary data of this map and
30
    increases the download count."""
31
    m = get_object_or_404(models.Map, slug=map_slug)
32
33
    file = open(m.file.path, 'rb')
161 by Holger Rapp
Attempt to fix wrong downloads when getting maps
34
    data = file.read()
402.1.2 by franku
add_hint_to_map_views
35
    filename = os.path.basename('%s.wmf' % m.name)
173.2.3 by Holger Rapp
Made the site compatible to django 1.1 and all the various packages
36
161 by Holger Rapp
Attempt to fix wrong downloads when getting maps
37
    # Remember that this has been downloaded
121 by Holger Rapp
Wlmaps application is now in a usuable state
38
    m.nr_downloads += 1
459 by franku
Send notifications only if the map is newly saved, not when updating some fields, e.g. nr_downloads
39
    m.save(update_fields=['nr_downloads'])
173.2.3 by Holger Rapp
Made the site compatible to django 1.1 and all the various packages
40
404.2.12 by franku
runthrough deprecation timeline; fixed some things; added NOCOMMs
41
    response = HttpResponse(data, content_type='application/octet-stream')
363.1.1 by Shevonar
several map uploader fixes
42
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename
121 by Holger Rapp
Wlmaps application is now in a usuable state
43
44
    return response
173.2.3 by Holger Rapp
Made the site compatible to django 1.1 and all the various packages
45
121 by Holger Rapp
Wlmaps application is now in a usuable state
46
120 by Holger Rapp
Initital (unusable) version of wlmaps application
47
def view(request, map_slug):
402.1.2 by franku
add_hint_to_map_views
48
    map = get_object_or_404(models.Map, slug=map_slug)
120 by Holger Rapp
Initital (unusable) version of wlmaps application
49
    context = {
402.1.2 by franku
add_hint_to_map_views
50
        'map': map,
120 by Holger Rapp
Initital (unusable) version of wlmaps application
51
    }
489.1.1 by franku
url fixes; render_to_response() -> render(); disabled tracking
52
    return render(request, 'wlmaps/map_detail.html',
53
                              context)
402.1.2 by franku
add_hint_to_map_views
54
55
56
@login_required
57
def edit_comment(request, map_slug):
58
    map = get_object_or_404(models.Map, slug=map_slug)
59
    if request.method == 'POST':
60
        form = EditCommentForm(request.POST)
61
        if form.is_valid():
62
            map.uploader_comment = form.cleaned_data['uploader_comment']
460 by franku
Prevend sending notices also if a map-comment is edited
63
            map.save(update_fields=['uploader_comment'])
402.1.2 by franku
add_hint_to_map_views
64
            return HttpResponseRedirect(map.get_absolute_url())
65
    else:
66
        form = EditCommentForm(instance=map)
67
68
    context = {'form': form, 'map': map}
69
489.1.1 by franku
url fixes; render_to_response() -> render(); disabled tracking
70
    return render(request, 'wlmaps/edit_comment.html',
71
                              context)
402.1.2 by franku
add_hint_to_map_views
72
73
74
@login_required
75
def upload(request):
325.1.9 by Shevonar
Maps modul reworked and adjusted to new style.
76
    if request.method == 'POST':
77
        form = UploadMapForm(request.POST, request.FILES)
78
        if form.is_valid():
350 by Holger Rapp
Added support for PIL. Fixed some whitespace. Fixed tests for wlmaps.
79
            map = form.save(commit=False)
325.1.9 by Shevonar
Maps modul reworked and adjusted to new style.
80
            map.uploader = request.user
81
            map.save()
82
            return HttpResponseRedirect(map.get_absolute_url())
83
    else:
84
        form = UploadMapForm()
350 by Holger Rapp
Added support for PIL. Fixed some whitespace. Fixed tests for wlmaps.
85
402.1.2 by franku
add_hint_to_map_views
86
    context = {'form': form, }
489.1.1 by franku
url fixes; render_to_response() -> render(); disabled tracking
87
    return render(request, 'wlmaps/upload.html',
88
                              context)