def test_get_num_queries(self): with self.assertNumQueries(1): setting_value = Setting.get('TEST_SETTING_STRING') with self.assertNumQueries(0): setting_value = Setting.get('TEST_SETTING_STRING') with self.assertNumQueries(0): setting_value = Setting.get('TEST_SETTING_STRING')
def test_cache_updated_on_model_delete(self): setting_obj, setting_created = Setting.objects.get_or_create( name='TEST_GETTER_SETTER', defaults={ 'value_type':Setting.TYPE_STRING }) setting_obj.value = 'string value' setting_obj.save() self.assertEqual(Setting.get('TEST_GETTER_SETTER'), 'string value') Setting.objects.filter(pk=setting_obj.pk).delete() self.assertEqual(Setting.get('TEST_GETTER_SETTER'), None)
def test_cache_updated_on_model_name_changed(self): setting_obj, setting_created = Setting.objects.get_or_create( name='TEST_GETTER_SETTER', defaults={ 'value_type':Setting.TYPE_STRING }) setting_obj.value = 'string value' setting_obj.save() self.assertEqual(Setting.get('TEST_GETTER_SETTER'), 'string value') setting_obj.name = 'TEST_GETTER_SETTER_RENAMED' setting_obj.save() self.assertEqual(Setting.get('TEST_GETTER_SETTER'), None) self.assertEqual(Setting.get('TEST_GETTER_SETTER_RENAMED'), 'string value')
def get_context_data(self, *args, **kwargs): phase = Setting.get('PHASE_PARRAINAGE') show_data = show_sensible_data(self.request.user) context = {} context['list_family'] = [{ 'name': f.name if show_data else f'Famille n°{f.id}', 'url': f.get_absolute_url(), 'id': f.id, } for f in Family.objects.all()] memberships = MembershipFamily.objects.all().select_related( 'student__user', 'group') if show_data: context['list_2A'] = [{ 'name': m.student.alphabetical_name, 'family': m.group.name, 'url': m.group.get_absolute_url(), } for m in memberships.filter(role='2A+')] if phase >= 3: context['list_1A'] = [{ 'name': m.student.alphabetical_name, 'family': m.group.name if show_data and phase > 3 else f'Famille n°{m.group.id}', 'url': m.group.get_absolute_url(), } for m in memberships.filter(role='1A', group__isnull=False)] context['ariane'] = [{ 'target': reverse('family:home'), 'label': 'Parrainage' }, { 'target': '#', 'label': 'Liste' }] return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['housing'] = self.object.housing context['roommates_list'] = Roommates.objects.filter( housing=context['housing']).exclude( pk=self.object.pk).order_by('-begin_date') context['colocathlon'] = Setting.get('PHASE_COLOCATHLON') context[ 'nb_participants'] = self.object.colocathlon_participants.count() return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) families = [] for f in Family.objects.filter(year=scholar_year()): members_2A = f.memberships.filter(role='2A+') members_2A_plus = f.non_subscribed_members.split(',') if f.non_subscribed_members else [] members_1A = f.memberships.filter(role='1A') families.append({'A1':members_1A, 'A2':members_2A, 'A2plus':members_2A_plus, 'family':f}) context['families'] = families context['phase'] = Setting.get('PHASE_PARRAINAGE') return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) data_years = StandardizedFile.objects.distinct('reporting_year')\ .order_by('-reporting_year')\ .values_list('reporting_year', flat=True) context['data_years'] = list(data_years) context['show_donate_banner'] = Setting.get( 'PAYROLL_SHOW_DONATE_BANNER', False) donate_text = Setting.get('DONATE_MESSAGE', '') context['donate_message'] = bleach.clean(donate_text, tags=['p', 'strong', 'a'], attributes={ 'a': ['href', 'target'], '*': ['class', 'style'] }) return context
def show_sensible_data(user: User, membership: MembershipFamily = None) -> bool: """Décide si on doit montrer les données sensibles : on les montre pour les 2A+ et pour les 1A après la chasse aux parrains (phase > 3).""" if not membership: membership = get_membership(user) phase = Setting.get('PHASE_PARRAINAGE') if membership: first_year = is_1A(user, membership) return (not first_year) or (phase >= 4) else: return phase >= 4
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) families = [] try: member1A_list, member2A_list, family_list = self.resolve() for f in family_list: members_2A = [m for m in member2A_list if m['family']==f['family']] members_1A = [m for m in member1A_list if m['family']==f['family']] if members_2A or members_1A: families.append({'A1':members_1A, 'A2':members_2A, 'family':f['family']}) except Exception as e: messages.error(self.request, e) context['families'] = families context['phase'] = Setting.get('PHASE_PARRAINAGE') return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['phase'] = Setting.get('PHASE_PARRAINAGE') ## FAMILIES # family - general families = Family.objects.filter(year=scholar_year()) context['nb_families'] = len(families) # family - bad number of members bad_nb_families = [f for f in families if f.count_members2A()<3] context['bad_nb_families'] = bad_nb_families context['nb_bad_nb_families'] = len(bad_nb_families) # family - not finish to complete all answers non_completed_families = [f for f in families if not f.form_complete()] context['nb_non_complete_families'] = len(non_completed_families) if context['nb_non_complete_families'] < 10: context['non_complete_families'] = non_completed_families ## MEMBERS members = MembershipFamily.objects.filter(Q(group__isnull=True) | Q(group__year=scholar_year())).order_by('group__name') ## MEMBERS 1A members1A = members.filter(role='1A') context['nb_1A'] = members1A.count() context['nb_itii'] = members1A.filter(student__faculty='Iti').count() context['nb_1A_unplaced'] = members1A.filter(group__isnull=True).count() context['nb_1A_placed'] = members1A.filter(group__isnull=False).count() # 1A pas encore dans une famille if context['nb_1A_unplaced'] < 10: context['unplaced_1A'] = members1A.filter(group__isnull=True) # 1A n'ayant pas fini le questionnaire non_complete_1A = [m for m in members1A if not m.form_complete()] context['nb_non_complete_1A'] = len(non_complete_1A) context['non_complete_1A'] = non_complete_1A ## MEMBERS 2A+ members2A = members.filter(role='2A+') context['nb_2A'] = members2A.count() # 2A n'ayant pas fini leur questionnaire non_complete_2A = [m for m in members2A if not m.form_complete()] context['non_complete_2A'] = non_complete_2A context['nb_non_complete_2A'] = len(non_complete_2A) # membres non inscrits non_subscribed_2A = [] for f in families: if f.non_subscribed_members: for m in f.non_subscribed_members.split(','): if m: non_subscribed_2A.append((m, f)) context['non_subscribed_2A'] = non_subscribed_2A context['nb_non_subscribed_2A'] = len(non_subscribed_2A) return context
def get_context_data(self, *args, **kwargs): context = super().get_context_data(*args, **kwargs) family = self.get_object() context['show_name'] = show_sensible_data(self.request.user) context['is_admin'] = family.is_admin(self.request.user) context['parrains'] = family.memberships.filter(role='2A+') context['filleuls'] = family.memberships.filter(role='1A') context['phase'] = Setting.get('PHASE_PARRAINAGE') context['ariane'] = [{ 'target': reverse('family:home'), 'label': 'Parrainage' }, { 'target': reverse('family:family-list'), 'label': 'Liste' }, { 'target': '#', 'label': family.name }] return context
def get_context_data(self, **kwargs): # by default all functions call data for the current year only membership = get_membership(self.request.user) context = {} context['phase'] = Setting.get('PHASE_PARRAINAGE') context['is_2Aplus'] = not is_1A(self.request.user, membership) context['show_sensible_data'] = show_sensible_data( self.request.user, membership) context['is_itii'] = self.request.user.student.faculty == 'Iti' context['membership'] = membership if membership: context['form_perso_complete'] = membership.form_complete() family = membership.group context['family'] = family if family: context['form_family_complete'] = family.form_complete() context['1A_members'] = family.memberships.filter(role='1A') context['ariane'] = [{'target': '#', 'label': 'Parrainage'}] return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) data_years = StandardizedFile.objects.distinct('reporting_year')\ .order_by('-reporting_year')\ .values_list('reporting_year', flat=True) context['data_years'] = list(data_years) context['show_donate_banner'] = Setting.get('PAYROLL_SHOW_DONATE_BANNER', False) try: state_officers_slug = Department.objects.get(name='State Officers', parent__name='Illinois').slug except Department.DoesNotExist: state_officers_slug = None context['state_officers_slug'] = state_officers_slug return context
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: context = super().get_context_data(**kwargs) context['MAPBOX_API_KEY'] = settings.MAPBOX_API_KEY PHASE_COLOCATHLON = Setting.get('PHASE_COLOCATHLON') context['colocathlon'] = PHASE_COLOCATHLON if PHASE_COLOCATHLON == 2: roommate = Roommates.objects.filter( colocathlon_participants=self.request.user.student) if roommate.exists(): context['CURRENT_COLOC'] = roommate.first().name context['CURRENT_COLOC_URL'] = roommate.first( ).get_absolute_url() context['ariane'] = [{ 'target': '#', 'label': 'Colocs' }, { 'target': '#', 'label': 'Carte' }] return context
def test_get_with_valid_name(self): setting_value = Setting.get('TEST_SETTING_STRING') self.assertEqual(setting_value, '')
def get_setting(context, name, default=""): return Setting.get(name, default)
def setUp(self): Setting.objects.bulk_create([ Setting(name='TEST_SETTING_BOOL', value_type=Setting.TYPE_BOOL), # Setting(name='TEST_SETTING_COLOR', value_type=Setting.TYPE_COLOR='color'), Setting(name='TEST_SETTING_DATE', value_type=Setting.TYPE_DATE), Setting(name='TEST_SETTING_DATETIME', value_type=Setting.TYPE_DATETIME), Setting(name='TEST_SETTING_DURATION', value_type=Setting.TYPE_DURATION), Setting(name='TEST_SETTING_DECIMAL', value_type=Setting.TYPE_DECIMAL), Setting(name='TEST_SETTING_EMAIL', value_type=Setting.TYPE_EMAIL), Setting(name='TEST_SETTING_FILE', value_type=Setting.TYPE_FILE), Setting(name='TEST_SETTING_FLOAT', value_type=Setting.TYPE_FLOAT), # Setting(name='TEST_SETTING_HTML', value_type=Setting.TYPE_HTML), Setting(name='TEST_SETTING_IMAGE', value_type=Setting.TYPE_IMAGE), Setting(name='TEST_SETTING_INT', value_type=Setting.TYPE_INT), # Setting(name='TEST_SETTING_JSON', value_type=Setting.TYPE_JSON), Setting(name='TEST_SETTING_STRING', value_type=Setting.TYPE_STRING), Setting(name='TEST_SETTING_TEXT', value_type=Setting.TYPE_TEXT), Setting(name='TEST_SETTING_TIME', value_type=Setting.TYPE_TIME), # Setting(name='TEST_SETTING_UUID', value_type=Setting.TYPE_UUID), Setting(name='TEST_SETTING_URL', value_type=Setting.TYPE_URL), ])
def test_get_with_invalid_name_and_default_value(self): setting_value = Setting.get('TEST_SETTING_STRING_INVALID', default='default string value') self.assertEqual(setting_value, 'default string value')
def get_price(self): cnc_net_cost = 0 print_net_cost = 0 surface_finish_cost = 0 displayed_price = 0 displayed_price_cnc = 0 displayed_price_print = 0 displayed_manufacturing_days = 0 elements = Element.objects.filter(order=self) DRAW_COEFFICIENT = Setting.get('EXTRA_DRAW_COEFFICIENT', default='django-extra-settings') TOLERANCE_COEFFICIENT = Setting.get('EXTRA_TOLERANCE_COEFFICIENT', default='django-extra-settings') THREADS_COEFFICIENT = Setting.get('EXTRA_THREADS_COEFFICIENT', default='django-extra-settings') URGENT_FACTOR = Setting.get('EXTRA_URGENT_FACTOR', default='django-extra-settings') RISKY_FACTOR = Setting.get('EXTRA_RISKY_FACTOR', default='django-extra-settings') ADDITIONAL_COST = Setting.get('EXTRA_ADDITIONAL_COST', default='django-extra-settings') WORKPIECE_PRODUCTION_COST = Setting.get( 'EXTRA_WORKPIECE_PRODUCTION_COST', default='django-extra-settings') WORKPIECE_VOLUME = Setting.get('EXTRA_WORKPIECE_VOLUME', default='django-extra-settings') COMMISSION = Setting.get('EXTRA_COMMISSION', default='django-extra-settings') COMMISSION_ADDITIONAL = Setting.get('EXTRA_COMMISSION_ADDITIONAL', default='django-extra-settings') TIME_FOR_QA = Setting.get('EXTRA_TIME_FOR_QA', default='django-extra-settings') MACHINING_TIME = Setting.get('EXTRA_MACHINING_TIME', default='django-extra-settings') for element in elements: # func_cnc_quantity_factor(((var_material_cost * 1000 * var_material_density * workpiece_volume + workpiece_production_cost + (startup_time + production_time / 60 * var_material_difficulty_factor * draw_coefficient * tolerance_coefficient * threads_coefficient) * var_working_cost + postprocessing_time * var_postprocessing_cost) * urgent_factor * risky_factor + additional_cost) * quantity) cnc_net_cost += ( element.material.material_cost * 1000 * element.material.material_density * WORKPIECE_PRODUCTION_COST + WORKPIECE_VOLUME + (element.technology.starup_time + self.time / 60 * element.material.material_difficulty_factor * DRAW_COEFFICIENT * TOLERANCE_COEFFICIENT * THREADS_COEFFICIENT) * element.technology.working_cost * element.technology.postprocessing_time * element.technology.postprocessing_cost ) * URGENT_FACTOR * RISKY_FACTOR * ADDITIONAL_COST # func_3dprint_quantity_factor(((var_material_cost * 1000 * var_material_density * detail_volume + support_volume + (startup_time + production_time / 60 * var_material_difficulty_factor * draw_coefficient * tolerance_coefficient * threads_coefficient) * var_working_cost + postprocessing_time * var_postprocessing_cost) * urgent_factor * risky_factor + additional_cost) * quantity) print_net_cost += ( element.material.material_cost * 1000 * element.material.material_density * self.part_volume + WORKPIECE_VOLUME + (element.technology.starup_time + self.time / 60 * element.material.material_difficulty_factor * DRAW_COEFFICIENT * TOLERANCE_COEFFICIENT * THREADS_COEFFICIENT) * element.technology.working_cost * element.technology.postprocessing_time * element.technology.postprocessing_cost ) * URGENT_FACTOR * RISKY_FACTOR * ADDITIONAL_COST # surface_area / 1000000 * var_material_coating_cost + (coating difficulty factor * surface_area / coating speed) * coating cost surface_finish_cost += self.target_surface_area / 1000000 * element.material.material_cost + \ (element.surface.coating_difficulty_factor * self.target_surface_area / element.surface.surface_finish) * element.surface.coating_cost # ((Net cost + Surface finish time) × Commission + Additional commission) × (1 + 0.15) displayed_price += COMMISSION + COMMISSION_ADDITIONAL * 1.15 # (RoundUpDays(technologist_time_factor*production_time + production_time + time_for_qa) + surface_time) * 1.5 displayed_manufacturing_days += ( (element.technology.time_factor + TIME_FOR_QA + MACHINING_TIME) + element.surface.coating_time) * 1.5 displayed_price_cnc += cnc_net_cost * displayed_price displayed_price_print += print_net_cost * displayed_price return { 'cnc_net_cost': cnc_net_cost, 'print_net_cost': print_net_cost, 'surface_finish_cost': surface_finish_cost, 'displayed_price_cnc': displayed_price_cnc, 'displayed_price_print': displayed_price_print }
def test_get_with_invalid_name(self): setting_value = Setting.get('TEST_SETTING_STRING_INVALID') self.assertEqual(setting_value, None)
def test_modeladmin_save(self): ma = SettingAdmin(model=Setting, admin_site=AdminSite()) ma.save_model(obj=Setting(), request=None, form=None, change=None)