def upload_map(request, server_id): server = get_object_or_404(Server.active.select_related(), pk=server_id) moderator = server.moderators.filter(user=request.user) if server.owner != request.user and (not moderator or not moderator[0].map_upload_allowed): raise Http404 moderator = moderator[0] if moderator else None if request.method == 'POST': form = MapUploadForm(request.POST, request.FILES) if form.is_valid(): map_file = form.cleaned_data['map_file'] mod_name = server.mod.title maps_path = os.path.join(MEDIA_ROOT, 'mods', mod_name, 'servers', server.owner.username, '{0}'.format(server_id), server.random_key, 'maps') if not os.path.exists(maps_path): os.makedirs(maps_path) with open(os.path.join(maps_path, map_file.name), 'wb') as f: f.write(map_file.read()) map_obj = Map(server=server, name=os.path.splitext(map_file.name)[0]) map_obj.save() messages.success(request, 'Map was successfully uploaded.') else: form = MapUploadForm() return render_to_response('mod/server_detail_upload_map.html', { 'server': server, 'form': form, 'moderator': moderator }, context_instance=RequestContext(request))
def upload_map(request, server_id): server = get_object_or_404(Server.by_owner.for_user(request.user).select_related().filter(is_active=True), pk=server_id) if request.method == 'POST': form = MapUploadForm(request.POST, request.FILES) if form.is_valid(): map_file = form.cleaned_data['map_file'] mod_name = server.mod.title maps_path = os.path.join(MEDIA_ROOT, 'mods', mod_name, 'servers', request.user.username, '{0}'.format(server_id), 'maps') if not os.path.exists(maps_path): os.makedirs(maps_path) with open(os.path.join(maps_path, map_file.name), 'wb') as f: f.write(map_file.read()) map_obj = Map(server=server, name=os.path.splitext(map_file.name)[0]) map_obj.save() messages.success(request, 'Map was successfully uploaded.') else: form = MapUploadForm() return render_to_response('mod/server_detail_upload_map.html', { 'server': server, 'form': form }, context_instance=RequestContext(request))