1 # Create your views here.
2 from django.contrib.auth.decorators import login_required
3 from django.contrib.auth.models import User
4 from django.shortcuts import render, redirect
5 from django.http import HttpResponse, Http404
6 #from django.views.decorators import require_post
7 from .forms import OrganizationForm, UserCardForm
8 from .models import Organization, Membership, UserCard
12 if request.method == 'POST':
13 form = OrganizationForm(request.POST, request.FILES)
16 org.set_user_status(request.user, Membership.Status.OWNER)
17 return redirect(org.get_absolute_url())
19 form = OrganizationForm()
20 return render(request, 'organizations/new.html', {'form': form})
23 def main(request, pk, tab='documents'):
25 org = Organization.objects.get(pk=pk)
26 except Organization.DoesNotExist:
29 my_status = org.get_user_status(request.user)
30 am_owner = my_status == Membership.Status.OWNER
31 am_member = am_owner or my_status == Membership.Status.REGULAR
33 return render(request, 'organizations/main.html', {
36 'my_status': my_status,
38 'am_member': am_member,
41 def user_card(request, pk):
43 user = User.objects.get(pk=pk)
44 except User.DoesNotExist:
46 card, created = UserCard.objects.get_or_create(pk=user.pk)
47 return render(request, 'organizations/user_card.html', {
53 def edit(request, pk):
55 org = Organization.objects.get(pk=pk)
56 except Organization.DoesNotExist:
59 if request.method == 'POST':
60 form = OrganizationForm(request.POST, request.FILES, instance=org)
63 return redirect(org.get_absolute_url())
65 form = OrganizationForm(instance=org)
66 return render(request, 'organizations/edit.html', {'form': form})
70 def user_edit(request):
71 card, created = UserCard.objects.get_or_create(pk=request.user.pk)
72 if request.method == 'POST':
73 form = UserCardForm(request.POST, request.FILES, instance=card)
76 return redirect(card.get_absolute_url())
78 form = UserCardForm(instance=card)
79 return render(request, 'organizations/edit_user.html', {'form': form})
83 def join(request, pk):
85 org = Organization.objects.get(pk=pk)
86 except Organization.DoesNotExist:
89 if request.method == 'POST':
90 org.set_user_status(request.user, Membership.Status.PENDING)
91 return redirect('organizations_members', org.pk)
93 return render(request, 'organizations/join.html', {'org': org})
97 def membership(request, pk):
99 org = Organization.objects.get(pk=pk)
100 except Organization.DoesNotExist:
104 user = User.objects.get(pk=request.POST['user'])
105 except KeyError, User.DoesNotExist:
108 if user != request.user:
109 action = request.POST.get('action')
111 if action == 'remove':
113 org.set_user_status(user, action)
114 return redirect('organizations_members', org.pk)
117 def organizations(request):
118 return render(request, "organizations/organizations.html", {
119 'organizations': Organization.objects.all(),