~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to wlmaps/views.py

  • Committer: Holger Rapp
  • Date: 2009-04-04 14:32:27 UTC
  • Revision ID: sirver@kallisto.local-20090404143227-mf49s08keq3nv08c
Initital (unusable) version of wlmaps application

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
 
6
from django.shortcuts import render_to_response, get_object_or_404
 
7
from django.template import RequestContext
 
8
from django.contrib.auth.decorators import login_required
 
9
from django.http import HttpResponseRedirect, HttpResponseNotAllowed, HttpResponse
 
10
from django.core.urlresolvers import reverse
 
11
from django.utils import simplejson as json
 
12
from django.db import IntegrityError
 
13
import models
 
14
 
 
15
from widelandslib.Map import WidelandsMap, WlMapLibraryException 
 
16
 
 
17
import scipy
 
18
 
 
19
from settings import WIDELANDS_SVN_DIR, MEDIA_ROOT, MEDIA_URL
 
20
 
 
21
 
 
22
#########
 
23
# Views #
 
24
#########
 
25
def index( request ):
 
26
    pass
 
27
 
 
28
def view(request, map_slug):
 
29
    m = get_object_or_404( models.Map, slug = map_slug )
 
30
    
 
31
    context = {
 
32
        "object": m,
 
33
    }
 
34
    return render_to_response( "wlmaps/map_detail.html", 
 
35
                              context, 
 
36
                              context_instance=RequestContext(request))
 
37
 
 
38
@login_required
 
39
def upload( request ):
 
40
    """
 
41
    Uploads a map. This is an ajax post and returns an JSON object
 
42
    with the following values. 
 
43
 
 
44
    success_code - integer (0 means success else error)
 
45
    error_msg - if success_code = 1 this contains an descriptive error
 
46
    map_id - id of newly uploaded map
 
47
    """
 
48
    def JsonReply( success_code, error_msg = None, **kwargs):
 
49
        d = kwargs
 
50
        d['success_code'] = success_code
 
51
        if error_msg != None:
 
52
            d['error_msg'] = error_msg
 
53
        return HttpResponse( json.dumps(d), mimetype="application/javascript" )
 
54
 
 
55
    if request.method != "POST":
 
56
        return HttpResponseNotAllowed(["post"])
 
57
    
 
58
    form = UploadMapForm( request.POST )
 
59
    test = request.POST.get("test", False)
 
60
 
 
61
    if "mapfile" in request.FILES: 
 
62
        mf = request.FILES["mapfile"]
 
63
        
 
64
        m = WidelandsMap()
 
65
        try:
 
66
            m.load(mf)
 
67
        except WlMapLibraryException:
 
68
            return JsonReply( 3, "Invalid Map File" )
 
69
 
 
70
        # Draw the minimaps
 
71
        mm = m.make_minimap(WIDELANDS_SVN_DIR)
 
72
        mm_path = "%s/wlmaps/minimaps/%s.png" % (MEDIA_ROOT,m.name)
 
73
        mm_url = "/wlmaps/minimaps/%s.png" % m.name
 
74
        
 
75
        if not test:
 
76
            scipy.misc.pilutil.imsave(mm_path, mm)
 
77
        
 
78
 
 
79
        # Create the map
 
80
        try:
 
81
            nm = models.Map.objects.create(
 
82
                name = m.name,
 
83
                author = m.author,
 
84
                w = m.w,
 
85
                h = m.h,
 
86
                descr = m.descr,
 
87
                minimap = mm_url,
 
88
                world_name = m.world_name,
 
89
 
 
90
                uploader = request.user,
 
91
                uploader_comment = ""
 
92
            )
 
93
        except IntegrityError:
 
94
            return JsonReply(2, "Map with the same name already exists in database!")
 
95
 
 
96
        nm.save()
 
97
        return JsonReply(0, map_id = nm.pk )
 
98
    
 
99
    return JsonReply(1, "No mapfile in request!")