1
#!/usr/bin/env python -tt
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
15
from widelandslib.Map import WidelandsMap, WlMapLibraryException
19
from settings import WIDELANDS_SVN_DIR, MEDIA_ROOT, MEDIA_URL
28
def view(request, map_slug):
29
m = get_object_or_404( models.Map, slug = map_slug )
34
return render_to_response( "wlmaps/map_detail.html",
36
context_instance=RequestContext(request))
39
def upload( request ):
41
Uploads a map. This is an ajax post and returns an JSON object
42
with the following values.
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
48
def JsonReply( success_code, error_msg = None, **kwargs):
50
d['success_code'] = success_code
52
d['error_msg'] = error_msg
53
return HttpResponse( json.dumps(d), mimetype="application/javascript" )
55
if request.method != "POST":
56
return HttpResponseNotAllowed(["post"])
58
form = UploadMapForm( request.POST )
59
test = request.POST.get("test", False)
61
if "mapfile" in request.FILES:
62
mf = request.FILES["mapfile"]
67
except WlMapLibraryException:
68
return JsonReply( 3, "Invalid Map File" )
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
76
scipy.misc.pilutil.imsave(mm_path, mm)
81
nm = models.Map.objects.create(
88
world_name = m.world_name,
90
uploader = request.user,
93
except IntegrityError:
94
return JsonReply(2, "Map with the same name already exists in database!")
97
return JsonReply(0, map_id = nm.pk )
99
return JsonReply(1, "No mapfile in request!")