1 # -*- coding: utf-8 -*-
3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from django.contrib.auth.decorators import login_required
7 from django.contrib.auth.models import User
8 from django.shortcuts import render, redirect
9 from django.http import Http404
11 from organizations.filters import OrganizationFilterSet
12 from .forms import OrganizationForm, UserCardForm
13 from .models import Organization, Membership, UserCard
18 if request.method == 'POST':
19 form = OrganizationForm(request.POST, request.FILES)
22 org.set_user_status(request.user, Membership.Status.OWNER)
23 return redirect(org.get_absolute_url())
25 form = OrganizationForm()
26 return render(request, 'organizations/new.html', {'form': form})
29 def main(request, pk, tab='documents'):
31 org = Organization.objects.get(pk=pk)
32 except Organization.DoesNotExist:
35 my_status = org.get_user_status(request.user)
36 am_owner = my_status == Membership.Status.OWNER
37 am_member = am_owner or my_status == Membership.Status.REGULAR
39 return render(request, 'organizations/main.html', {
42 'my_status': my_status,
44 'am_member': am_member,
48 def user_card(request, pk):
50 user = User.objects.get(pk=pk)
51 except User.DoesNotExist:
53 card, created = UserCard.objects.get_or_create(pk=user.pk)
54 return render(request, 'organizations/user_card.html', {
60 def edit(request, pk):
62 org = Organization.objects.get(pk=pk)
63 except Organization.DoesNotExist:
66 if request.method == 'POST':
67 form = OrganizationForm(request.POST, request.FILES, instance=org)
70 return redirect(org.get_absolute_url())
72 form = OrganizationForm(instance=org)
73 return render(request, 'organizations/edit.html', {'form': form})
77 def user_edit(request):
78 card, created = UserCard.objects.get_or_create(pk=request.user.pk)
79 if request.method == 'POST':
80 form = UserCardForm(request.POST, request.FILES, instance=card)
83 return redirect(card.get_absolute_url())
85 form = UserCardForm(instance=card)
86 return render(request, 'organizations/edit_user.html', {'form': form})
90 def join(request, pk):
92 org = Organization.objects.get(pk=pk)
93 except Organization.DoesNotExist:
96 if request.method == 'POST':
97 org.set_user_status(request.user, Membership.Status.PENDING)
98 return redirect('organizations_members', org.pk)
100 return render(request, 'organizations/join.html', {'org': org})
104 def membership(request, pk):
106 org = Organization.objects.get(pk=pk)
107 except Organization.DoesNotExist:
111 user = User.objects.get(pk=request.POST['user'])
112 except KeyError, User.DoesNotExist:
115 if user != request.user:
116 action = request.POST.get('action')
118 if action == 'remove':
120 org.set_user_status(user, action)
121 return redirect('organizations_members', org.pk)
124 def organizations(request):
125 f = OrganizationFilterSet(request.GET, queryset=Organization.objects.all())
126 return render(request, "organizations/organizations.html", {