~widelands-dev/widelands-website/update_beautifulsoup4

« back to all changes in this revision

Viewing changes to wlmaps/views.py

  • Committer: Shevonar
  • Date: 2012-04-24 21:14:22 UTC
  • mto: This revision was merged to the branch mainline in revision 330.
  • Revision ID: infomh@anmaruco.de-20120424211422-d80y5yd8x9q9gzpx
Maps modul reworked and adjusted to new style.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from django.contrib.auth.decorators import login_required
9
9
from django.http import HttpResponseRedirect, HttpResponseNotAllowed, HttpResponse, HttpResponseBadRequest
10
10
from django.core.urlresolvers import reverse
11
 
from django.utils import simplejson as json
12
11
from django.db import IntegrityError
13
 
import Image
14
12
import models
15
 
 
16
 
from widelandslib.map import WidelandsMap, WlMapLibraryException
 
13
from pybb.util import paginate
 
14
from settings import MAPS_PER_PAGE
17
15
 
18
16
import os
19
 
from cStringIO import StringIO
20
17
import zipfile
21
18
 
22
 
from settings import WIDELANDS_SVN_DIR, MEDIA_ROOT, MEDIA_URL
23
 
 
24
19
 
25
20
#########
26
21
# Views #
27
22
#########
28
23
def index( request ):
29
 
    objects = models.Map.objects.all()
 
24
    maps = models.Map.objects.all()
 
25
    page, paginator = paginate(maps, request, MAPS_PER_PAGE,
 
26
                               total_count=maps.count())
30
27
    return render_to_response("wlmaps/index.html",
31
 
                { "object_list": objects, },
 
28
                { "maps": page.object_list, 
 
29
                  "page": page,
 
30
                  "paginator": paginator,
 
31
                },
32
32
                context_instance = RequestContext(request))
33
33
 
34
34
def rate( request, map_slug ):
88
88
    return response
89
89
 
90
90
 
91
 
 
92
91
def view(request, map_slug):
93
 
    m = get_object_or_404( models.Map, slug = map_slug )
94
 
 
95
 
    if m.rating.votes > 0:
96
 
        avg = "%.1f" %( float(m.rating.score) /m.rating.votes )
97
 
    else:
98
 
        avg = "0"
 
92
    map = get_object_or_404( models.Map, slug = map_slug )
99
93
 
100
94
    context = {
101
 
        "average_rating": avg,
102
 
        "object": m,
 
95
        #"average_rating": _average_rating( map.rating ),
 
96
        "map": map,
103
97
    }
104
98
    return render_to_response( "wlmaps/map_detail.html",
105
99
                              context,
106
100
                              context_instance=RequestContext(request))
107
101
 
 
102
 
108
103
@login_required
109
104
def upload( request ):
110
 
    """
111
 
    Uploads a map. This is an ajax post and returns an JSON object
112
 
    with the following values.
113
 
 
114
 
    success_code - integer (0 means success else error)
115
 
    error_msg - if success_code = 1 this contains an descriptive error
116
 
    map_id - id of newly uploaded map
117
 
    """
118
 
    def JsonReply( success_code, error_msg = None, **kwargs):
119
 
        d = kwargs
120
 
        d['success_code'] = success_code
121
 
        if error_msg != None:
122
 
            d['error_msg'] = error_msg
123
 
        return HttpResponse( json.dumps(d), mimetype="application/javascript" )
124
 
 
125
 
    if request.method != "POST":
126
 
        return HttpResponseNotAllowed(["post"])
127
 
 
128
 
    form = UploadMapForm( request.POST )
129
 
    test = request.POST.get("test", False)
130
 
    comment = request.POST.get("comment",u"")
131
 
 
132
 
    if "mapfile" in request.FILES:
133
 
        mf = request.FILES["mapfile"]
134
 
 
135
 
        mfdata = mf.read()
136
 
 
137
 
        m = WidelandsMap()
138
 
        try:
139
 
            m.load(StringIO(mfdata))
140
 
        except WlMapLibraryException:
141
 
            return JsonReply( 3, "Invalid Map File" )
142
 
 
143
 
        # Draw the minimaps
144
 
        mm = m.make_minimap(WIDELANDS_SVN_DIR)
145
 
        mm_path = "%s/wlmaps/minimaps/%s.png" % (MEDIA_ROOT,m.name)
146
 
        mm_url = "/wlmaps/minimaps/%s.png" % m.name
147
 
        file_path = "%s/wlmaps/maps/%s.wmf" % (MEDIA_ROOT,m.name)
148
 
 
149
 
        if not test:
150
 
            f = open(file_path,"wb")
151
 
            f.write(mfdata)
152
 
            f.close()
153
 
            i = Image.fromarray(mm)
154
 
            i.save(mm_path)
155
 
 
156
 
        # Create the map
157
 
        try:
158
 
            nm = models.Map.objects.create(
159
 
                name = m.name,
160
 
                author = m.author,
161
 
                w = m.w,
162
 
                h = m.h,
163
 
                nr_players = m.nr_players,
164
 
                descr = m.descr,
165
 
                minimap = mm_url,
166
 
                file = file_path,
167
 
                world_name = m.world_name,
168
 
 
169
 
                uploader = request.user,
170
 
                uploader_comment = comment,
171
 
            )
172
 
        except IntegrityError:
173
 
            return JsonReply(2, "Map with the same name already exists in database!")
174
 
 
175
 
        nm.save()
176
 
 
177
 
        return JsonReply(0, map_id = nm.pk )
178
 
 
179
 
    return JsonReply(1, "No mapfile in request!")
 
105
    if request.method == 'POST':
 
106
        form = UploadMapForm(request.POST, request.FILES)
 
107
        if form.is_valid():
 
108
            map = form.save(commit = False)
 
109
            map.uploader = request.user
 
110
            map.save()
 
111
            return HttpResponseRedirect(map.get_absolute_url())
 
112
 
 
113
    else:
 
114
        form = UploadMapForm()
 
115
    
 
116
    context = { 'form': form, }
 
117
    return render_to_response( "wlmaps/upload.html",
 
118
                              context,
 
119
                              context_instance=RequestContext(request))