from django.utils.text import slugify

from guardian.shortcuts import get_perms


def build_org_group_name(org_name, group):
    '''Returns org group name in the form of <ORGNAME>_ORG_<GROUP>'''
    return f'{slugify({org_name})}_ORG_{group}'.upper()

    
def build_study_group_name(org_name, study_name, study_pk, group):
    '''Returns org group name in the form of <ORGNAME>_ORG_<GROUP>'''
    return f'{slugify({org_name})}_{slugify({study_name})}_{study_pk}_STUDY_{group}'.upper()

status_tooltip_text = {
    'created': 'Study has not been submitted for approval',
    'active': 'Study is collecting data',
    'submitted': 'Study is awaiting approval',
    'draft': 'Study has not been submitted for approval',
    'approved': 'Study is approved but not started',
    'rejected': 'Study has been rejected. Please edit before resubmitting.',
    'retracted': 'Study has been withdrawn',
    'paused': 'Study is not collecting data',
    'deactivated': 'Study is not collecting data',
    'archived': 'Study has been archived and removed from search.'
}


def get_permitted_triggers(view_instance, triggers):
    ''' Get permitted triggers for study state '''
    permitted_triggers = []
    organization_permissions = get_perms(view_instance.request.user, view_instance.object.organization)

    admin_triggers = ['reject', 'approve']

    for trigger in triggers:
        # remove autogenerated triggers
        if trigger.startswith('to_'):
            continue
        # remove triggers that people don't have permission to
        if not view_instance.request.user.is_superuser or (trigger in admin_triggers and 'is_admin' not in organization_permissions):
            continue

        permitted_triggers.append(trigger)

    return permitted_triggers


def update_trigger(view_instance):
    ''' Transition to next state in study workflow '''
    trigger = view_instance.request.POST.get('trigger')
    object = view_instance.get_object()
    if trigger:
        if hasattr(object, trigger):
            # transition through workflow state
            getattr(object, trigger)(user=view_instance.request.user)
    if 'comments-text' in view_instance.request.POST.keys():
        object.comments = view_instance.request.POST['comments-text']
        object.save()
    return object

@@ -245,20 +245,16 @@
 pper()%0A%0A
-    
 %0Adef bui
@@ -1191,67 +1191,163 @@
     
-organization_permissions = get_perms(view_instance.request.
+user = view_instance.request.user%0A    study = view_instance.object%0A    organization_permissions = get_perms(user, study)%0A    study_permissions = get_perms(
 user
@@ -1372,21 +1372,9 @@
 ject
-.organization
+ 
 )%0A%0A 
@@ -1399,29 +1399,28 @@
 = %5B'
-reject', 'approve
+approve', 'reject
 '%5D%0A
-%0A
     
@@ -1543,16 +1543,17 @@
 ontinue%0A
+%0A
         
@@ -1558,46 +1558,123 @@
   # 
-remove triggers that people don't have
+Trigger valid if 1) superuser 2) trigger is admin trigger and user is org admin 3) trigger is found in user's study
  per
@@ -1680,19 +1680,17 @@
 rmission
- to
+s
 %0A       
@@ -1701,112 +1701,247 @@
 not 
-view_instance.request.user.is_superuser or (trigger in admin_triggers and 'is_admin' not in organization
+user.is_superuser:%0A            if trigger in admin_triggers:%0A                 if not (user.organization == study.organization and user.is_org_admin):%0A                     continue%0A            elif ('can_' + trigger + '_study') not in study
 _per
@@ -1948,19 +1948,22 @@
 missions
-)
 :%0A
+    
         

Improve get_permitted_triggers so studies can be approved/rejected in interface.