Ejemplo n.º 1
0
def save(request):
    if settings.DEBUG and not request.POST:
        request.POST = request.GET
    if settings.REQUIRE_LOGIN and not request.user.is_authenticated():
        raise Http404
    form = SaveForm(request.POST)
    overwrite = request.POST.get('overwrite', False)
    result = ''
    if form.is_valid():
        content = request.POST.get('content', '')
        name = form.cleaned_data['name']
        user = request.user
        if not user.is_authenticated():
            user = None
        try:
            worksheet = Worksheet.objects.get(user=user, name=name)
            if overwrite:
                worksheet.content = content
            else:
                result = 'overwrite'
        except Worksheet.DoesNotExist:
            worksheet = Worksheet(user=user, name=name, content=content)
        worksheet.save()

    return JsonResponse({
        'form': form.as_json(),
        'result': result,
    })
Ejemplo n.º 2
0
def save(request):
    if settings.DEBUG and not request.POST:
        request.POST = request.GET
    if settings.REQUIRE_LOGIN and not request.user.is_authenticated():
        raise Http404
    form = SaveForm(request.POST)
    overwrite = request.POST.get('overwrite', False)
    result = ''
    general_errors = []
    if form.is_valid():
        content = request.POST.get('content', '')
        name = form.cleaned_data['name']

        if re.match(r'^[0-9A-Za-z]+[0-9A-Za-z._()\-\[\] ]*$', name) is None:
            general_errors = ["Worksheet name is invalid."]
        else:
            user = request.user
            if not user.is_authenticated():
                user = None
            try:
                worksheet = Worksheet.objects.get(user=user, name=name)
                if overwrite:
                    worksheet.content = content
                else:
                    result = 'overwrite'
            except Worksheet.DoesNotExist:
                worksheet = Worksheet(user=user, name=name, content=content)
            worksheet.save()

    return JsonResponse({
        'form': form.as_json(general_errors=general_errors),
        'result': result,
    })