Пример #1
0
def create(request, template='contacts/person/create.html'):
    """Create a person.

    :param template: A custom template.
    """
    user = request.user
    if not user.has_perm('{}.add_person'.format(Person._meta.app_label)):
        return HttpResponseForbidden()

    if request.method == 'POST':
        form = PersonCreateForm(request.POST)

        if form.is_valid():
            p = form.save(commit=False)
            p.slug = slugify("%s %s" % (p.first_name, p.last_name))
            p.save()
            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = PersonCreateForm()

    kwvars = {
        'form': form
    }

    return render_to_response(template, kwvars, RequestContext(request))
Пример #2
0
def create(request, template='contacts/person/create.html'):
    """Create a person.

    :param template: A custom template.
    """


    user = request.user
    if not user.has_perm('contacts.add_person'):
        raise PermissionDenied

    if request.method == 'POST':
        form = PersonCreateForm(request.POST)

        if form.is_valid():
            p = form.save()
            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = PersonCreateForm()

    kwvars = {
        'form': form
    }

    return render_to_response(template, kwvars, RequestContext(request))
def create(request, template='contacts/person/create.html'):
    """Create a person.

    :param template: A custom template.

    https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#more-than-one-foreign-key-to-the-same-model
    """

    if not request.user.is_authenticated():
        return HttpResponseRedirect('/login/?next=%s' % request.path)

    user = request.user
    if not user.has_perm('add_person'):
        return HttpResponseForbidden()

    if request.method == 'POST':
        form = PersonCreateForm(request.POST)

        if form.is_valid():
            p = form.save(commit=False)
            base_slug = slugify("%s %s" % (p.first_name, p.last_name))

            # hem de comprovar que no existeix cap persona amb aquest nom. Si no, hem d'afegir -1
            tmp_slug = base_slug
            trobat = True
            counter = 0
            while trobat:
                try:
                    Person.objects.get(slug__iexact=tmp_slug)
                    counter = counter + 1
                    tmp_slug = "%s-%s" % (base_slug, str(counter))
                except Person.DoesNotExist:
                    trobat = False

            p.slug = tmp_slug
            p.user_add = user
            p.user_modify = user
            p.date_registration = datetime.today()
            p.save()
            return HttpResponseRedirect(p.get_update_url())
    else:
        form = PersonCreateForm()

    kwvars = {
        'form': form
    }

    return render_to_response(template, kwvars, RequestContext(request))
Пример #4
0
def create(request, template='contacts/person/create.html'):
    """Create a person.

    :param template: A custom template.
    """
    user = request.user
    if not user.has_perm('{}.add_person'.format(Person._meta.app_label)):
        return HttpResponseForbidden()

    if request.method == 'POST':
        form = PersonCreateForm(request.POST)

        if form.is_valid():
            p = form.save(commit=False)
            p.slug = slugify("%s %s" % (p.first_name, p.last_name))
            p.save()
            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = PersonCreateForm()

    kwvars = {'form': form}

    return render_to_response(template, kwvars, RequestContext(request))