From 13058a41f7328e829bcf050be4ab2a6bbbce2c6c Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 29 Sep 2014 15:01:35 +0200 Subject: [PATCH] [TASK] Big unicode cleanup. --- citavi_mapper/urls.py | 14 ++--- frontend/forms.py | 50 +++++++-------- frontend/models.py | 17 +++--- frontend/templates/project/view-person.html | 5 -- frontend/templates/projects.html | 4 +- frontend/views.py | 67 +++++++++++---------- service/Citavi.py | 10 +-- service/Django.py | 4 ++ service/Mapper.py | 9 +++ 9 files changed, 94 insertions(+), 86 deletions(-) diff --git a/citavi_mapper/urls.py b/citavi_mapper/urls.py index a53010f..0571b10 100644 --- a/citavi_mapper/urls.py +++ b/citavi_mapper/urls.py @@ -10,12 +10,12 @@ from frontend.views import IndexView, ProjectView, ProjectUpdateView, ProjectPer urlpatterns = patterns( '', - url(r'^$', IndexView.as_view(), name='frontend-index'), - url(r'^project/$', ProjectView.as_view(), name='frontend-projects'), - url(r'^project/(?P\d+)/update$', ProjectUpdateView.as_view(), name='frontend-project-update'), - url(r'^project/(?P\d+)/map/person$', ProjectPersonView.as_view(), name='frontend-project-view-person'), - url(r'^project/(?P\d+)/map/person/(?P[\w\d-]+)$', ProjectMapPersonView.as_view(), name='frontend-project-map-person'), - url(r'^login/$', login_wrap, name='frontend-login'), - url(r'^logout/$', logout_wrap, name='frontend-logout'), + url(r'^$', IndexView.as_view(), name=u'frontend-index'), + url(r'^project/$', ProjectView.as_view(), name=u'frontend-projects'), + url(r'^project/(?P\d+)/update$', ProjectUpdateView.as_view(), name=u'frontend-project-update'), + url(r'^project/(?P\d+)/map/person$', ProjectPersonView.as_view(), name=u'frontend-project-view-person'), + url(r'^project/(?P\d+)/map/person/(?P[\w\d-]+)$', ProjectMapPersonView.as_view(), name=u'frontend-project-map-person'), + url(r'^login/$', login_wrap, name=u'frontend-login'), + url(r'^logout/$', logout_wrap, name=u'frontend-logout'), url(r'^admin/', include(admin.site.urls)), ) \ No newline at end of file diff --git a/frontend/forms.py b/frontend/forms.py index 01e992d..f2eea1b 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -9,42 +9,42 @@ class NewProjectForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(NewProjectForm, self).__init__(*args, **kwargs) self.helper = FormHelper() - self.helper.form_class = 'form-horizontal' - self.helper.label_class = 'col-lg-2' - self.helper.field_class = 'col-lg-4' - self.helper.form_method = 'post' + self.helper.form_class = u'form-horizontal' + self.helper.label_class = u'col-lg-2' + self.helper.field_class = u'col-lg-4' + self.helper.form_method = u'post' self.helper.layout = Layout( - 'name', - 'description', - Submit('send', 'Create', css_class='btn-default') + u'name', + u'description', + Submit(u'send', u'Create', css_class=u'btn-default') ) name = forms.CharField( - label="Name", + label=u"Name", max_length=255, required=True, ) description = forms.CharField( - label="Description", + label=u"Description", required=True, ) class Meta: model = Project - fields = ['name', 'description'] + fields = [u'name', u'description'] class FileUploadForm(forms.Form): def __init__(self, *args, **kwargs): super(FileUploadForm, self).__init__(*args, **kwargs) self.helper = FormHelper() - self.helper.form_class = 'form-horizontal' - self.helper.label_class = 'col-lg-2' - self.helper.field_class = 'col-lg-4' - self.helper.form_method = 'post' + self.helper.form_class = u'form-horizontal' + self.helper.label_class = u'col-lg-2' + self.helper.field_class = u'col-lg-4' + self.helper.form_method = u'post' self.helper.layout = Layout( - 'file', - Submit('send', 'Upload', css_class='btn-default') + u'file', + Submit(u'send', u'Upload', css_class=u'btn-default') ) file = forms.FileField() @@ -54,17 +54,17 @@ class PersonMapForm(forms.Form): def __init__(self, *args, **kwargs): super(PersonMapForm, self).__init__(*args, **kwargs) self.helper = FormHelper() - self.helper.form_class = 'form-horizontal' - self.helper.label_class = 'col-lg-2' - self.helper.field_class = 'col-lg-4' - self.helper.form_method = 'post' + self.helper.form_class = u'form-horizontal' + self.helper.label_class = u'col-lg-2' + self.helper.field_class = u'col-lg-4' + self.helper.form_method = u'post' self.helper.layout = Layout( - 'action', - 'global_identity', - Submit('skip', 'Skip', css_class='btn-default'), - Submit('save-continue', 'Save and continue', css_class='btn-default'), + u'action', + u'global_identity', + Submit(u'skip', u'Skip', css_class=u'btn-default'), + Submit(u'save-continue', u'Save and continue', css_class=u'btn-default'), ) - action = forms.ChoiceField(choices=[('new', 'Create new global Identity'), ('existing', 'Map to existing identity')], initial='new', widget=forms.RadioSelect()) + 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) diff --git a/frontend/models.py b/frontend/models.py index 972ec79..59907fb 100644 --- a/frontend/models.py +++ b/frontend/models.py @@ -2,7 +2,6 @@ from django.db import models - class Project(models.Model): """ Model representing a citavi project. """ name = models.CharField(max_length=255) @@ -10,12 +9,12 @@ class Project(models.Model): associated_filename = models.CharField(max_length=255, blank=True, null=True) def __unicode__(self): - repr = self.name + temp = unicode(self.name) if self.associated_filename: - repr += " (" + self.associated_filename + ")" + temp += u" (" + unicode(self.associated_filename) + u")" else: - repr += " (empty)" - return repr + temp += u" (empty)" + return temp class PersonGlobalIdentity(models.Model): @@ -24,8 +23,9 @@ class PersonGlobalIdentity(models.Model): # TODO: Extend this for further stuff - maybe vivo external url or something? def __unicode__(self): - repr = "" - return repr + from service.Mapper import PersonMapper + pm = PersonMapper() + return u"" class CitaviProjectIdentity(models.Model): @@ -36,5 +36,4 @@ class CitaviProjectIdentity(models.Model): preferred = models.BooleanField() def __unicode__(self): - repr = "" - return repr + return u"" diff --git a/frontend/templates/project/view-person.html b/frontend/templates/project/view-person.html index 19b1d51..1539ab0 100644 --- a/frontend/templates/project/view-person.html +++ b/frontend/templates/project/view-person.html @@ -11,11 +11,6 @@

Description: {{project.description}}

Current uploaded database filename: {{project.associated_filename}}

- -

Stats

Mapped: {{mapped_persons|length}}
diff --git a/frontend/templates/projects.html b/frontend/templates/projects.html index e37aeab..aa64afe 100644 --- a/frontend/templates/projects.html +++ b/frontend/templates/projects.html @@ -27,8 +27,8 @@ {{project.description}} {{project.associated_filename}} - Update project - Persons + Update, + Map {% endfor %} diff --git a/frontend/views.py b/frontend/views.py index eb9d7fe..7405c24 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -17,25 +17,25 @@ from frontend.forms import NewProjectForm, FileUploadForm, PersonMapForm from frontend.models import Project -FRONTEND_PAGE_NAME = 'Citavi Mapper' +FRONTEND_PAGE_NAME = u'Citavi Mapper' # Login wrapper functions def login_wrap(*args, **kwargs): """ Wrapper function for login page. """ - kwargs['extra_context'] = { - 'page': { - 'name': FRONTEND_PAGE_NAME, - 'title': 'Login' + kwargs[u'extra_context'] = { + u'page': { + u'name': FRONTEND_PAGE_NAME, + u'title': 'Login' } } return login(*args, **kwargs) def logout_wrap(*args, **kwargs): """ Wrapper function for logout page. """ - kwargs['extra_context'] = { - 'page': { - 'name': FRONTEND_PAGE_NAME, - 'title': 'Logout' + kwargs[u'extra_context'] = { + u'page': { + u'name': FRONTEND_PAGE_NAME, + u'title': 'Logout' } } return logout(*args, **kwargs) @@ -44,9 +44,9 @@ def logout_wrap(*args, **kwargs): # My base classes for views class MyViewMixin(object): """ Basic view mixin to add global variables to all templates. """ - template_name = 'base.html' + template_name = u'base.html' page_name = FRONTEND_PAGE_NAME - page_title = 'BASE' + page_title = u'BASE' def get_page_data(self): return { @@ -95,18 +95,18 @@ class ProtectedUpdateView(LoggedInMixin, MyUpdateView): # Actual Views class IndexView(ProtectedTemplateView): - template_name = 'index.html' - page_title = 'Index' + template_name = u'index.html' + page_title = u'Index' class ProjectView(ProtectedFormView): - template_name = 'projects.html' - page_title = 'Projects' + template_name = u'projects.html' + page_title = u'Projects' form_class = NewProjectForm - success_url = '/project' + success_url = u'/project' def get_context_data(self, **kwargs): - kwargs[u'projects'] = Project.objects.order_by('id') + kwargs[u'projects'] = Project.objects.order_by(u'id') return super(ProjectView, self).get_context_data(**kwargs) def form_valid(self, form): @@ -115,11 +115,11 @@ class ProjectView(ProtectedFormView): class ProjectUpdateView(ProtectedFormView, SingleObjectMixin): - template_name = 'project/update.html' - page_title = 'Update project' + template_name = u'project/update.html' + page_title = u'Update project' form_class = FileUploadForm - success_url = '/project/' + success_url = u'/project/' def get(self, request, *args, **kwargs): self.project_id = kwargs[u'project_id'] @@ -128,16 +128,17 @@ class ProjectUpdateView(ProtectedFormView, SingleObjectMixin): def post(self, request, *args, **kwargs): self.project_id = kwargs[u'project_id'] - self.success_url = self.success_url + self.project_id + '/update' + self.object = Project.objects.get(pk=self.project_id) + self.success_url = self.success_url + self.project_id + u'/update' return super(ProjectUpdateView, self).post(request, *args, **kwargs) def form_valid(self, form, *args, **kwargs): """ This form_valid handles the file upload. """ original_file = form.files[u'file'] - original_filename = str(original_file) + original_filename = unicode(original_file) original_contentfile = ContentFile(original_file.read()) """ Put file into temporary folder for analysis """ - target_filename = 'tmp/project_' + str(self.project_id) + '.ctt4' + target_filename = u'tmp/project_' + unicode(self.project_id) + u'.ctt4' relative_path = default_storage.save(target_filename, original_contentfile) temp_sqlite = os.path.join(settings.MEDIA_ROOT, relative_path) @@ -154,7 +155,7 @@ class ProjectUpdateView(ProtectedFormView, SingleObjectMixin): """ TODO: Put up an error message or something. """ pass else: - target_filename = 'citavi/project_' + str(self.project_id) + '.ctt4' + target_filename = u'citavi/project_' + unicode(self.project_id) + u'.ctt4' """ Remove eventually pre-existing citavi file. """ if default_storage.exists(target_filename): default_storage.delete(target_filename) @@ -177,11 +178,11 @@ class ProjectUpdateView(ProtectedFormView, SingleObjectMixin): class ProjectPersonView(ProtectedFormView, SingleObjectMixin): - template_name = 'project/view-person.html' - page_title = 'Person List View' + template_name = u'project/view-person.html' + page_title = u'Person List View' form_class = FileUploadForm - success_url = '/projects/' + success_url = u'/project/' def get_context_data(self, **kwargs): project = self.object @@ -207,11 +208,11 @@ class ProjectPersonView(ProtectedFormView, SingleObjectMixin): class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin): - template_name = 'project/map-person.html' - page_title = 'Person Mapping' + template_name = u'project/map-person.html' + page_title = u'Person Mapping' form_class = PersonMapForm - success_url = '/project' + success_url = u'/project' def get_context_data(self, **kwargs): project = self.object @@ -239,11 +240,11 @@ class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin): pm = Mapper.PersonMapper() person = pm.get_person_by_uuid(self.object, self.person_uuid) - print str(form.cleaned_data) + print unicode(form.cleaned_data) # TODO: do mapping according to parameters, override success_url to point to next person! - if form.cleaned_data['action'] == 'new': + if form.cleaned_data[u'action'] == u'new': pm.create_new_identity(self.object, person) - elif form.cleaned_data['action'] == 'skip': + elif form.cleaned_data[u'action'] == u'skip': pass return super(ProjectMapPersonView, self).form_valid(form) diff --git a/service/Citavi.py b/service/Citavi.py index de71a66..cd333b7 100644 --- a/service/Citavi.py +++ b/service/Citavi.py @@ -31,19 +31,19 @@ class Project(): except: self._is_error = True # TODO: better error handling! - print "An error occured within a get_persons call!" + print u"An error occured within a get_persons call!" return False def get_person_by_uuid(self, uuid): """ Returns a person from the Citavi project by their uuid. """ try: person_class = self._sa_sqlite_autobase.classes.Person - citavi_person = self._sa_sqlite_session.query(person_class).filter('ID=\'' + str(uuid) + '\'').all() + citavi_person = self._sa_sqlite_session.query(person_class).filter(u'ID=\'' + unicode(uuid) + u'\'').all() return citavi_person[0] except: self._is_error = True # TODO: better error handling! - print "An error occured within a get_person_by_uuid call!" + print u"An error occured within a get_person_by_uuid call!" return False def open(self): @@ -59,7 +59,7 @@ class Project(): def _open(self): """ Internal method to open a citavi project file. """ try: - self._sa_sqlite_engine = create_engine('sqlite+pysqlite:///' + self.sqlite_file) + self._sa_sqlite_engine = create_engine(u'sqlite+pysqlite:///' + self.sqlite_file) self._sa_sqlite_session = Session(self._sa_sqlite_engine) self._sa_sqlite_meta = MetaData(bind=self._sa_sqlite_engine) self._sa_sqlite_meta.reflect() @@ -96,7 +96,7 @@ class ProjectManager(): self._projects.clear() def get_path_for_project_id(self, project_id): - return 'media/citavi/project_' + str(project_id) + '.ctt4' + return u'media/citavi/project_' + unicode(project_id) + u'.ctt4' def _add_project(self, project_id): """ Internal method to add a Project instance if not existing. """ diff --git a/service/Django.py b/service/Django.py index 37e2623..a313fd2 100644 --- a/service/Django.py +++ b/service/Django.py @@ -26,3 +26,7 @@ class PersonIdentityManager(): def get_mapped_identities_for_project(self, project_instance): """ Returns all existing (mapped) identies for a given project. """ return CitaviProjectIdentity.objects.filter(project=project_instance).all() + + def get_citavi_identity_by_global_identity(self, global_identity): + """ Returns the preferred citavi identity based on their global identity. """ + return CitaviProjectIdentity.objects.filter(global_identity=global_identity, preferred=True).all()[0] diff --git a/service/Mapper.py b/service/Mapper.py index 2b78610..55eda26 100644 --- a/service/Mapper.py +++ b/service/Mapper.py @@ -46,3 +46,12 @@ class PersonMapper(): def get_person_by_uuid(self, project, uuid): """ Returns a person from a citavi project by uuid. """ return self._citavi_project_manager.get_person_by_uuid(project.id, uuid) + + def get_representation_for_global_identity(self, global_identity): + """ Returns a unicode string representation for a global identiy. """ + 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) + else: + return unicode(global_identity) \ No newline at end of file