예제 #1
0
파일: person.py 프로젝트: vperic/gramps
def process_person(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Person")
    context["tviews"] = _("People")
    logform = None
    if request.user.is_authenticated():
        if act == "share":
            item, handle = add_to
            context["pickform"] = PickForm("Pick a person", 
                                           Person, 
                                           ("name__surname__surname", 
                                            "name__first_name"),
                                      request.POST)     
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
        elif act == "save-share":
            item, handle = add_to # ("Family", handle)
            pickform = PickForm("Pick a person", 
                                Person, 
                                ("name__surname__surname", 
                                 "name__first_name"),
                                request.POST)
            if pickform.data["picklist"]:
                person_handle = pickform.data["picklist"]
                person = Person.objects.get(handle=person_handle)
                model = dji.get_model(item) # what model?
                obj = model.objects.get(handle=handle) # get family
                dji.add_child_ref_default(obj, person) # add person to family
                #person.parent_families.add(obj) # add family to child
                pfo = MyParentFamilies(person=person, family=obj,
                                       order=len(person.parent_families.all())+1)
                pfo.save()
                person.save_cache() # rebuild child
                obj.save_cache() # rebuild family
                return redirect("/%s/%s%s" % (item, handle, build_search(request)))
            else:
                context["pickform"] = pickform
                context["object_handle"] = handle
                context["object_type"] = "family"
                return render_to_response("pick.html", context)
        elif act in ["edit", "view"]:
            pf, nf, sf, person = get_person_forms(handle, empty=False)
            if act == "edit":
                logform = LogForm()
        elif act == "add":
            pf, nf, sf, person = get_person_forms(handle=None, protect=False, empty=True)
            logform = LogForm()
        elif act == "delete":
            pf, nf, sf, person = get_person_forms(handle, protect=False, empty=True)
            person.delete()
            return redirect("/person/%s" % build_search(request))
        elif act in ["save", "create"]: # could be create a new person
            # look up old data, if any:
            logform = LogForm(request.POST)
            if handle: 
                person = Person.objects.get(handle=handle)
                name = person.name_set.get(preferred=True)
                surname = name.surname_set.get(primary=True)
            else: # create new item
                person = Person(handle=create_id())
                name = Name(person=person, preferred=True)
                surname = Surname(name=name, primary=True, order=1)
                surname = Surname(name=name, 
                                  primary=True, 
                                  order=1,
                                  name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0]))
            # combine with user data:
            pf = PersonForm(request.POST, instance=person)
            pf.model = person
            nf = NameFormFromPerson(request.POST, instance=name)
            nf.model = name
            sf = SurnameForm(request.POST, instance=surname)
            sf.model = surname
            # check if valid:
            if nf.is_valid() and pf.is_valid() and sf.is_valid() and logform.is_valid():
                # name.preferred and surname.primary get set False in the above is_valid()
                update_last_changed(person, request.user.username)
                person = pf.save(save_cache=False)
                # Process data:
                name.person = person
                name = nf.save(commit=False)
                # Manually set any data:
                name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else ""
                name.preferred = True # FIXME: why is this False? Remove from form?
                check_preferred(request, name, person)
                update_last_changed(name, request.user.username)
                name.save()
                # Process data:
                surname.name = name
                surname = sf.save(commit=False)
                # Manually set any data:
                surname.prefix = sf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else ""
                surname.primary = True # FIXME: why is this False? Remove from form?
                surname.save()
                # FIXME: put this in correct place to get correct cache, before changes:
                make_log(person, act, request.user.username, logform.cleaned_data["reason"], person.cache)
                if add_to: # Adding a child to the family
                    item, handle = add_to # ("Family", handle)
                    model = dji.get_model(item) # what model?
                    obj = model.objects.get(handle=handle) # get family
                    dji.add_child_ref_default(obj, person) # add person to family
                    #person.parent_families.add(obj) # add family to child
                    pfo = MyParentFamilies(person=person, family=obj,
                                           order=len(person.parent_families.all())+1)
                    pfo.save()
                    person.save_cache() # rebuild child
                    obj.save_cache() # rebuild family
                    return redirect("/%s/%s%s" % (item, handle, build_search(request)))
                person.save_cache()
                return redirect("/person/%s%s" % (person.handle, build_search(request)))
            else: 
                # need to edit again
                if handle:
                    act = "edit"
                else:
                    act = "add"
        else: # error?
            raise Http404(_("Requested %s does not exist.") % "person")
    else: # not authenticated
        # BEGIN NON-AUTHENTICATED ACCESS
        try:
            person = Person.objects.get(handle=handle)
        except:
            raise Http404(_("Requested %s does not exist.") % "person")
        if person.private:
            raise Http404(_("Requested %s does not exist.") % "person")
        pf, nf, sf, person = get_person_forms(handle, protect=True)
        # END NON-AUTHENTICATED ACCESS
    context["action"] = act
    context["view"] = "person"
    context["tview"] = _("Person")
    context["tviews"] = _("People")
    context["personform"] = pf
    context["nameform"] = nf
    context["surnameform"] = sf
    context["logform"] = logform
    context["person"] = person
    context["object"] = person
    context["next"] = "/person/%s" % person.handle
예제 #2
0
파일: person.py 프로젝트: vperic/gramps
def process_name(request, handle, order, act="view"):
    if order == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")
    ### Process act:
    if act in "view":
        pf, nf, sf, person = get_person_forms(handle, order=order)
        name = nf.model
    elif act == "edit":
        pf, nf, sf, person = get_person_forms(handle, order=order)
        name = nf.model
    elif act == "delete":
        person = Person.objects.get(handle=handle)
        name = person.name_set.filter(order=order)
        names = person.name_set.all()
        if len(names) > 1:
            name.delete()
            check_order(request, person)
        else:
            request.user.message_set.create(message = "Can't delete only name.")
        return redirect("/person/%s%s#tab-names" % (person.handle,
                                                    build_search(request)))
    elif act == "add": # add name
        person = Person.objects.get(handle=handle)
        name = Name(person=person, 
                    preferred=False,
                    display_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]), 
                    sort_as=NameFormatType.objects.get(val=NameFormatType._DEFAULT[0]), 
                    name_type=NameType.objects.get(val=NameType._DEFAULT[0]))
        nf = NameForm(instance=name)
        nf.model = name
        surname = Surname(name=name, 
                          primary=True, 
                          order=1,
                          name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0]))
        sf = SurnameForm(request.POST, instance=surname)
        sf.model = surname
    elif act == "create":
        # make new data
        person = Person.objects.get(handle=handle)
        name = Name(preferred=False)
        next_order = max([name.order for name in person.name_set.all()]) + 1
        surname = Surname(name=name, 
                          primary=True, 
                          order=next_order, 
                          name_origin_type=NameOriginType.objects.get(val=NameOriginType._DEFAULT[0]))
        # combine with user data:
        nf = NameForm(request.POST, instance=name)
        name.id = None # FIXME: why did this get set to an existing name? Should be new. Remove from form?
        name.preferred = False
        nf.model = name
        sf = SurnameForm(request.POST, instance=surname)
        sf.model = surname
        if nf.is_valid() and sf.is_valid():
            # name.preferred and surname.primary get set False in the above is_valid()
            # person = pf.save()
            # Process data:
            name = nf.save(commit=False)
            name.person = person
            update_last_changed(name, request.user.username)
            # Manually set any data:
            name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else ""
            name.preferred = False # FIXME: why is this False? Remove from form?
            name.order = next_order
            name.save()
            # Process data:
            surname = sf.save(commit=False)
            surname.name = name
            # Manually set any data:
            surname.prefix = sf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else ""
            surname.primary = True # FIXME: why is this False? Remove from form?
            surname.save()
            person.save_cache()
            return redirect("/person/%s/name/%s%s#tab-surnames" % (person.handle, name.order,             
                                                                   build_search(request)))
        else:
            act = "add"
    elif act == "save":
        # look up old data:
        person = Person.objects.get(handle=handle)
        oldname = person.name_set.get(order=order)
        oldsurname = oldname.surname_set.get(primary=True)
        # combine with user data:
        pf = PersonForm(request.POST, instance=person)
        pf.model = person
        nf = NameForm(request.POST, instance=oldname)
        nf.model = oldname
        sf = SurnameForm(request.POST, instance=oldsurname)
        sf.model = oldsurname
        if nf.is_valid() and sf.is_valid():
            # name.preferred and surname.primary get set False in the above is_valid()
            # person = pf.save()
            # Process data:
            oldname.person = person
            name = nf.save()
            # Manually set any data:
            name.suffix = nf.cleaned_data["suffix"] if nf.cleaned_data["suffix"] != " suffix " else ""
            name.preferred = True # FIXME: why is this False? Remove from form?
            update_last_changed(name, request.user.username)
            check_preferred(request, name, person)
            name.save()
            # Process data:
            oldsurname.name = name
            surname = sf.save(commit=False)
            # Manually set any data:
            surname.prefix = sf.cleaned_data["prefix"] if sf.cleaned_data["prefix"] != " prefix " else ""
            surname.primary = True # FIXME: why is this False? Remove from form?
            surname.save()
            person.save_cache()
            return redirect("/person/%s/name/%s%s#tab-surnames" % (person.handle, name.order,
                                                                   build_search(request)))
        else:
            act = "edit"
    context = RequestContext(request)
    context["action"] = act
    context["tview"] = _('Name')
    context["tviews"] = _('Names')
    context["view"] = 'name'
    context["handle"] = handle
    context["id"] = id
    context["person"] = person
    context["object"] = person
    context["nameform"] = nf
    context["surnameform"] = sf
    context["order"] = order
    context["next"] = "/person/%s/name/%d" % (person.handle, name.order)
    view_template = "view_name_detail.html"
    return render_to_response(view_template, context)