def hasPerm(user, form): """ Checks if this user qualifies to view this form """ if (not form.anonymous or form.perms!="") and not user.is_authenticated(): return False, "You need to be logged in to view this form." if form.perms == "": return True, "" else: perms_list = form.perms.strip(',').split(',') main_perm = perms_list[0] prog_id = "" sub_perms = None if len(perms_list)>1: prog_id = perms_list[1] if len(perms_list)>2: sub_perms = perms_list[2:] Qlist = [] Qlist.append(ESPUser.getAllOfType(main_perm)) # Check -> what to do with students? if sub_perms: if prog_id != "": prog = Program.objects.get(pk=int(prog_id)) all_Qs = prog.getLists(QObjects=True) for perm in sub_perms: Qlist.append(all_Qs[perm]['list']) if ESPUser.objects.filter(id=user.id).filter(*Qlist).exists(): return True, "" else: return False, "You are not permitted to view this form."
def hasPerm(user, form): """ Checks if this user qualifies to view this form """ if (not form.anonymous or form.perms != "") and not user.is_authenticated(): return False, "You need to be logged in to view this form." if form.perms == "": return True, "" else: perms_list = form.perms.strip(',').split(',') main_perm = perms_list[0] prog_id = "" sub_perms = None if len(perms_list) > 1: prog_id = perms_list[1] if len(perms_list) > 2: sub_perms = perms_list[2:] Qlist = [] Qlist.append(ESPUser.getAllOfType( main_perm)) # Check -> what to do with students? if sub_perms: if prog_id != "": prog = Program.objects.get(pk=int(prog_id)) all_Qs = prog.getLists(QObjects=True) for perm in sub_perms: Qlist.append(all_Qs[perm]['list']) if ESPUser.objects.filter(id=user.id).filter(*Qlist).exists(): return True, "" else: return False, "You are not permitted to view this form."
def prepare_context(self, program, target_path=None): context = {} context['program'] = program context['user_types'] = ESPUser.getTypes() category_lists = {} list_descriptions = program.getListDescriptions() # Add in program-specific lists for most common user types for user_type, list_func in zip(Program.USER_TYPES_WITH_LIST_FUNCS, Program.USER_TYPE_LIST_FUNCS): raw_lists = getattr(program, list_func)(True) category_lists[user_type] = [{ 'name': key, 'list': raw_lists[key], 'description': list_descriptions[key] } for key in raw_lists] for item in category_lists[user_type]: if item['name'] in UserSearchController.preferred_lists: item['preferred'] = True # Add in global lists for each user type for user_type in ESPUser.getTypes(): key = user_type.lower() + 's' if user_type not in category_lists: category_lists[user_type] = [] category_lists[user_type].insert( 0, { 'name': 'all_%s' % user_type, 'list': ESPUser.getAllOfType(user_type), 'description': 'All %s in the database' % key, 'preferred': True, 'all_flag': True }) # Add in mailing list accounts category_lists['emaillist'] = [{ 'name': 'all_emaillist', 'list': Q(password='******'), 'description': 'Everyone signed up for the mailing list', 'preferred': True }] context['lists'] = category_lists context['all_list_names'] = [] for category in category_lists: for item in category_lists[category]: context['all_list_names'].append(item['name']) if target_path is None: target_path = '/manage/%s/commpanel' % program.getUrlBase() context['action_path'] = target_path context['groups'] = Group.objects.all() return context
def prepare_context(self, program, target_path=None): context = {} context['program'] = program category_lists = {} list_descriptions = program.getListDescriptions() # Add in program-specific lists for most common user types for user_type in UserSearchController.user_types: raw_lists = getattr(program, user_type)(True) category_lists[user_type] = [{ 'name': key, 'list': raw_lists[key], 'description': list_descriptions[key] } for key in raw_lists] for item in category_lists[user_type]: if item['name'] in UserSearchController.preferred_lists: item['preferred'] = True # Add in global lists for each user type for cat_pair in UserSearchController.global_categories: key = cat_pair[0].lower() + 's' if cat_pair[1] not in category_lists: category_lists[cat_pair[1]] = [] category_lists[cat_pair[1]].insert( 0, { 'name': 'all_%s' % key, 'list': ESPUser.getAllOfType(cat_pair[0]), 'description': 'All %s in the database' % key, 'preferred': True, 'all_flag': True }) # Add in mailing list accounts category_lists['emaillist'] = [{ 'name': 'all_emaillist', 'list': Q(password='******'), 'description': 'Everyone signed up for the mailing list', 'preferred': True }] context['lists'] = category_lists context['all_list_names'] = [] for category in category_lists: for item in category_lists[category]: context['all_list_names'].append(item['name']) if target_path is None: target_path = '/manage/%s/commpanel' % program.getUrlBase() context['action_path'] = target_path return context
def schedule_students(self, request, tl, one, two, module, extra, prog): """ Redirect to student registration, having morphed into the desired student. """ user, found = search_for_user(request, ESPUser.getAllOfType('Student', False)) if not found: return user request.user.switch_to_user(request, user, self.getCoreURL(tl), 'OnSite Registration!', True) return HttpResponseRedirect('/learn/%s/studentreg' % self.program.getUrlBase())
def query_from_criteria(self, user_type, criteria): """ Get the "base list" consisting of all the users of a specific type. """ if user_type.lower() == 'any': Q_base = Q() else: if user_type not in ESPUser.getTypes(): raise ESPUser(), 'user_type must be one of ' + str( ESPUser.getTypes()) Q_base = ESPUser.getAllOfType(user_type, True) Q_include = Q() Q_exclude = Q() """ Apply the specified criteria to filter the list of users. """ if criteria.has_key('userid') and len(criteria['userid'].strip()) > 0: ## Select users based on their IDs only userid = [] for digit in criteria['userid'].split(','): try: userid.append(int(digit)) except: raise ESPError( False ), 'User id invalid, please enter a number or comma-separated list of numbers.' if criteria.has_key('userid__not'): Q_exclude &= Q(id__in=userid) else: Q_include &= Q(id__in=userid) self.updated = True else: ## Select users based on all other criteria that was entered for field in ['username', 'last_name', 'first_name', 'email']: if criteria.has_key(field) and len( criteria[field].strip()) > 0: # Check that it's a valid regular expression try: rc = re.compile(criteria[field]) except: raise ESPError( False ), 'Invalid search expression, please check your syntax: %s' % criteria[ field] filter_dict = {'%s__iregex' % field: criteria[field]} if criteria.has_key('%s__not' % field): Q_exclude &= Q(**filter_dict) else: Q_include &= Q(**filter_dict) self.updated = True if criteria.has_key('zipcode') and criteria.has_key('zipdistance') and \ len(criteria['zipcode'].strip()) > 0 and len(criteria['zipdistance'].strip()) > 0: try: zipc = ZipCode.objects.get(zip_code=criteria['zipcode']) except: raise ESPError( False ), 'Zip code not found. This may be because you didn\'t enter a valid US zipcode. Tried: "%s"' % criteria[ 'zipcode'] zipcodes = zipc.close_zipcodes(criteria['zipdistance']) # Excludes zipcodes within a certain radius, giving an annulus; can fail to exclude people who used to live outside the radius. # This may have something to do with the Q_include line below taking more than just the most recent profile. -ageng, 2008-01-15 if criteria.has_key('zipdistance_exclude') and len( criteria['zipdistance_exclude'].strip()) > 0: zipcodes_exclude = zipc.close_zipcodes( criteria['zipdistance_exclude']) zipcodes = [ zipcode for zipcode in zipcodes if zipcode not in zipcodes_exclude ] if len(zipcodes) > 0: Q_include &= Q( registrationprofile__contact_user__address_zip__in= zipcodes, registrationprofile__most_recent_profile=True) self.updated = True if criteria.has_key('states') and len( criteria['states'].strip()) > 0: state_codes = criteria['states'].strip().upper().split(',') if criteria.has_key('states__not'): Q_exclude &= Q( registrationprofile__contact_user__address_state__in= state_codes, registrationprofile__most_recent_profile=True) else: Q_include &= Q( registrationprofile__contact_user__address_state__in= state_codes, registrationprofile__most_recent_profile=True) self.updated = True if criteria.has_key('grade_min'): yog = ESPUser.YOGFromGrade(criteria['grade_min']) if yog != 0: Q_include &= Q( registrationprofile__student_info__graduation_year__lte =yog, registrationprofile__most_recent_profile=True) self.updated = True if criteria.has_key('grade_max'): yog = ESPUser.YOGFromGrade(criteria['grade_max']) if yog != 0: Q_include &= Q( registrationprofile__student_info__graduation_year__gte =yog, registrationprofile__most_recent_profile=True) self.updated = True if criteria.has_key('school'): school = criteria['school'] if school: Q_include &= ( Q(studentinfo__school__icontains=school) | Q(studentinfo__k12school__name__icontains=school)) self.updated = True # Filter by graduation years if specifically looking for teachers. possible_gradyears = range(1920, 2020) if criteria.has_key('gradyear_min') and len( criteria['gradyear_min'].strip()) > 0: try: gradyear_min = int(criteria['gradyear_min']) except: raise ESPError( False ), 'Please enter a 4-digit integer for graduation year limits.' possible_gradyears = filter(lambda x: x >= gradyear_min, possible_gradyears) if criteria.has_key('gradyear_max') and len( criteria['gradyear_min'].strip()) > 0: try: gradyear_max = int(criteria['gradyear_max']) except: raise ESPError( False ), 'Please enter a 4-digit integer for graduation year limits.' possible_gradyears = filter(lambda x: x <= gradyear_max, possible_gradyears) if criteria.get('gradyear_min', None) or criteria.get( 'gradyear_max', None): Q_include &= Q( registrationprofile__teacher_info__graduation_year__in=map( str, possible_gradyears), registrationprofile__most_recent_profile=True) self.updated = True return Q_base & (Q_include & ~Q_exclude)
def query_from_criteria(self, user_type, criteria): """ Get the "base list" consisting of all the users of a specific type. """ if user_type.lower() == 'any': Q_base = Q() else: if user_type not in ESPUser.getTypes(): raise ESPError('user_type must be one of '+str(ESPUser.getTypes())) Q_base = ESPUser.getAllOfType(user_type, True) Q_include = Q() Q_exclude = Q() """ Apply the specified criteria to filter the list of users. """ if criteria.get('userid', '').strip(): ## Select users based on their IDs only userid = [] for digit in criteria['userid'].split(','): try: userid.append(int(digit)) except: raise ESPError('User id invalid, please enter a number or comma-separated list of numbers.', log=False) if 'userid__not' in criteria: Q_exclude &= Q(id__in = userid) else: Q_include &= Q(id__in = userid) self.updated = True else: ## Select users based on all other criteria that was entered for field in ['username','last_name','first_name', 'email']: if criteria.get(field, '').strip(): # Check that it's a valid regular expression try: rc = re.compile(criteria[field]) except: raise ESPError('Invalid search expression, please check your syntax: %s' % criteria[field], log=False) filter_dict = {'%s__iregex' % field: criteria[field]} if '%s__not' % field in criteria: Q_exclude &= Q(**filter_dict) else: Q_include &= Q(**filter_dict) self.updated = True if 'zipcode' in criteria and 'zipdistance' in criteria and \ len(criteria['zipcode'].strip()) > 0 and len(criteria['zipdistance'].strip()) > 0: try: zipc = ZipCode.objects.get(zip_code = criteria['zipcode']) except: raise ESPError('Zip code not found. This may be because you didn\'t enter a valid US zipcode. Tried: "%s"' % criteria['zipcode'], log=False) zipcodes = zipc.close_zipcodes(criteria['zipdistance']) # Excludes zipcodes within a certain radius, giving an annulus; can fail to exclude people who used to live outside the radius. # This may have something to do with the Q_include line below taking more than just the most recent profile. -ageng, 2008-01-15 if criteria.get('zipdistance_exclude', '').strip(): zipcodes_exclude = zipc.close_zipcodes(criteria['zipdistance_exclude']) zipcodes = [ zipcode for zipcode in zipcodes if zipcode not in zipcodes_exclude ] if len(zipcodes) > 0: Q_include &= Q(registrationprofile__contact_user__address_zip__in = zipcodes, registrationprofile__most_recent_profile=True) self.updated = True if criteria.get('states', '').strip(): state_codes = criteria['states'].strip().upper().split(',') if 'states__not' in criteria: Q_exclude &= Q(registrationprofile__contact_user__address_state__in = state_codes, registrationprofile__most_recent_profile=True) else: Q_include &= Q(registrationprofile__contact_user__address_state__in = state_codes, registrationprofile__most_recent_profile=True) self.updated = True if 'grade_min' in criteria: yog = ESPUser.YOGFromGrade(criteria['grade_min']) if yog != 0: Q_include &= Q(registrationprofile__student_info__graduation_year__lte = yog, registrationprofile__most_recent_profile=True) self.updated = True if 'grade_max' in criteria: yog = ESPUser.YOGFromGrade(criteria['grade_max']) if yog != 0: Q_include &= Q(registrationprofile__student_info__graduation_year__gte = yog, registrationprofile__most_recent_profile=True) self.updated = True if 'school' in criteria: school = criteria['school'] if school: Q_include &= (Q(studentinfo__school__icontains=school) | Q(studentinfo__k12school__name__icontains=school)) self.updated = True # Filter by graduation years if specifically looking for teachers. possible_gradyears = range(1920, 2020) if criteria.get('gradyear_min', '').strip(): try: gradyear_min = int(criteria['gradyear_min']) except: raise ESPError('Please enter a 4-digit integer for graduation year limits.', log=False) possible_gradyears = filter(lambda x: x >= gradyear_min, possible_gradyears) if criteria.get('gradyear_max', '').strip(): try: gradyear_max = int(criteria['gradyear_max']) except: raise ESPError('Please enter a 4-digit integer for graduation year limits.', log=False) possible_gradyears = filter(lambda x: x <= gradyear_max, possible_gradyears) if criteria.get('gradyear_min', None) or criteria.get('gradyear_max', None): Q_include &= Q(registrationprofile__teacher_info__graduation_year__in = map(str, possible_gradyears), registrationprofile__most_recent_profile=True) self.updated = True return Q_base & (Q_include & ~Q_exclude)
def prepare_context(self, program, target_path=None): context = {} context['program'] = program context['user_types'] = ESPUser.getTypes() category_lists = {} list_descriptions = program.getListDescriptions() # Add in program-specific lists for most common user types for user_type, list_func in zip(Program.USER_TYPES_WITH_LIST_FUNCS, Program.USER_TYPE_LIST_FUNCS): raw_lists = getattr(program, list_func)(True) category_lists[user_type] = [{'name': key, 'list': raw_lists[key], 'description': list_descriptions[key]} for key in raw_lists] for item in category_lists[user_type]: if item['name'] in UserSearchController.preferred_lists: item['preferred'] = True # Add in global lists for each user type for user_type in ESPUser.getTypes(): key = user_type.lower() + 's' if user_type not in category_lists: category_lists[user_type] = [] category_lists[user_type].insert(0, {'name': 'all_%s' % user_type, 'list': ESPUser.getAllOfType(user_type), 'description': 'All %s in the database' % key, 'preferred': True, 'all_flag': True}) # Add in mailing list accounts category_lists['emaillist'] = [{'name': 'all_emaillist', 'list': Q(password = '******'), 'description': 'Everyone signed up for the mailing list', 'preferred': True}] context['lists'] = category_lists context['all_list_names'] = [] for category in category_lists: for item in category_lists[category]: context['all_list_names'].append(item['name']) if target_path is None: target_path = '/manage/%s/commpanel' % program.getUrlBase() context['action_path'] = target_path return context