Revision c7c0ec58 snf-astakos-app/astakos/im/functions.py

b/snf-astakos-app/astakos/im/functions.py
54 54
from datetime import datetime
55 55
from functools import wraps
56 56

  
57
import astakos.im.settings as astakos_settings
57 58
from astakos.im.settings import (
58 59
    DEFAULT_CONTACT_EMAIL, SITENAME, BASEURL, LOGGING_LEVEL,
59 60
    VERIFICATION_EMAIL_SUBJECT, ACCOUNT_CREATION_SUBJECT,
......
67 68
from astakos.im.notifications import build_notification, NotificationError
68 69
from astakos.im.models import (
69 70
    AstakosUser, Invitation, ProjectMembership, ProjectApplication, Project,
71
    UserSetting,
70 72
    PendingMembershipError, get_resource_names, new_chain)
71 73
from astakos.im.project_notif import (
72 74
    membership_change_notify, membership_enroll_notify,
......
694 696
            m = _(astakos_messages.NOT_ALLOWED)
695 697
            raise PermissionDenied(m)
696 698

  
699
    reached, limit = reached_pending_application_limit(request_user.id, precursor)
700
    if reached:
701
        m = _(astakos_messages.REACHED_PENDING_APPLICATION_LIMIT) % limit
702
        raise PermissionDenied(m)
703

  
697 704
    application = ProjectApplication(**kw)
698 705

  
699 706
    if precursor is None:
......
808 815
            raise Http404
809 816
        else:
810 817
            return None, application
818

  
819

  
820
def get_user_setting(user_id, key):
821
    try:
822
        setting = UserSetting.objects.get(
823
            user=user_id, setting=key)
824
        return setting.value
825
    except UserSetting.DoesNotExist:
826
        return getattr(astakos_settings, key)
827

  
828

  
829
def set_user_setting(user_id, key, value):
830
    try:
831
        setting = UserSetting.objects.get_for_update(
832
            user=user_id, setting=key)
833
    except UserSetting.DoesNotExist:
834
        setting = UserSetting(user_id=user_id, setting=key)
835
    setting.value = value
836
    setting.save()
837

  
838

  
839
def unset_user_setting(user_id, key):
840
    UserSetting.objects.filter(user=user_id, setting=key).delete()
841

  
842

  
843
PENDING_APPLICATION_LIMIT_SETTING = 'PENDING_APPLICATION_LIMIT'
844

  
845
def get_pending_application_limit(user_id):
846
    key = PENDING_APPLICATION_LIMIT_SETTING
847
    return get_user_setting(user_id, key)
848

  
849

  
850
def set_pending_application_limit(user_id, value):
851
    key = PENDING_APPLICATION_LIMIT_SETTING
852
    return set_user_setting(user_id, key, value)
853

  
854

  
855
def unset_pending_application_limit(user_id):
856
    key = PENDING_APPLICATION_LIMIT_SETTING
857
    return unset_user_setting(user_id, key)
858

  
859

  
860
def _reached_pending_application_limit(user_id):
861
    limit = get_pending_application_limit(user_id)
862

  
863
    PENDING = ProjectApplication.PENDING
864
    pending = ProjectApplication.objects.filter(
865
        applicant__id=user_id, state=PENDING).count()
866

  
867
    return pending >= limit, limit
868

  
869

  
870
def reached_pending_application_limit(user_id, precursor=None):
871
    reached, limit = _reached_pending_application_limit(user_id)
872

  
873
    if precursor is None:
874
        return reached, limit
875

  
876
    chain = precursor.chain
877
    objs = ProjectApplication.objects
878
    q = objs.filter(chain=chain, state=ProjectApplication.PENDING)
879
    has_pending = q.exists()
880

  
881
    if not has_pending:
882
        return reached, limit
883

  
884
    return False, limit

Also available in: Unified diff