~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wlmaps/views.py

  • Committer: Holger Rapp
  • Date: 2009-02-19 15:31:42 UTC
  • Revision ID: sirver@h566336-20090219153142-dc8xuabldnw5t395
Initial commit of new widelands homepage

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python -tt
2
 
# encoding: utf-8
3
 
#
4
 
 
5
 
from forms import UploadMapForm, EditCommentForm
6
 
from django.shortcuts import render, get_object_or_404
7
 
from django.contrib.auth.decorators import login_required
8
 
from django.http import HttpResponseRedirect, HttpResponseNotAllowed, HttpResponse, HttpResponseBadRequest
9
 
from django.urls import reverse
10
 
import models
11
 
from settings import MAPS_PER_PAGE
12
 
from wl_utils import get_real_ip
13
 
import os
14
 
 
15
 
 
16
 
#########
17
 
# Views #
18
 
#########
19
 
def index(request):
20
 
    maps = models.Map.objects.all()
21
 
    return render(request, 'wlmaps/index.html',
22
 
                              {'maps': maps,
23
 
                               'maps_per_page': MAPS_PER_PAGE,
24
 
                               })
25
 
 
26
 
 
27
 
def download(request, map_slug):
28
 
    """Very simple view that just returns the binary data of this map and
29
 
    increases the download count."""
30
 
    m = get_object_or_404(models.Map, slug=map_slug)
31
 
 
32
 
    file = open(m.file.path, 'rb')
33
 
    data = file.read()
34
 
    filename = os.path.basename('%s.wmf' % m.name)
35
 
 
36
 
    # Remember that this has been downloaded
37
 
    m.nr_downloads += 1
38
 
    m.save(update_fields=['nr_downloads'])
39
 
 
40
 
    response = HttpResponse(data, content_type='application/octet-stream')
41
 
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename
42
 
 
43
 
    return response
44
 
 
45
 
 
46
 
def view(request, map_slug):
47
 
    map = get_object_or_404(models.Map, slug=map_slug)
48
 
    context = {
49
 
        'map': map,
50
 
    }
51
 
    return render(request, 'wlmaps/map_detail.html',
52
 
                              context)
53
 
 
54
 
 
55
 
@login_required
56
 
def edit_comment(request, map_slug):
57
 
    map = get_object_or_404(models.Map, slug=map_slug)
58
 
    if request.method == 'POST':
59
 
        form = EditCommentForm(request.POST)
60
 
        if form.is_valid():
61
 
            map.uploader_comment = form.cleaned_data['uploader_comment']
62
 
            map.save(update_fields=['uploader_comment'])
63
 
            return HttpResponseRedirect(map.get_absolute_url())
64
 
    else:
65
 
        form = EditCommentForm(instance=map)
66
 
 
67
 
    context = {'form': form, 'map': map}
68
 
 
69
 
    return render(request, 'wlmaps/edit_comment.html',
70
 
                              context)
71
 
 
72
 
 
73
 
@login_required
74
 
def upload(request):
75
 
    if request.method == 'POST':
76
 
        form = UploadMapForm(request.POST, request.FILES)
77
 
        if form.is_valid():
78
 
            map = form.save(commit=False)
79
 
            map.uploader = request.user
80
 
            map.save()
81
 
            return HttpResponseRedirect(map.get_absolute_url())
82
 
    else:
83
 
        form = UploadMapForm()
84
 
 
85
 
    context = {'form': form, }
86
 
    return render(request, 'wlmaps/upload.html',
87
 
                              context)