Skip to content

Commit

Permalink
[#766 #776] Link user to organisation(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Oct 9, 2014
1 parent 15c4772 commit e77aeca
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 14 deletions.
2 changes: 2 additions & 0 deletions akvo/rest/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,6 +36,7 @@
'BudgetItemLabelSerializer',
'CategorySerializer',
'CountrySerializer',
'EmploymentSerializer',
'FocusAreaSerializer',
'GoalSerializer',
'InternalOrganisationIDSerializer',
Expand Down
20 changes: 20 additions & 0 deletions akvo/rest/serializers/employment.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions akvo/rest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions akvo/rest/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +37,7 @@
'BudgetItemLabelViewSet',
'CategoryViewSet',
'CountryViewSet',
'EmploymentViewSet',
'FocusAreaViewSet',
'GoalViewSet',
'InternalOrganisationIDViewSet',
Expand Down
18 changes: 18 additions & 0 deletions akvo/rest/views/employment.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 16 additions & 2 deletions akvo/rsr/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions akvo/rsr/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,7 +58,6 @@
from .sector import Sector
from .transaction import Transaction
from .user import User
# from .user_profile import UserProfile

__all__ = [
'Benchmark',
Expand All @@ -68,6 +68,7 @@
'Country',
'RecipientCountry',
'Category',
'Employment',
'FocusArea',
'Goal',
'Indicator',
Expand Down Expand Up @@ -102,7 +103,6 @@
'Sector',
'Transaction',
'User',
# 'UserProfile',
]

# signals!
Expand Down
59 changes: 49 additions & 10 deletions akvo/templates/myrsr/my_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div class="alert alert-' + response["status"] + '" role="alert">' + response["message"] + '</div>');
$.ajax({
type:"POST",
url: "/rest/v1/employment/?format=json",
data : JSON.stringify(serializedData),
contentType : 'application/json; charset=UTF-8',
success: function(response){
org_entry = '<li>' + 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 += ' <i>Pending approval</i></li>';
$( "#organisations" ).prepend('<div class="alert alert-success" role="alert">Request has been sent.</div>');
$( "#noOrganisation").remove();
$( "#organisationsList").append(org_entry);
},
error: function(response)
{
if (response['status'] == 500) {
$("#organisations").prepend('<div class="alert alert-danger" role="alert">You are already linked to this organisation.</div>');
} else {
jsonValue = $.parseJSON(response['responseText']);

$.each(jsonValue, function (key, value) {
$("#organisations").prepend('<div class="alert alert-danger" role="alert">' + key + ': ' + value + '</div>');
});
}
}
});

event.preventDefault();
Expand Down Expand Up @@ -100,14 +130,23 @@ <h2>My details</h2>

<div class="col-md-5 col-xs-6 col-ts-12" id="organisations">
<h2>My organisations</h2>
{% if user.organisations.all %}
<ul>
{% for org in user.organisations.all %}
<li>{{ org.name }}</li>
{% if user.employments.all %}
<ul id="organisationsList">
{% for empl in user.employments.all %}
<li>
{{ empl.organisation.name }}
{% if empl.job_title or empl.country %} (
{% if empl.job_title %}{{ empl.job_title }}{% endif %}
{% if empl.job_title and empl.country %}, {% endif %}
{% if empl.country %}{{ empl.country.name }}{% endif %}
){% endif %}
{% if not empl.is_approved %} <i>Pending approval</i>{% endif %}
</li>
{% endfor %}
</ul>
{% else %}
<h2><small>Not affiliated with an organisation.</small></h2>
<h2 id="noOrganisation"><small>Not affiliated with an organisation.</small></h2>
<ul id="organisationsList"></ul>
{% endif %}

<h3>Connect with an organisation</h3>
Expand Down

0 comments on commit e77aeca

Please sign in to comment.