From e77aecadbc73436b0fdba961b1aef439ad2eed31 Mon Sep 17 00:00:00 2001 From: Kasper Brandt Date: Thu, 9 Oct 2014 15:49:56 +0300 Subject: [PATCH] [#766 #776] Link user to organisation(s) --- akvo/rest/serializers/__init__.py | 2 + akvo/rest/serializers/employment.py | 20 ++++++++++ akvo/rest/urls.py | 1 + akvo/rest/views/__init__.py | 2 + akvo/rest/views/employment.py | 18 +++++++++ akvo/rsr/forms.py | 18 ++++++++- akvo/rsr/models/__init__.py | 4 +- akvo/templates/myrsr/my_details.html | 59 +++++++++++++++++++++++----- 8 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 akvo/rest/serializers/employment.py create mode 100644 akvo/rest/views/employment.py diff --git a/akvo/rest/serializers/__init__.py b/akvo/rest/serializers/__init__.py index becd09f173..955dc440e9 100644 --- a/akvo/rest/serializers/__init__.py +++ b/akvo/rest/serializers/__init__.py @@ -10,6 +10,7 @@ from .budget_item_label import BudgetItemLabelSerializer from .category import CategorySerializer from .country import CountrySerializer +from .employment import EmploymentSerializer from .focus_area import FocusAreaSerializer from .goal import GoalSerializer from .internal_organisation_id import InternalOrganisationIDSerializer @@ -35,6 +36,7 @@ 'BudgetItemLabelSerializer', 'CategorySerializer', 'CountrySerializer', + 'EmploymentSerializer', 'FocusAreaSerializer', 'GoalSerializer', 'InternalOrganisationIDSerializer', diff --git a/akvo/rest/serializers/employment.py b/akvo/rest/serializers/employment.py new file mode 100644 index 0000000000..87eccd9153 --- /dev/null +++ b/akvo/rest/serializers/employment.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +# Akvo RSR is covered by the GNU Affero General Public License. +# See more details in the license.txt file located at the root folder of the Akvo RSR module. +# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >. + +from rest_framework import serializers + +from akvo.rsr.models import Employment + +from .rsr_serializer import BaseRSRSerializer + + +class EmploymentSerializer(BaseRSRSerializer): + + organisation_name = serializers.Field(source='organisation.name') + country_name = serializers.Field(source='country.name') + + class Meta: + model = Employment diff --git a/akvo/rest/urls.py b/akvo/rest/urls.py index 84bda9965d..c8bc3f6905 100644 --- a/akvo/rest/urls.py +++ b/akvo/rest/urls.py @@ -17,6 +17,7 @@ router.register(r'budget_item_label', views.BudgetItemLabelViewSet) router.register(r'category', views.CategoryViewSet) router.register(r'country', views.CountryViewSet) +router.register(r'employment', views.EmploymentViewSet) router.register(r'focus_area', views.FocusAreaViewSet) router.register(r'goal', views.GoalViewSet) router.register(r'internal_organisation_id', views.InternalOrganisationIDViewSet) diff --git a/akvo/rest/views/__init__.py b/akvo/rest/views/__init__.py index b02f435bac..095f599039 100644 --- a/akvo/rest/views/__init__.py +++ b/akvo/rest/views/__init__.py @@ -11,6 +11,7 @@ from .budget_item_label import BudgetItemLabelViewSet from .category import CategoryViewSet from .country import CountryViewSet +from .employment import EmploymentViewSet from .focus_area import FocusAreaViewSet from .goal import GoalViewSet from .internal_organisation_id import InternalOrganisationIDViewSet @@ -36,6 +37,7 @@ 'BudgetItemLabelViewSet', 'CategoryViewSet', 'CountryViewSet', + 'EmploymentViewSet', 'FocusAreaViewSet', 'GoalViewSet', 'InternalOrganisationIDViewSet', diff --git a/akvo/rest/views/employment.py b/akvo/rest/views/employment.py new file mode 100644 index 0000000000..95cdae582d --- /dev/null +++ b/akvo/rest/views/employment.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- + +# Akvo RSR is covered by the GNU Affero General Public License. +# See more details in the license.txt file located at the root folder of the Akvo RSR module. +# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >. + + +from akvo.rsr.models import Employment + +from ..serializers import EmploymentSerializer +from ..viewsets import BaseRSRViewSet + + +class EmploymentViewSet(BaseRSRViewSet): + """ + """ + queryset = Employment.objects.all() + serializer_class = EmploymentSerializer diff --git a/akvo/rsr/forms.py b/akvo/rsr/forms.py index 12426c115a..379b826216 100644 --- a/akvo/rsr/forms.py +++ b/akvo/rsr/forms.py @@ -16,7 +16,7 @@ from registration.models import RegistrationProfile -from .models import Organisation +from .models import Country, Organisation class RegisterForm(forms.Form): email = forms.EmailField( @@ -165,7 +165,21 @@ class UserOrganisationForm(forms.Form): organisation = forms.ModelChoiceField( queryset=Organisation.objects.all(), label='', - initial='Organisation' + empty_label='Organisation' + ) + job_title = forms.CharField( + label='', + required=False, + max_length=50, + widget=forms.TextInput( + attrs={'placeholder': 'Job title (optional)'} + ), + ) + country = forms.ModelChoiceField( + queryset=Country.objects.all(), + label='', + required=False, + empty_label='Country (optional)' ) def __init__(self, *args, **kwargs): diff --git a/akvo/rsr/models/__init__.py b/akvo/rsr/models/__init__.py index 99668f554f..ba49affc49 100644 --- a/akvo/rsr/models/__init__.py +++ b/akvo/rsr/models/__init__.py @@ -28,6 +28,7 @@ from .budget_item import BudgetItem, BudgetItemLabel, CountryBudgetItem from .country import Country, RecipientCountry from .category import Category +from .employment import Employment from .focus_area import FocusArea from .goal import Goal from .indicator import Indicator, IndicatorPeriod @@ -57,7 +58,6 @@ from .sector import Sector from .transaction import Transaction from .user import User -# from .user_profile import UserProfile __all__ = [ 'Benchmark', @@ -68,6 +68,7 @@ 'Country', 'RecipientCountry', 'Category', + 'Employment', 'FocusArea', 'Goal', 'Indicator', @@ -102,7 +103,6 @@ 'Sector', 'Transaction', 'User', - # 'UserProfile', ] # signals! diff --git a/akvo/templates/myrsr/my_details.html b/akvo/templates/myrsr/my_details.html index 9561f8becd..faf0c8401a 100644 --- a/akvo/templates/myrsr/my_details.html +++ b/akvo/templates/myrsr/my_details.html @@ -67,12 +67,42 @@ $('#organisationForm').submit(function(event) { - var $form = $(this); - var serializedData = $form.serialize(); + serializedData = {'user': '{{ user.pk }}'}; + $.each($(this).serializeArray(), function(i, obj) { serializedData[obj.name] = obj.value }); - $.post('/myrsr/', serializedData, function(response) { - response = JSON.parse(response); - $( "#organisations" ).prepend(''); + $.ajax({ + type:"POST", + url: "/rest/v1/employment/?format=json", + data : JSON.stringify(serializedData), + contentType : 'application/json; charset=UTF-8', + success: function(response){ + org_entry = '
  • ' + response.organisation_name; + if (response.job_title != "") { + org_entry += ' ( ' + response.job_title; + if (response.country_name != null) { + org_entry += ' , ' + response.country_name; + } + org_entry += ' )'; + } else if (response.country_name != null) { + org_entry += ' ( ' + response.country_name + ' )'; + } + org_entry += ' Pending approval
  • '; + $( "#organisations" ).prepend(''); + $( "#noOrganisation").remove(); + $( "#organisationsList").append(org_entry); + }, + error: function(response) + { + if (response['status'] == 500) { + $("#organisations").prepend(''); + } else { + jsonValue = $.parseJSON(response['responseText']); + + $.each(jsonValue, function (key, value) { + $("#organisations").prepend(''); + }); + } + } }); event.preventDefault(); @@ -100,14 +130,23 @@

    My details

    My organisations

    - {% if user.organisations.all %} -