Пример #1
0
def check_preferred(request, name, person):
    """
    Check for a proper preferred name.
    """
    names = []
    if person:
        names = person.name_set.all()
    if name.preferred:
        # then all reast should not be:
        for s in names:
            if s.preferred and s.id != name.id:
                s.preferred = False
                update_last_changed(s, request.user.username)
                s.save()
    else:
        # then one of them should be
        ok = False
        for s in names:
            if s.id != name.id:
                if s.preferred:
                    ok = True
                    break
                else:
                    s.preferred = False
                    update_last_changed(s, request.user.username)
                    s.save()
                    ok = True
                    break
        if not ok:
            name.preferred = True
Пример #2
0
def check_order(request, person):
    """
    Check for proper ordering 1..., and for a preferred name.
    """
    order = 1
    preferred = False
    for name in person.name_set.all().order_by("order"):
        if name.preferred:
            preferred = True
        if name.order != order:
            name.order = order
            update_last_changed(name, request.user.username)
            name.save()
        order += 1
    if not preferred:
        name = person.name_set.get(order=1)
        name.preferred = True
        update_last_changed(name, request.user.username)
        name.save()
Пример #3
0
def process_repository(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Repository")
    context["tviews"] = _("Repositories")
    context["action"] = "view"
    view_template = "view_repository_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick repository",
                                       Repository,
                                       (),
                                       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
        pickform = PickForm("Pick repository",
                            Repository,
                            (),
                            request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item) # what model?
            parent_obj = parent_model.objects.get(handle=handle) # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Repository.objects.get(handle=ref_handle)
            dji.add_repository_ref_default(parent_obj, ref_obj)
            parent_obj.save_cache() # rebuild cache
            return redirect("/%s/%s%s#tab-repositories" % (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "add":
        repository = Repository(gramps_id=dji.get_next_id(Repository, "R"))
        repositoryform = RepositoryForm(instance=repository)
        repositoryform.model = repository
    elif act in ["view", "edit"]:
        repository = Repository.objects.get(handle=handle)
        repositoryform = RepositoryForm(instance=repository)
        repositoryform.model = repository
    elif act == "save":
        repository = Repository.objects.get(handle=handle)
        repositoryform = RepositoryForm(request.POST, instance=repository)
        repositoryform.model = repository
        if repositoryform.is_valid():
            update_last_changed(repository, request.user.username)
            repository = repositoryform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        repository = Repository(handle=create_id())
        repositoryform = RepositoryForm(request.POST, instance=repository)
        repositoryform.model = repository
        if repositoryform.is_valid():
            update_last_changed(repository, request.user.username)
            repository = repositoryform.save()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_repository_ref_default(obj, repository)
                obj.save_cache()
                return redirect("/%s/%s#tab-repositories" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        repository = Repository.objects.get(handle=handle)
        repository.delete()
        return redirect("/repository/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["repositoryform"] = repositoryform
    context["object"] = repository
    context["repository"] = repository
    context["action"] = act

    return render_to_response(view_template, context)
Пример #4
0
def process_event(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Event")
    context["tviews"] = _("Events")
    context["action"] = "view"
    view_template = "view_event_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick event", 
                                       Event, 
                                       (),
                                       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 
        pickform = PickForm("Pick event", 
                            Event, 
                            (),
                            request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item) # what model?
            parent_obj = parent_model.objects.get(handle=handle) # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Event.objects.get(handle=ref_handle) 
            dji.add_event_ref_default(parent_obj, ref_obj)
            if item == "person": # then need to recheck birth/death indexes:
                recheck_birth_death_refs(parent_obj)
                parent_obj.save(save_cache=False)
            parent_obj.save_cache()
            return redirect("/%s/%s%s#tab-events" % (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "add":
        event = Event(gramps_id=dji.get_next_id(Event, "E"))
        eventform = EventForm(instance=event)
        eventform.model = event
    elif act in ["view", "edit"]: 
        event = Event.objects.get(handle=handle)
        genlibevent = db.get_event_from_handle(handle)
        if genlibevent:
            date = genlibevent.get_date_object()
            event.text = dd(date)
        eventform = EventForm(instance=event)
        eventform.model = event
    elif act == "save": 
        event = Event.objects.get(handle=handle)
        eventform = EventForm(request.POST, instance=event)
        eventform.model = event
        if eventform.is_valid():
            update_last_changed(event, request.user.username)
            event = eventform.save()
            # Check any person that might be referenced to see if
            # birth/death issues changed:
            check_event(event)
            event.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create": 
        event = Event(handle=create_id())
        eventform = EventForm(request.POST, instance=event)
        eventform.model = event
        if eventform.is_valid():
            update_last_changed(event, request.user.username)
            event = eventform.save()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_event_ref_default(obj, event)
                if item == "person": # then need to recheck birth/death indexes:
                    recheck_birth_death_refs(obj)
                    obj.save(save_cache=False)
                obj.save_cache()
                return redirect("/%s/%s#tab-events" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete": 
        event = Event.objects.get(handle=handle)
        delete_event(event)
        return redirect("/event/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["eventform"] = eventform
    context["object"] = event
    context["event"] = event
    context["action"] = act
    
    return render_to_response(view_template, context)
Пример #5
0
def process_family(request,
                   context,
                   handle,
                   act,
                   add_to=None):  # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Family")
    context["tviews"] = _("Familes")

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        # Adds a person to an existing family
        item, handle = add_to
        context["pickform"] = PickForm("Pick family", Family, (), request.POST)
        context["object_handle"] = handle
        context["object_type"] = "person"
        return render_to_response("pick.html", context)
    elif act == "save-share":
        item, handle = add_to
        pickform = PickForm("Pick family", Family, (), request.POST)
        if pickform.data["picklist"]:
            person = Person.objects.get(handle=handle)  # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Family.objects.get(handle=ref_handle)
            if item == "child":
                dji.add_child_ref_default(ref_obj,
                                          person)  # add person to family
                #person.parent_families.add(ref_obj) # add family to child
                pfo = MyParentFamilies(
                    person=person,
                    family=ref_obj,
                    order=len(person.parent_families.all()) + 1)
                pfo.save()
            elif item == "spouse":
                if person.gender_type.name == "Female":
                    ref_obj.mother = person
                elif person.gender_type.name == "Male":
                    ref_obj.father = person
                else:
                    ref_obj.father = person  # FIXME: Unknown gender, add to open
                #person.families.add(ref_obj) # add family to person
                pfo = MyFamilies(person=person,
                                 family=ref_obj,
                                 order=len(person.families.all()) + 1)
                pfo.save()
            ref_obj.save()
            person.save()
            return redirect("/%s/%s%s#tab-references" %
                            ("person", handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = "person"
            return render_to_response("pick.html", context)
    elif act == "add":
        family = Family(gramps_id=dji.get_next_id(Family, "F"),
                        family_rel_type=FamilyRelType.objects.get(
                            val=FamilyRelType._DEFAULT[0]))
        if add_to:
            what, phandle = add_to
            person = Person.objects.get(handle=phandle)
            gender = person.gender_type.name  # Male, Female, Unknown
            if what == "spouse":
                if gender == "Male":
                    family.father = person
                elif gender == "Female":
                    family.mother = person
                else:  # You have to pick one!
                    family.father = person
            elif what == "child":
                pass  # FIXME: can't show child in table?
                # Table from children_table
            else:  # unknown what!
                raise Exception("can't add_to: '%s'" % what)
        familyform = FamilyForm(instance=family)
        familyform.model = family
    elif act in ["view", "edit"]:
        family = Family.objects.get(handle=handle)
        familyform = FamilyForm(instance=family)
        familyform.model = family
    elif act == "save":  # editing an existing family
        family = Family.objects.get(handle=handle)
        old_family_mother = family.mother
        old_family_father = family.father
        familyform = FamilyForm(request.POST, instance=family)
        familyform.model = family
        if familyform.is_valid():
            update_last_changed(family, request.user.username)
            family = familyform.save()
            # Remove family from previous mother/father if changed
            if familyform.cleaned_data[
                    "mother"] != old_family_mother and old_family_mother:
                MyFamilies.objects.get(person=old_family_mother,
                                       family=family).delete()
            if familyform.cleaned_data[
                    "father"] != old_family_father and old_family_father:
                MyFamilies.objects.get(person=old_family_father,
                                       family=family).delete()
            # Add family to newly selected mother/father if needed:
            if familyform.cleaned_data["mother"]:
                if family not in familyform.cleaned_data[
                        "mother"].families.all():
                    #family.mother.families.add(family)
                    pfo = MyFamilies(
                        person=familyform.cleaned_data["mother"],
                        family=family,
                        order=len(
                            familyform.cleaned_data["mother"].families.all()) +
                        1)
                    pfo.save()
            if familyform.cleaned_data["father"]:
                if family not in familyform.cleaned_data[
                        "father"].families.all():
                    #family.father.families.add(family)
                    pfo = MyFamilies(
                        person=family.father,
                        family=family,
                        order=len(
                            familyform.cleaned_data["father"].families.all()) +
                        1)
                    pfo.save()
            familyform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        family = Family(family_rel_type=FamilyRelType.objects.get(
            val=FamilyRelType._DEFAULT[0]),
                        handle=create_id())
        familyform = FamilyForm(request.POST, instance=family)
        familyform.model = family
        if familyform.is_valid():
            update_last_changed(family, request.user.username)
            family = familyform.save()
            if family.mother:
                #family.mother.families.add(family)
                pfo = MyFamilies(person=family.mother,
                                 family=family,
                                 order=len(family.mother.families.all()) + 1)
                pfo.save()
            if family.father:
                #family.father.families.add(family)
                pfo = MyFamilies(person=family.father,
                                 family=family,
                                 order=len(family.father.families.all()) + 1)
                pfo.save()
            family.save_cache()
            if add_to:  # add child or spouse to family
                item, handle = add_to
                person = Person.objects.get(handle=handle)
                if item == "child":
                    dji.add_child_ref_default(family,
                                              person)  # add person to family
                    ##person.parent_families.add(family) # add family to child
                    pfo = MyParentFamilies(
                        person=person,
                        family=family,
                        order=len(person.parent_families.all()) + 1)
                    pfo.save()
                    #elif item == "spouse":
                    # already added by selecting
                    person.save()
                return redirect("/%s/%s%s#tab-references" %
                                ("person", handle, build_search(request)))
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        family = Family.objects.get(handle=handle)
        family.delete()
        return redirect("/family/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["familyform"] = familyform
    context["object"] = family
    context["family"] = family
    context["action"] = act
    view_template = "view_family_detail.html"

    return render_to_response(view_template, context)
Пример #6
0
def process_place(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Place")
    context["tviews"] = _("Places")
    context["action"] = "view"
    view_template = "view_place_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete
    if act == "add":
        place = Place(gramps_id=dji.get_next_id(Place, "P"))
        placeform = PlaceForm(instance=place)
        placeform.model = place
    elif act in ["view", "edit"]: 
        place = Place.objects.get(handle=handle)
        placeform = PlaceForm(instance=place)
        placeform.model = place
    elif act == "save": 
        place = Place.objects.get(handle=handle)
        placeform = PlaceForm(request.POST, instance=place)
        placeform.model = place
        if placeform.is_valid():
            update_last_changed(place, request.user.username)
            place = placeform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create": 
        place = Place(handle=create_id())
        placeform = PlaceForm(request.POST, instance=place)
        placeform.model = place
        if placeform.is_valid():
            update_last_changed(place, request.user.username)
            place = placeform.save()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_place_ref(obj, place.handle)
                obj.save_cache()
                return redirect("/%s/%s#tab-places" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete": 
        place = Place.objects.get(handle=handle)
        place.delete()
        return redirect("/place/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["placeform"] = placeform
    context["object"] = place
    context["place"] = place
    context["action"] = act
    
    return render_to_response(view_template, context)
Пример #7
0
def process_event(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Event")
    context["tviews"] = _("Events")
    context["action"] = "view"
    view_template = "view_event_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick event",
                                       Event,
                                       (),
                                       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
        pickform = PickForm("Pick event",
                            Event,
                            (),
                            request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item) # what model?
            parent_obj = parent_model.objects.get(handle=handle) # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Event.objects.get(handle=ref_handle)
            dji.add_event_ref_default(parent_obj, ref_obj)
            if item == "person": # then need to recheck birth/death indexes:
                recheck_birth_death_refs(parent_obj)
                parent_obj.save(save_cache=False)
            parent_obj.save_cache()
            return redirect("/%s/%s%s#tab-events" % (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "add":
        event = Event(gramps_id=dji.get_next_id(Event, "E"))
        eventform = EventForm(instance=event)
        eventform.model = event
    elif act in ["view", "edit"]:
        event = Event.objects.get(handle=handle)
        genlibevent = db.get_event_from_handle(handle)
        if genlibevent:
            date = genlibevent.get_date_object()
            event.text = dd(date)
        eventform = EventForm(instance=event)
        eventform.model = event
    elif act == "save":
        event = Event.objects.get(handle=handle)
        eventform = EventForm(request.POST, instance=event)
        eventform.model = event
        if eventform.is_valid():
            update_last_changed(event, request.user.username)
            event = eventform.save()
            # Check any person that might be referenced to see if
            # birth/death issues changed:
            check_event(event)
            event.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        event = Event(handle=create_id())
        eventform = EventForm(request.POST, instance=event)
        eventform.model = event
        if eventform.is_valid():
            update_last_changed(event, request.user.username)
            event = eventform.save()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_event_ref_default(obj, event)
                if item == "person": # then need to recheck birth/death indexes:
                    recheck_birth_death_refs(obj)
                    obj.save(save_cache=False)
                obj.save_cache()
                return redirect("/%s/%s#tab-events" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        event = Event.objects.get(handle=handle)
        delete_event(event)
        return redirect("/event/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["eventform"] = eventform
    context["object"] = event
    context["event"] = event
    context["action"] = act

    return render_to_response(view_template, context)
Пример #8
0
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
Пример #9
0
def process_source(request,
                   context,
                   handle,
                   act,
                   add_to=None):  # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Source")
    context["tviews"] = _("Sources")
    context["action"] = "view"
    view_template = "view_source_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick source", Source, (), 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
        pickform = PickForm("Pick source", Source, (), request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item)  # what model?
            parent_obj = parent_model.objects.get(handle=handle)  # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Source.objects.get(handle=ref_handle)
            dji.add_source_ref_default(parent_obj, ref_obj)
            parent_obj.save_cache()  # rebuild cache
            return redirect("/%s/%s%s#tab-sources" %
                            (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "add":
        source = Source(gramps_id=dji.get_next_id(Source, "S"))
        sourceform = SourceForm(instance=source)
        sourceform.model = source
    elif act in ["view", "edit"]:
        source = Source.objects.get(handle=handle)
        sourceform = SourceForm(instance=source)
        sourceform.model = source
    elif act == "save":
        source = Source.objects.get(handle=handle)
        sourceform = SourceForm(request.POST, instance=source)
        sourceform.model = source
        if sourceform.is_valid():
            update_last_changed(source, request.user.username)
            source = sourceform.save()
            dji.rebuild_cache(source)
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        source = Source(handle=create_id())
        sourceform = SourceForm(request.POST, instance=source)
        sourceform.model = source
        if sourceform.is_valid():
            update_last_changed(source, request.user.username)
            source = sourceform.save()
            dji.rebuild_cache(source)
            if add_to:
                raise Exception("Cannot add reference")
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        source = Source.objects.get(handle=handle)
        source.delete()
        return redirect("/source/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["sourceform"] = sourceform
    context["object"] = source
    context["source"] = source
    context["action"] = act

    return render_to_response(view_template, context)
Пример #10
0
def process_note(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Note")
    context["tviews"] = _("Notes")
    context["action"] = "view"
    view_template = "view_note_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick note", 
                                       Note, 
                                       (),
                                       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 
        pickform = PickForm("Pick note", 
                            Note, 
                            (),
                            request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item) # what model?
            parent_obj = parent_model.objects.get(handle=handle) # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Note.objects.get(handle=ref_handle) 
            dji.add_note_ref(parent_obj, ref_obj)
            parent_obj.save_cache() # rebuild cache
            return redirect("/%s/%s%s#tab-notes" % (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "add":
        note = Note(gramps_id=dji.get_next_id(Note, "N"))
        notetext = ""
        noteform = NoteForm(instance=note, initial={"notetext": notetext})
        noteform.model = note
    elif act in ["view", "edit"]: 
        note = Note.objects.get(handle=handle)
        genlibnote = db.get_note_from_handle(note.handle)
        notetext = snf.format(genlibnote)
        noteform = NoteForm(instance=note, initial={"notetext": notetext})
        noteform.model = note
    elif act == "save": 
        note = Note.objects.get(handle=handle)
        notetext = ""
        noteform = NoteForm(request.POST, instance=note, initial={"notetext": notetext})
        noteform.model = note
        if noteform.is_valid():
            update_last_changed(note, request.user.username)
            notedata = parse_styled_text(noteform.data["notetext"])
            note.text = notedata[0]
            note = noteform.save()
            dji.save_note_markup(note, notedata[1])
            note.save_cache()
            notetext = noteform.data["notetext"] 
            act = "view"
        else:
            notetext = noteform.data["notetext"] 
            act = "edit"
    elif act == "create": 
        note = Note(handle=create_id())
        notetext = ""
        noteform = NoteForm(request.POST, instance=note, initial={"notetext": notetext})
        noteform.model = note
        if noteform.is_valid():
            update_last_changed(note, request.user.username)
            notedata = parse_styled_text(noteform.data["notetext"])
            note.text = notedata[0]
            note = noteform.save()
            dji.save_note_markup(note, notedata[1])
            note.save_cache()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_note_ref(obj, note)
                obj.save_cache()
                return redirect("/%s/%s#tab-notes" % (item, handle))
            notetext = noteform.data["notetext"] 
            act = "view"
        else:
            notetext = noteform.data["notetext"] 
            act = "add"
    elif act == "delete": 
        # FIXME: delete markup too for this note
        note = Note.objects.get(handle=handle)
        note.delete()
        return redirect("/note/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["noteform"] = noteform
    context["object"] = note
    context["notetext"] = notetext
    context["note"] = note
    context["action"] = act
    
    return render_to_response(view_template, context)
Пример #11
0
def process_place(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Place")
    context["tviews"] = _("Places")
    context["action"] = "view"
    view_template = "view_place_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete
    if act == "add":
        place = Place(gramps_id=dji.get_next_id(Place, "P"))
        placeform = PlaceForm(instance=place)
        placeform.model = place
    elif act in ["view", "edit"]:
        place = Place.objects.get(handle=handle)
        placeform = PlaceForm(instance=place)
        placeform.model = place
    elif act == "save":
        place = Place.objects.get(handle=handle)
        placeform = PlaceForm(request.POST, instance=place)
        placeform.model = place
        if placeform.is_valid():
            update_last_changed(place, request.user.username)
            place = placeform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        place = Place(handle=create_id())
        placeform = PlaceForm(request.POST, instance=place)
        placeform.model = place
        if placeform.is_valid():
            update_last_changed(place, request.user.username)
            place = placeform.save()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_place_ref(obj, place.handle)
                obj.save_cache()
                return redirect("/%s/%s#tab-places" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        place = Place.objects.get(handle=handle)
        place.delete()
        return redirect("/place/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["placeform"] = placeform
    context["object"] = place
    context["place"] = place
    context["action"] = act

    return render_to_response(view_template, context)
Пример #12
0
def process_tag(request,
                context,
                handle,
                act,
                add_to=None):  # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Tag")
    context["tviews"] = _("Tags")
    context["action"] = "view"
    view_template = "view_tag_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete
    if act == "add":
        tag = Tag()
        tagform = TagForm(instance=tag)
        tagform.model = tag
    elif act in ["view", "edit"]:
        tag = Tag.objects.get(handle=handle)
        tagform = TagForm(instance=tag)
        tagform.model = tag
    elif act == "save":
        tag = Tag.objects.get(handle=handle)
        tagform = TagForm(request.POST, instance=tag)
        tagform.model = tag
        if tagform.is_valid():
            update_last_changed(tag, request.user.username)
            tag = tagform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        tag = Tag(handle=create_id())
        tagform = TagForm(request.POST, instance=tag)
        tagform.model = tag
        if tagform.is_valid():
            update_last_changed(tag, request.user.username)
            tag = tagform.save()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_tag_ref_default(obj, tag)
                obj.save_cache()
                return redirect("/%s/%s#tab-tags" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        tag = Tag.objects.get(handle=handle)
        tag.delete()
        return redirect("/tag/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["tagform"] = tagform
    context["object"] = tag
    context["tag"] = tag
    context["action"] = act

    return render_to_response(view_template, context)
Пример #13
0
def process_media(request,
                  context,
                  handle,
                  act,
                  add_to=None):  # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Media")
    context["tviews"] = _("Media")
    context["action"] = "view"
    view_template = "view_media_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick media", Media, (), 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
        pickform = PickForm("Pick media", Media, (), request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item)  # what model?
            parent_obj = parent_model.objects.get(handle=handle)  # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Media.objects.get(handle=ref_handle)
            dji.add_media_ref_default(parent_obj, ref_obj)
            parent_obj.save_cache()  # rebuild cache
            return redirect("/%s/%s%s#tab-media" %
                            (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "full":
        media = Media.objects.get(handle=handle)
        media_type, media_ext = media.mime.split("/", 1)
        # FIXME: This should be absolute:
        folder = Config.objects.get(
            setting="behavior.addmedia-image-dir").value
        # FIXME: media.path should not have any .. for security
        response = HttpResponse(content_type=media.mime)
        if NEW_PIL or media_ext != "png":
            image = Image.open("%s/%s" % (folder, media.path))
            image.save(response, media_ext)
        else:
            # FIXME: older PIL 1.1.6 cannot read interlaced PNG files
            reader = png.Reader(filename="%s/%s" % (folder, media.path))
            x, y, pixels, meta = reader.asDirect()
            image = png.Image(pixels, meta)
            image.save(response)
        return response
    elif act == "thumbnail":
        media = Media.objects.get(handle=handle)
        media_type, media_ext = media.mime.split("/", 1)
        # FIXME: This should be absolute:
        folder = Config.objects.get(
            setting="behavior.addmedia-image-dir").value
        # FIXME: media.path should not have any .. for security
        response = HttpResponse(content_type=media.mime)
        if os.path.exists("%s/thumbnail/%s" % (folder, media.path)):
            if NEW_PIL or media_ext != "png":
                image = Image.open("%s/thumbnail/%s" % (folder, media.path))
                image.save(response, media_ext)
            else:
                # FIXME: older PIL 1.1.6 cannot read interlaced PNG files
                reader = png.Reader(filename="%s/thumbnail/%s" %
                                    (folder, media.path))
                x, y, pixels, meta = reader.asDirect()
                image = png.Image(pixels, meta)
                image.save(response)
        else:
            try:
                os.makedirs("%s/thumbnail" % folder)
            except:
                pass
            if NEW_PIL or media_ext != "png":
                image = Image.open("%s/%s" % (folder, media.path))
                image.thumbnail((300, 300), Image.ANTIALIAS)
                image.save("%s/thumbnail/%s" % (folder, media.path), media_ext)
                image.save(response, media_ext)
            else:
                # FIXME: older PIL 1.1.6 cannot read interlaced PNG files
                reader = png.Reader(filename="%s/%s" % (folder, media.path))
                x, y, pixels, meta = reader.asDirect()
                meta["interlace"] = False
                image = png.Image(pixels, meta)
                image.save("/tmp/%s" % media.path)
                # Now open in PIL to rescale
                image = Image.open("/tmp/%s" % media.path)
                image.thumbnail((300, 300), Image.ANTIALIAS)
                image.save("%s/thumbnail/%s" % (folder, media.path), media_ext)
                image.save(response, media_ext.upper())
        return response
    elif act == "add":
        media = Media(gramps_id=dji.get_next_id(Media, "M"))
        mediaform = MediaForm(instance=media)
        mediaform.model = media
    elif act in ["view", "edit"]:
        media = Media.objects.get(handle=handle)
        mediaform = MediaForm(instance=media)
        mediaform.model = media
    elif act == "save":
        media = Media.objects.get(handle=handle)
        mediaform = MediaForm(request.POST, instance=media)
        mediaform.model = media
        if mediaform.is_valid():
            update_last_changed(media, request.user.username)
            media = mediaform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        media = Media(handle=create_id())
        mediaform = MediaForm(request.POST, instance=media)
        mediaform.model = media
        if mediaform.is_valid():
            update_last_changed(media, request.user.username)
            media = mediaform.save(save_cache=False)
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_media_ref_default(obj, media)
                obj.save_cache()
                media.save_cache()
                return redirect("/%s/%s#tab-gallery" % (item, handle))
            else:
                media.save_cache()
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        media = Media.objects.get(handle=handle)
        media.delete()
        return redirect("/media/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["mediaform"] = mediaform
    context["object"] = media
    context["media"] = media
    context["action"] = act

    return render_to_response(view_template, context)
Пример #14
0
def process_media(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Media")
    context["tviews"] = _("Media")
    context["action"] = "view"
    view_template = "view_media_detail.html"
    
    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick media", 
                                       Media, 
                                       (),
                                       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 
        pickform = PickForm("Pick media", 
                            Media, 
                            (),
                            request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item) # what model?
            parent_obj = parent_model.objects.get(handle=handle) # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Media.objects.get(handle=ref_handle) 
            dji.add_media_ref_default(parent_obj, ref_obj)
            parent_obj.save_cache() # rebuild cache
            return redirect("/%s/%s%s#tab-media" % (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "full":
        media = Media.objects.get(handle=handle)
        media_type, media_ext = media.mime.split("/", 1)
        # FIXME: This should be absolute:
        folder = Config.objects.get(setting="behavior.addmedia-image-dir").value
        # FIXME: media.path should not have any .. for security
        response = HttpResponse(content_type=media.mime)
        if NEW_PIL or media_ext != "png":
            image = Image.open("%s/%s" % (folder, media.path))
            image.save(response, media_ext)
        else:
            # FIXME: older PIL 1.1.6 cannot read interlaced PNG files
            reader = png.Reader(filename="%s/%s" % (folder, media.path))
            x, y, pixels, meta = reader.asDirect()
            image = png.Image(pixels, meta)
            image.save(response)
        return response
    elif act == "thumbnail":
        media = Media.objects.get(handle=handle)
        media_type, media_ext = media.mime.split("/", 1)
        # FIXME: This should be absolute:
        folder = Config.objects.get(setting="behavior.addmedia-image-dir").value
        # FIXME: media.path should not have any .. for security
        response = HttpResponse(content_type=media.mime)
        if os.path.exists("%s/thumbnail/%s" % (folder, media.path)):
            if NEW_PIL or media_ext != "png":
                image = Image.open("%s/thumbnail/%s" % (folder, media.path))
                image.save(response, media_ext)
            else:
                # FIXME: older PIL 1.1.6 cannot read interlaced PNG files
                reader = png.Reader(filename="%s/thumbnail/%s" % (folder, media.path))
                x, y, pixels, meta = reader.asDirect()
                image = png.Image(pixels, meta)
                image.save(response)
        else:
            try:
                os.makedirs("%s/thumbnail" % folder)
            except:
                pass
            if NEW_PIL or media_ext != "png":
                image = Image.open("%s/%s" % (folder, media.path))
                image.thumbnail((300,300), Image.ANTIALIAS)
                image.save("%s/thumbnail/%s" % (folder, media.path), media_ext)
                image.save(response, media_ext)
            else:
                # FIXME: older PIL 1.1.6 cannot read interlaced PNG files
                reader = png.Reader(filename="%s/%s" % (folder, media.path))
                x, y, pixels, meta = reader.asDirect()
                meta["interlace"] = False
                image = png.Image(pixels, meta)
                image.save("/tmp/%s" % media.path)
                # Now open in PIL to rescale
                image = Image.open("/tmp/%s" % media.path)
                image.thumbnail((300,300), Image.ANTIALIAS)
                image.save("%s/thumbnail/%s" % (folder, media.path), media_ext)
                image.save(response, media_ext.upper())
        return response
    elif act == "add":
        media = Media(gramps_id=dji.get_next_id(Media, "M"))
        mediaform = MediaForm(instance=media)
        mediaform.model = media
    elif act in ["view", "edit"]: 
        media = Media.objects.get(handle=handle)
        mediaform = MediaForm(instance=media)
        mediaform.model = media
    elif act == "save": 
        media = Media.objects.get(handle=handle)
        mediaform = MediaForm(request.POST, instance=media)
        mediaform.model = media
        if mediaform.is_valid():
            update_last_changed(media, request.user.username)
            media = mediaform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create": 
        media = Media(handle=create_id())
        mediaform = MediaForm(request.POST, instance=media)
        mediaform.model = media
        if mediaform.is_valid():
            update_last_changed(media, request.user.username)
            media = mediaform.save(save_cache=False)
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_media_ref_default(obj, media)
                obj.save_cache()
                media.save_cache()
                return redirect("/%s/%s#tab-gallery" % (item, handle))
            else:
                media.save_cache()
            act = "view"
        else:
            act = "add"
    elif act == "delete": 
        media = Media.objects.get(handle=handle)
        media.delete()
        return redirect("/media/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["mediaform"] = mediaform
    context["object"] = media
    context["media"] = media
    context["action"] = act
    
    return render_to_response(view_template, context)
Пример #15
0
def process_tag(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Tag")
    context["tviews"] = _("Tags")
    context["action"] = "view"
    view_template = "view_tag_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete
    if act == "add":
        tag = Tag()
        tagform = TagForm(instance=tag)
        tagform.model = tag
    elif act in ["view", "edit"]:
        tag = Tag.objects.get(handle=handle)
        tagform = TagForm(instance=tag)
        tagform.model = tag
    elif act == "save":
        tag = Tag.objects.get(handle=handle)
        tagform = TagForm(request.POST, instance=tag)
        tagform.model = tag
        if tagform.is_valid():
            update_last_changed(tag, request.user.username)
            tag = tagform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        tag = Tag(handle=create_id())
        tagform = TagForm(request.POST, instance=tag)
        tagform.model = tag
        if tagform.is_valid():
            update_last_changed(tag, request.user.username)
            tag = tagform.save()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_tag_ref_default(obj, tag)
                obj.save_cache()
                return redirect("/%s/%s#tab-tags" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        tag = Tag.objects.get(handle=handle)
        tag.delete()
        return redirect("/tag/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["tagform"] = tagform
    context["object"] = tag
    context["tag"] = tag
    context["action"] = act

    return render_to_response(view_template, context)
Пример #16
0
def process_citation(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Citation")
    context["tviews"] = _("Citations")
    context["action"] = "view"
    view_template = "view_citation_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick citation",
                                       Citation,
                                       (),
                                       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
        pickform = PickForm("Pick citation",
                            Citation,
                            (),
                            request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item) # what model?
            parent_obj = parent_model.objects.get(handle=handle) # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Citation.objects.get(handle=ref_handle)
            dji.add_citation_ref_default(parent_obj, ref_obj)
            parent_obj.save_cache() # rebuild cache
            return redirect("/%s/%s%s#tab-citations" % (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "add":
        source = Source(gramps_id=dji.get_next_id(Source, "S"))
        sourceform = SourceForm(instance=source)
        sourceform.model = source
        citation = Citation(source=source, gramps_id=dji.get_next_id(Citation, "C"))
        citationform = CitationForm(instance=citation)
        citationform.model = citation
    elif act in ["view", "edit"]:
        citation = Citation.objects.get(handle=handle)
        citationform = CitationForm(instance=citation)
        citationform.model = citation
        source = citation.source
        sourceform = SourceForm(instance=source)
        sourceform.model = source
    elif act == "save":
        citation = Citation.objects.get(handle=handle)
        citationform = CitationForm(request.POST, instance=citation)
        citationform.model = citation
        if citationform.is_valid():
            update_last_changed(citation, request.user.username)
            citation = citationform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        source = Source(handle=create_id())
        sourceform = SourceForm(request.POST, instance=source)
        sourceform.model = source
        citation = Citation(handle=create_id(), source=source)
        citationform = CitationForm(request.POST, instance=citation)
        citationform.model = citation
        if citationform.is_valid() and sourceform.is_valid():
            update_last_changed(source, request.user.username)
            source = sourceform.save()
            citation.source = source
            update_last_changed(citation, request.user.username)
            citation = citationform.save(save_cache=False)
            source.save_cache()
            citation.save_cache()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_citation_ref(obj, citation.handle)
                obj.save_cache()
                return redirect("/%s/%s#tab-citations" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        citation = Citation.objects.get(handle=handle)
        citation.delete()
        return redirect("/citation/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["citationform"] = citationform
    context["sourceform"] = sourceform
    context["object"] = citation
    context["citation"] = citation
    context["source"] = source
    context["action"] = act

    return render_to_response(view_template, context)
def process_repository(request,
                       context,
                       handle,
                       act,
                       add_to=None):  # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Repository")
    context["tviews"] = _("Repositories")
    context["action"] = "view"
    view_template = "view_repository_detail.html"

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        item, handle = add_to
        context["pickform"] = PickForm("Pick repository", Repository, (),
                                       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
        pickform = PickForm("Pick repository", Repository, (), request.POST)
        if pickform.data["picklist"]:
            parent_model = dji.get_model(item)  # what model?
            parent_obj = parent_model.objects.get(handle=handle)  # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Repository.objects.get(handle=ref_handle)
            dji.add_repository_ref_default(parent_obj, ref_obj)
            parent_obj.save_cache()  # rebuild cache
            return redirect("/%s/%s%s#tab-repositories" %
                            (item, handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = item
            return render_to_response("pick.html", context)
    elif act == "add":
        repository = Repository(gramps_id=dji.get_next_id(Repository, "R"))
        repositoryform = RepositoryForm(instance=repository)
        repositoryform.model = repository
    elif act in ["view", "edit"]:
        repository = Repository.objects.get(handle=handle)
        repositoryform = RepositoryForm(instance=repository)
        repositoryform.model = repository
    elif act == "save":
        repository = Repository.objects.get(handle=handle)
        repositoryform = RepositoryForm(request.POST, instance=repository)
        repositoryform.model = repository
        if repositoryform.is_valid():
            update_last_changed(repository, request.user.username)
            repository = repositoryform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create":
        repository = Repository(handle=create_id())
        repositoryform = RepositoryForm(request.POST, instance=repository)
        repositoryform.model = repository
        if repositoryform.is_valid():
            update_last_changed(repository, request.user.username)
            repository = repositoryform.save()
            if add_to:
                item, handle = add_to
                model = dji.get_model(item)
                obj = model.objects.get(handle=handle)
                dji.add_repository_ref_default(obj, repository)
                obj.save_cache()
                return redirect("/%s/%s#tab-repositories" % (item, handle))
            act = "view"
        else:
            act = "add"
    elif act == "delete":
        repository = Repository.objects.get(handle=handle)
        repository.delete()
        return redirect("/repository/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["repositoryform"] = repositoryform
    context["object"] = repository
    context["repository"] = repository
    context["action"] = act

    return render_to_response(view_template, context)
Пример #18
0
def process_family(request, context, handle, act, add_to=None): # view, edit, save
    """
    Process act on person. Can return a redirect.
    """
    context["tview"] = _("Family")
    context["tviews"] = _("Familes")

    if handle == "add":
        act = "add"
    if "action" in request.POST:
        act = request.POST.get("action")

    # Handle: edit, view, add, create, save, delete, share, save-share
    if act == "share":
        # Adds a person to an existing family
        item, handle = add_to
        context["pickform"] = PickForm("Pick family", 
                                       Family, 
                                       (),
                                       request.POST)     
        context["object_handle"] = handle
        context["object_type"] = "person"
        return render_to_response("pick.html", context)
    elif act == "save-share":
        item, handle = add_to 
        pickform = PickForm("Pick family", 
                            Family, 
                            (),
                            request.POST)
        if pickform.data["picklist"]:
            person = Person.objects.get(handle=handle) # to add
            ref_handle = pickform.data["picklist"]
            ref_obj = Family.objects.get(handle=ref_handle) 
            if item == "child":
                dji.add_child_ref_default(ref_obj, person) # add person to family
                #person.parent_families.add(ref_obj) # add family to child
                pfo = MyParentFamilies(person=person, family=ref_obj, 
                                       order=len(person.parent_families.all())+1)
                pfo.save()
            elif item == "spouse":
                if person.gender_type.name == "Female":
                    ref_obj.mother = person
                elif person.gender_type.name == "Male":
                    ref_obj.father = person
                else:
                    ref_obj.father = person # FIXME: Unknown gender, add to open
                #person.families.add(ref_obj) # add family to person
                pfo = MyFamilies(person=person, family=ref_obj, 
                                 order=len(person.families.all())+1)
                pfo.save()
            ref_obj.save()
            person.save()
            return redirect("/%s/%s%s#tab-references" % ("person", handle, build_search(request)))
        else:
            context["pickform"] = pickform
            context["object_handle"] = handle
            context["object_type"] = "person"
            return render_to_response("pick.html", context)
    elif act == "add":
        family = Family(
            gramps_id=dji.get_next_id(Family, "F"),
            family_rel_type=FamilyRelType.objects.get(
                val=FamilyRelType._DEFAULT[0]))
        if add_to:
            what, phandle = add_to
            person = Person.objects.get(handle=phandle)
            gender = person.gender_type.name # Male, Female, Unknown
            if what == "spouse":
                if gender == "Male":
                    family.father = person
                elif gender == "Female":
                    family.mother = person
                else: # You have to pick one!
                    family.father = person
            elif what == "child":
                pass # FIXME: can't show child in table? 
                     # Table from children_table
            else: # unknown what!
                raise Exception("can't add_to: '%s'" % what)
        familyform = FamilyForm(instance=family)
        familyform.model = family
    elif act in ["view", "edit"]: 
        family = Family.objects.get(handle=handle)
        familyform = FamilyForm(instance=family)
        familyform.model = family
    elif act == "save": # editing an existing family
        family = Family.objects.get(handle=handle)
        old_family_mother = family.mother
        old_family_father = family.father
        familyform = FamilyForm(request.POST, instance=family)
        familyform.model = family
        if familyform.is_valid():
            update_last_changed(family, request.user.username)
            family = familyform.save()
            # Remove family from previous mother/father if changed
            if familyform.cleaned_data["mother"] != old_family_mother and old_family_mother:
                MyFamilies.objects.get(person=old_family_mother, family=family).delete()
            if familyform.cleaned_data["father"] != old_family_father and old_family_father:
                MyFamilies.objects.get(person=old_family_father, family=family).delete()
            # Add family to newly selected mother/father if needed:
            if familyform.cleaned_data["mother"]:
                if family not in familyform.cleaned_data["mother"].families.all():
                    #family.mother.families.add(family)
                    pfo = MyFamilies(person=familyform.cleaned_data["mother"], family=family, 
                                     order=len(familyform.cleaned_data["mother"].families.all())+1)
                    pfo.save()
            if familyform.cleaned_data["father"]:
                if family not in familyform.cleaned_data["father"].families.all():
                    #family.father.families.add(family)
                    pfo = MyFamilies(person=family.father, family=family, 
                                     order=len(familyform.cleaned_data["father"].families.all())+1)
                    pfo.save()
            familyform.save()
            act = "view"
        else:
            act = "edit"
    elif act == "create": 
        family = Family(family_rel_type=FamilyRelType.objects.get(
                val=FamilyRelType._DEFAULT[0]),
                        handle=create_id())
        familyform = FamilyForm(request.POST, instance=family)
        familyform.model = family
        if familyform.is_valid():
            update_last_changed(family, request.user.username)
            family = familyform.save()
            if family.mother:
                #family.mother.families.add(family)
                pfo = MyFamilies(person=family.mother, family=family, 
                                 order=len(family.mother.families.all())+1)
                pfo.save()
            if family.father:
                #family.father.families.add(family)
                pfo = MyFamilies(person=family.father, family=family,
                                 order=len(family.father.families.all())+1)
                pfo.save()
            family.save_cache()
            if add_to: # add child or spouse to family
                item, handle = add_to
                person = Person.objects.get(handle=handle)
                if item == "child":
                    dji.add_child_ref_default(family, person) # add person to family
                    ##person.parent_families.add(family) # add family to child
                    pfo = MyParentFamilies(person=person, family=family,
                                           order=len(person.parent_families.all())+1)
                    pfo.save()
                #elif item == "spouse":
                # already added by selecting
                    person.save()
                return redirect("/%s/%s%s#tab-references" % ("person", handle, build_search(request)))
            act = "view"
        else:
            act = "add"
    elif act == "delete": 
        family = Family.objects.get(handle=handle)
        family.delete()
        return redirect("/family/")
    else:
        raise Exception("Unhandled act: '%s'" % act)

    context["familyform"] = familyform
    context["object"] = family
    context["family"] = family
    context["action"] = act
    view_template = "view_family_detail.html"
    
    return render_to_response(view_template, context)
Пример #19
0
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)