Beispiel #1
0
def xml_to_coordinates(request):
    data = None
    if 'track-from-xml' in request.FILES:
        t = XMLTrack(request.FILES['track-from-xml'].read())
        data = t.get_track()['rout']
    return HttpResponse(json.dumps(data),
                        content_type="text/json")
Beispiel #2
0
def track_edit(request, track_id=None):
    errors = []
    messages = []
    if track_id is not None:
        track = get_object_or_404(Track, id=track_id)
        title = u'Редактирование маршрута'
    else:
        track=None
        title = u'Новый маршрут'
    trans_forms = []
    
    if request.method == "POST":
        form = TrackForm(request.POST, request.FILES, instance=track)
        for lang_code, lang_name in settings.LANGUAGES:
            if lang_code != settings.LANGUAGE_CODE:
                if track is not None:
                    trans, c = Translation.objects.get_or_create(track=track, language=lang_code)
                else:
                    trans = None
                trans_forms.append(TransFormTrack(request.POST, instance=trans, prefix=lang_code))
        if form.is_valid() and all([i.is_valid() for i in trans_forms]):
            _track = form.save()
            for i in trans_forms:
                t = i.save()
                if not t.language:
                    t.language = i.prefix
                if t.track is None:
                    t.track = _track
                t.save()

            xml_file = request.FILES.get('xml_coordinates', None)
            created = False
            if track is None:
                track = form.instance
                created = True
            if xml_file:
                try:
                    t = XMLTrack(xml_file.read())                
                    rout = t.get_track()['rout']
                    track.coordinates = json.dumps(rout)
                    track.full_coordinates = json.dumps(rout)
                    track.save()
                except:
                    errors.append(u"Ошибка разбора XML.")
            if not errors:
                messages.append(u"Изменения успешно сохранены.")
            if request.POST.get('submit', 'to_current_page') == 'to_section' and not errors:
                return HttpResponseRedirect(reverse('manager_tracks'))
            if created and not errors:
                return HttpResponseRedirect(reverse('track-edit', 
                                                    args=[form.instance.id]))

    else:
        form = TrackForm(instance=track)
        for lang_code, lang_name in settings.LANGUAGES:
            if lang_code != settings.LANGUAGE_CODE:
                if track is not None:
                    trans, c = Translation.objects.get_or_create(track=track, language=lang_code)
                else:
                    trans = None
                trans_forms.append(TransFormTrack(instance=trans, prefix=lang_code))
    
    return render_to_response('obj_edit.html',
                              {'form': form,
                               'comments_instance': track,
                               'title': title,
                               'trans_forms': trans_forms,
                               'back_url': reverse('manager_tracks'),
                               'info': {'errors': errors,
                                        'messages': messages}
                               },
                              RequestContext(request))