diff --git a/frontend/forms.py b/frontend/forms.py index 8f53271..cebe7f4 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -51,6 +51,7 @@ class FileUploadForm(forms.Form): class PersonMapForm(forms.Form): + """ Form containing the person mapping 'wizard'. """ def __init__(self, *args, **kwargs): super(PersonMapForm, self).__init__(*args, **kwargs) self.helper = FormHelper() @@ -66,6 +67,17 @@ class PersonMapForm(forms.Form): Submit(u'save-continue', u'Save and continue', css_class=u'btn-default'), ) + def clean(self): + """ Make sure the submitted data is okay. """ + cleaned_data = super(PersonMapForm, self).clean() + action = cleaned_data.get(u'action') + global_identity = cleaned_data.get(u'global_identity') + if action == u'existing' and not global_identity: + msg = u"Cannot map to non-existing global identity!" + self._errors["global_identity"] = self.error_class([msg]) + return cleaned_data + action = forms.ChoiceField(choices=[(u'new', u'Create new global Identity'), (u'existing', u'Map to existing identity')], initial=u'new', widget=forms.RadioSelect()) global_identity = forms.ModelChoiceField(queryset=PersonGlobalIdentity.objects.all(), required=False) preferred_identity = forms.BooleanField(initial=False, required=False) + diff --git a/frontend/models.py b/frontend/models.py index cf44497..1d0a39c 100644 --- a/frontend/models.py +++ b/frontend/models.py @@ -13,7 +13,7 @@ class Project(models.Model): if self.associated_filename: temp += u" (" + unicode(self.associated_filename) + u")" else: - temp += u" (empty)" + temp += u" (No project file)" return temp @@ -24,7 +24,7 @@ class PersonGlobalIdentity(models.Model): def __unicode__(self): from service.Mapper import person_mapper - return u"" + return person_mapper.get_representation_for_global_identity(self) + u" [" u"ID=" + unicode(self.id) + u", Type=" + unicode(self.type) + u"]" class CitaviProjectIdentity(models.Model): @@ -35,4 +35,8 @@ class CitaviProjectIdentity(models.Model): preferred = models.BooleanField() def __unicode__(self): - return u"" + from service.Mapper import person_mapper + citavi_repr = person_mapper.get_representation_for_citavi_identity(self) + if self.preferred == True: + citavi_repr = citavi_repr + u" " + return citavi_repr \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index 316b520..3334910 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -12,6 +12,7 @@ from django.views.generic.detail import SingleObjectMixin from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.contrib.auth.views import login, logout +from django.forms import ValidationError from frontend.forms import NewProjectForm, FileUploadForm, PersonMapForm @@ -245,8 +246,7 @@ class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin): if form.cleaned_data[u'action'] == u'new': person_mapper.create_new_identity(self.object, person) elif form.cleaned_data[u'action'] == u'existing': - # TODO preferred = FALSE is not desired. - print form.cleaned_data - person_mapper.map_identity_to_existing(form.cleaned_data[u'global_identity'], self.object, person.ID, form.cleaned_data[u'preferred_identity']) + global_identity = form.cleaned_data[u'global_identity'] + person_mapper.map_identity_to_existing(global_identity, self.object, person.ID, form.cleaned_data[u'preferred_identity']) return super(ProjectMapPersonView, self).form_valid(form) diff --git a/service/Django.py b/service/Django.py index 1f11458..990e79b 100644 --- a/service/Django.py +++ b/service/Django.py @@ -15,6 +15,9 @@ class PersonIdentityManager(): def add_identity_to_global_identity(self, global_identity, project, uuid, preferred): """ Maps given citavi project identity to existing global identity. """ + # First, fetch all involved CitaviProjectIdentities and set them to preferred=False + CitaviProjectIdentity.objects.filter(global_identity=global_identity).update(preferred=False) + # Then add the new primary ProjectIdentity. cpi = CitaviProjectIdentity(global_identity=global_identity, project=project, citavi_uuid=uuid, preferred=preferred) cpi.save() return cpi diff --git a/service/Mapper.py b/service/Mapper.py index e649369..d6bbcb6 100644 --- a/service/Mapper.py +++ b/service/Mapper.py @@ -60,14 +60,18 @@ class PersonMapper(): if global_identity.type == 'citavi': citavi_identity = self._person_identity_manager.get_citavi_identity_by_global_identity(global_identity) citavi_person = self._citavi_project_manager.get_person_by_uuid(citavi_identity.project_id, citavi_identity.citavi_uuid) - return unicode(citavi_person.ID) + u" " + unicode(citavi_person.FirstName) + u" " + unicode(citavi_person.LastName) + return self._representation_for_citavi_person(citavi_person) else: return u'Not implemented yet!' def get_representation_for_citavi_identity(self, citavi_identity): """ Returns a unicode representation for a citavi project identiy. """ citavi_person = self._citavi_project_manager.get_person_by_uuid(citavi_identity.project_id, citavi_identity.citavi_uuid) - return unicode(citavi_person.ID) + u" " + unicode(citavi_person.FirstName) + u" " + unicode(citavi_person.LastName) + return self._representation_for_citavi_person(citavi_person) + + def _representation_for_citavi_person(self, citavi_person): + p = citavi_person + return u"<" + unicode(p.FirstName) + u" " + unicode(p.LastName) + u">" # Create a PersonMapper instance for public use, so ressources are not wasted.