Revision 0f4fa26d snf-astakos-app/astakos/im/views.py

b/snf-astakos-app/astakos/im/views.py
57 57
from django.views.generic.create_update import *
58 58
from django.views.generic.list_detail import *
59 59

  
60
from astakos.im.models import AstakosUser, Invitation, ApprovalTerms, AstakosGroup
60
from astakos.im.models import AstakosUser, Invitation, ApprovalTerms, AstakosGroup, Resource
61 61
from astakos.im.activation_backends import get_backend, SimpleBackend
62 62
from astakos.im.util import get_context, prepare_response, set_cookie, get_query
63 63
from astakos.im.forms import *
......
589 589

  
590 590
@signed_terms_required
591 591
@login_required
592
def group_add(request):
593
    return create_object(request,
594
                         form_class=get_astakos_group_creation_form(request),
595
                         post_save_redirect = '/im/group/%(id)s/')
592
def group_add(request, kind_name='default'):
593
    try:
594
        kind = GroupKind.objects.get(name = kind_name)
595
    except:
596
        return HttpResponseBadRequest(_('No such group kind'))
597
    
598
    template_name=None,
599
    template_loader=loader
600
    extra_context=None
601
    post_save_redirect='/im/group/%(id)s/'
602
    login_required=False
603
    context_processors=None
604
    model, form_class = get_model_and_form_class(
605
        model=None,
606
        form_class=AstakosGroupCreationForm
607
    )
608
    # TODO better approach???
609
    resources = dict( (str(r.id), r) for r in Resource.objects.select_related().all() )
610
    if request.method == 'POST':
611
        form = form_class(request.POST, request.FILES, resources=resources)
612
        if form.is_valid():
613
            new_object = form.save()
614
            new_object.owners = [request.user]
615
            for (rid, limit) in form.resources():
616
                try:
617
                    r = resources[rid]
618
                except KeyError, e:
619
                    logger.exception(e)
620
                    # Should I stay or should I go???
621
                    continue
622
                else:
623
                    new_object.astakosgroupquota_set.create(
624
                        resource = r,
625
                        limit = limit
626
                    )
627
            msg = _("The %(verbose_name)s was created successfully.") %\
628
                                    {"verbose_name": model._meta.verbose_name}
629
            messages.success(request, msg, fail_silently=True)
630
            return redirect(post_save_redirect, new_object)
631
    else:
632
        now = datetime.now()
633
        data = {
634
            'kind':kind,
635
            'issue_date':now,
636
            'expiration_date':now + timedelta(days=30)
637
        }
638
        form = form_class(data, resources=resources)
639

  
640
    # Create the template, context, response
641
    template_name = "%s/%s_form.html" % (
642
        model._meta.app_label,
643
        model._meta.object_name.lower()
644
    )
645
    t = template_loader.get_template(template_name)
646
    c = RequestContext(request, {
647
        'form': form
648
    }, context_processors)
649
    return HttpResponse(t.render(c))
596 650

  
597 651
@signed_terms_required
598 652
@login_required
599 653
def group_list(request):
600
    list = AstakosGroup.objects.filter(membership__person=request.user)
654
    list = request.user.astakos_groups.select_related().all()
601 655
    return object_list(request, queryset=list)
602 656

  
603 657
@signed_terms_required
......
608 662
    except AstakosGroup.DoesNotExist:
609 663
        return HttpResponseBadRequest(_('Invalid group.'))
610 664
    return object_detail(request,
611
                         AstakosGroup.objects.all(),
612
                         object_id=group_id,
613
                         extra_context = {'form':get_astakos_group_policy_creation_form(group),
614
                                          'quota':group.quota,
615
                                          'more_policies':group.has_undefined_policies})
616

  
617
@signed_terms_required
618
@login_required
619
def group_policies_list(request, group_id):
620
    list = AstakosGroupQuota.objects.filter(group__id=group_id)
621
    return object_list(request, queryset=list)
665
         AstakosGroup.objects.all(),
666
         object_id=group_id,
667
         extra_context = {'quota':group.quota}
668
    )
622 669

  
623 670
@signed_terms_required
624 671
@login_required
625
def group_policies_add(request, group_id):
626
    try:
627
        group = AstakosGroup.objects.select_related().get(id=group_id)
628
    except AstakosGroup.DoesNotExist:
629
        return HttpResponseBadRequest(_('Invalid group.'))
630
    return create_object(request,
631
                         form_class=get_astakos_group_policy_creation_form(group),
632
                         template_name = 'im/astakosgroup_detail.html',
633
                         post_save_redirect = reverse('group_detail', kwargs=dict(group_id=group_id)),
634
                         extra_context = {'group':group,
635
                                          'quota':group.quota,
636
                                          'more_policies':group.has_undefined_policies})
637
@signed_terms_required
638
@login_required
639 672
def group_approval_request(request, group_id):
640 673
    return HttpResponse()
641 674

  
......
653 686
            queryset = AstakosGroup.objects.select_related().filter(name=q)
654 687
            f = MembershipCreationForm
655 688
            for g in queryset:
656
                join_forms[g.name] = f(dict(group=g,
657
                                            person=request.user,
658
                                            date_requested=datetime.now().strftime("%d/%m/%Y")))
659
            return object_list(request,
660
                                queryset,
661
                                template_name='im/astakosgroup_list.html',
662
                                extra_context=dict(form=form, is_search=True, join_forms=join_forms))
663
    return render_response(template='im/astakosgroup_list.html',
664
                            form = form,
665
                            context_instance=get_context(request))
689
                join_forms[g.name] = f(
690
                    dict(
691
                        group=g,
692
                        person=request.user,
693
                        date_requested=datetime.now().strftime("%d/%m/%Y")
694
                    )
695
                )
696
            return object_list(
697
                request,
698
                queryset,
699
                template_name='im/astakosgroup_list.html',
700
                extra_context=dict(
701
                    form=form,
702
                    is_search=True,
703
                    join_forms=join_forms
704
                )
705
            )
706
    return render_response(
707
        template='im/astakosgroup_list.html',
708
        form = form,
709
        context_instance=get_context(request)
710
    )
666 711

  
667 712
@signed_terms_required
668 713
@login_required
669 714
def group_join(request, group_id):
670
    return create_object(request,
671
                         model=Membership,
672
                         template_name='im/astakosgroup_list.html',
673
                         post_save_redirect = reverse('group_detail', kwargs=dict(group_id=group_id)))
715
    return create_object(
716
        request,
717
        model=Membership,
718
        template_name='im/astakosgroup_list.html',
719
        post_save_redirect = reverse(
720
            'group_detail',
721
            kwargs=dict(group_id=group_id)
722
        )
723
    )
674 724

  
675 725
@signed_terms_required
676 726
@login_required
677 727
def group_leave(request, group_id):
678 728
    try:
679
        m = Membership.objects.select_related().get(group__id=group_id, person=request.user)
729
        m = Membership.objects.select_related().get(
730
            group__id=group_id,
731
            person=request.user
732
        )
680 733
    except Membership.DoesNotExist:
681 734
        return HttpResponseBadRequest(_('Invalid membership.'))
682 735
    if request.user in m.group.owner.all():
683 736
        return HttpResponseForbidden(_('Owner can not leave the group.'))
684
    return delete_object(request,
685
                         model=Membership,
686
                         object_id = m.id,
687
                         template_name='im/astakosgroup_list.html',
688
                         post_delete_redirect = reverse('group_detail', kwargs=dict(group_id=group_id)))
737
    return delete_object(
738
        request,
739
        model=Membership,
740
        object_id = m.id,
741
        template_name='im/astakosgroup_list.html',
742
        post_delete_redirect = reverse(
743
            'group_detail',
744
            kwargs=dict(group_id=group_id)
745
        )
746
    )
689 747

  
690 748
def handle_membership():
691 749
    def decorator(func):
692 750
        @wraps(func)
693 751
        def wrapper(request, group_id, user_id):
694 752
            try:
695
                m = Membership.objects.select_related().get(group__id=group_id, person__id=user_id)
753
                m = Membership.objects.select_related().get(
754
                    group__id=group_id,
755
                    person__id=user_id
756
                )
696 757
            except Membership.DoesNotExist:
697 758
                return HttpResponseBadRequest(_('Invalid membership.'))
698 759
            else:
699 760
                if request.user not in m.group.owner.all():
700 761
                    return HttpResponseForbidden(_('User is not a group owner.'))
701 762
                func(request, m)
702
                return render_response(template='im/astakosgroup_detail.html',
703
                                       context_instance=get_context(request),
704
                                       object=m.group,
705
                                       quota=m.group.quota,
706
                                       more_policies=m.group.has_undefined_policies)
763
                return render_response(
764
                    template='im/astakosgroup_detail.html',
765
                    context_instance=get_context(request),
766
                    object=m.group,
767
                    quota=m.group.quota,
768
                    more_policies=m.group.has_undefined_policies
769
                )
707 770
        return wrapper
708 771
    return decorator
709 772

  
......
738 801
@signed_terms_required
739 802
@login_required
740 803
def resource_list(request):
741
    return render_response(template='im/astakosuserquota_list.html',
742
                           context_instance=get_context(request),
743
                           quota=request.user.quota)
804
    return render_response(
805
        template='im/astakosuserquota_list.html',
806
        context_instance=get_context(request),
807
        quota=request.user.quota
808
    )

Also available in: Unified diff