예제 #1
0
파일: views.py 프로젝트: fiee/fiee-dorsale
def edit_item(request, app_name='', model_name='', object_id=None, title='', template='dorsale/edit_item.html'):
    """
    Allow to edit one object: `app_name.model_name(id=object_id)`.

    Available variables in the template:
    :item: the requested object
    :item_type: ContentType of the requested object
    :form: ModelForm for this object
    :notes: Notes on this object
    """
    action = request.META['PATH_INFO']
    object_model = get_model(app_name, model_name)
    if not title:
        title = _('Edit %s') % object_model._meta.verbose_name
    if object_id:
        item = object_model.objects.get(pk=object_id)
        item_type = ContentType.objects.get_for_model(item)
        if hasattr(item, 'notes'):
            notes = item.notes.all()
        # else:
        #    notes = Note.objects.filter(content_type__pk=item_type.id, object_id=object_id)
        if request.method == 'POST':
            form = ModelFormFactory(object_model, request.POST, request.FILES, user=request.user, instance=item)
            if form.is_valid():
                form.save()
                messages.success(request, _(u"%(model_name)s %(model_id)s saved.") % {'model_name':object_model._meta.verbose_name, 'model_id':object_id})
                return HttpResponseRedirect(request.path)
        else:
            form = ModelFormFactory(object_model, user=request.user, instance=item)
        return render(request, template, locals())
    else:
        return HttpResponse(status=404)
예제 #2
0
파일: views.py 프로젝트: fiee/fiee-dorsale
def show_item(request, app_name='', model_name='', object_id=None, template='dorsale/show_item.html', _c=0):
    """
    Display one object: `app_name.model_name(id=object_id)`.
    Render 404 if object is not available. Allow customization by `template`.

    Available variables in the template:
    :item: the requested object
    :item_type: ContentType of the requested object
    :form: ModelForm for this object
    :notes: Notes on this object
    """
    if not object_id:
        return render_404(request, locals())
    object_model = get_model(app_name, model_name)
    try:
        item = object_model.objects.mine(request.user.id).get(pk=object_id)
        item_type = ContentType.objects.get_for_model(item)
        if hasattr(item, 'notes'):
            notes = item.notes.all()
        # else:
        #    notes = Note.objects.filter(content_type__pk=item_type.id, object_id=object_id)
    except:
        return render_404(request, locals())
    form = ModelFormFactory(object_model, user=request.user, instance=item, disabled=True)
    if not form.visible_fields() and _c < 3:  # sometimes it works only in the second try, why?
        return show_item(request, app_name, model_name, object_id, template, _c + 1)
    return render(request, template, locals())
예제 #3
0
def show_item(request, app_name='', model_name='', object_id=None, template='dorsale/show_item.html', _c=0):
    """
    Display one object: `app_name.model_name(id=object_id)`.
    Render 404 if object is not available. Allow customization by `template`.

    Available variables in the template:
    :item: the requested object
    :item_type: ContentType of the requested object
    :form: ModelForm for this object
    :notes: Notes on this object
    """
    if not object_id:
        return render_404(request, locals())
    object_model = get_model(app_name, model_name)
    try:
        item = object_model.objects.mine(request.user.id).get(pk=object_id)
        item_type = ContentType.objects.get_for_model(item)
        if hasattr(item, 'notes'):
            notes = item.notes.all()
        # else:
        #    notes = Note.objects.filter(content_type__pk=item_type.id, object_id=object_id)
    except:
        return render_404(request, locals())
    form = ModelFormFactory(object_model, user=request.user, instance=item, disabled=True)
    if not form.visible_fields() and _c < 3:  # sometimes it works only in the second try, why?
        return show_item(request, app_name, model_name, object_id, template, _c + 1)
    return render_to_response(template, locals(), context_instance=RequestContext(request))
예제 #4
0
파일: views.py 프로젝트: fiee/fiee-adhesive
def json_save(request, id):
    """
    Save a note. User needs permissions to add/change a note.
    """
    data = check_request(request)
    try:
        note = Note.objects.get(id=id)
        if not request.user.has_perm('adhesive.change_note'):
            return JSONResponse(data, status=401)
        form = ModelFormFactory(Note, request.POST, user=request.user, instance=note)
        data['is_new'] = False
    except (ValueError, ObjectDoesNotExist):
        if not request.user.has_perm('adhesive.add_note'):
            return JSONResponse(data, status=401)
        form = ModelFormFactory(Note, request.POST, user=request.user)
        form.id = None
        data['is_new'] = True
    if form.is_valid():
        note = form.save()
        data['id'] = note.id
        data['placement'] = note.placement
        data['info'] = note.info_text()
    else:
        data['error'] = form.errors
        return JSONResponse(data, status=400)
    return JSONResponse(data)
예제 #5
0
def edit_item(request, app_name='', model_name='', object_id=None, title='', template='dorsale/edit_item.html'):
    """
    Allow to edit one object: `app_name.model_name(id=object_id)`.

    Available variables in the template:
    :item: the requested object
    :item_type: ContentType of the requested object
    :form: ModelForm for this object
    :notes: Notes on this object
    """
    action = request.META['PATH_INFO']
    object_model = get_model(app_name, model_name)
    if not title:
        title = _('Edit %s') % object_model._meta.verbose_name
    if object_id:
        item = object_model.objects.get(pk=object_id)
        item_type = ContentType.objects.get_for_model(item)
        if hasattr(item, 'notes'):
            notes = item.notes.all()
        # else:
        #    notes = Note.objects.filter(content_type__pk=item_type.id, object_id=object_id)
        if request.method == 'POST':
            form = ModelFormFactory(object_model, request.POST, request.FILES, user=request.user, instance=item)
            if form.is_valid():
                form.save()
                messages.success(request, _(u"%(model_name)s %(model_id)s saved.") % {'model_name':object_model._meta.verbose_name, 'model_id':object_id})
                return HttpResponseRedirect(request.path)
        else:
            form = ModelFormFactory(object_model, user=request.user, instance=item)
        return render_to_response(template, locals(), context_instance=RequestContext(request))
    else:
        return HttpResponse(status=404)
예제 #6
0
파일: views.py 프로젝트: fiee/fiee-dorsale
def new_item(request, app_name='', model_name='', title='', unique_fields=[], postprocess=None, template='dorsale/edit_item.html'):
    """
    Generate a new object of app_name.model_name and redirect to its single view.

    :title: (str) form title (defaults to "New model_name")
    :unique_fields: (list of str) check if an object with the same fields exists
    :postprocess: (callable with userid, itemid) call this after saving the new item

    Available variables in the template:
    :form: ModelForm
    :action: calling URL
    :app_name:
    :model_name:
    :title:
    """
    action = request.META['PATH_INFO']
    object_model = get_model(app_name, model_name)
    if not title:
        title = _('New %s') % object_model._meta.verbose_name
    if request.method == 'POST':
        form = ModelFormFactory(object_model, request.POST, request.FILES, user=request.user)
        if form.is_valid():
            try:
                params = {}
                if unique_fields:
                    # customize: which fields should be unique?
                    for pn in unique_fields:
                        params[pn] = form.cleaned_data[pn]
                    item = object_model.objects.get(**params)
                    messages.error(request, _('This %s already exists!') % object_model._meta.verbose_name)
                else:
                    raise ObjectDoesNotExist()
                del params
            except ObjectDoesNotExist:
                item = form.save()
                messages.success(request, _(u"New %s saved.") % object_model._meta.verbose_name)
                if postprocess:
                    postprocess(request.user.id, item.pk)
                return HttpResponseRedirect('/%s/%s/%d/' % (app_name, model_name, item.id))
    else:
        form = ModelFormFactory(object_model, user=request.user,)
    del object_model
    del postprocess
    return render(request, template, locals())
예제 #7
0
def new_item(request, app_name='', model_name='', title='', unique_fields=[], postprocess=None, template='dorsale/edit_item.html'):
    """
    Generate a new object of app_name.model_name and redirect to its single view.

    :title: (str) form title (defaults to "New model_name")
    :unique_fields: (list of str) check if an object with the same fields exists
    :postprocess: (callable with userid, itemid) call this after saving the new item

    Available variables in the template:
    :form: ModelForm
    :action: calling URL
    :app_name:
    :model_name:
    :title:
    """
    action = request.META['PATH_INFO']
    object_model = get_model(app_name, model_name)
    if not title:
        title = _('New %s') % object_model._meta.verbose_name
    if request.method == 'POST':
        form = ModelFormFactory(object_model, request.POST, request.FILES, user=request.user)
        if form.is_valid():
            try:
                params = {}
                if unique_fields:
                    # customize: which fields should be unique?
                    for pn in unique_fields:
                        params[pn] = form.cleaned_data[pn]
                    item = object_model.objects.get(**params)
                    messages.error(request, _('This %s already exists!') % object_model._meta.verbose_name)
                else:
                    raise ObjectDoesNotExist()
                del params
            except ObjectDoesNotExist:
                item = form.save()
                messages.success(request, _(u"New %s saved.") % object_model._meta.verbose_name)
                if postprocess:
                    postprocess(request.user.id, item.pk)
                return HttpResponseRedirect('/%s/%s/%d/' % (app_name, model_name, item.id))
    else:
        form = ModelFormFactory(object_model, user=request.user,)
    del object_model
    del postprocess
    return render_to_response(template, locals(), context_instance=RequestContext(request))