def post(self,request,*args,**kwargs): if request.user.is_superuser: gym_id = self.kwargs['gym_id'] form = RouteSetCreateForm(gym_id,request) context = form.get_context() if form.is_valid(): grade = form.cleaned['grade'] grade_sub = form.cleaned['grade_sub'] number = form.cleaned['number'] zone = form.cleaned['zone'][0] up_date = form.cleaned['up_date'] down_date = form.cleaned['down_date'] service_rs.create_route_set(gym_id, conf.Grade.get_value_from_name_list(grade), conf.GradeSub.get_value_from_name_list(grade_sub), up_date,down_date,zone,number) logger.info(f'{request.user} created new route set for gym {gym_id} in zone {zone}') context['message_success'] = ['Successfully created new route set.'] else: logger.warning(f'{request.user} failed to create new route set for gym {gym_id}') return render(request, self.template_name, context) else: return HttpResponseForbidden()
def test_create_route_set_invalid_gym_id(self): sampledata.create_auth_user() sampledata.add_gym() service_rs.create_route_set(101, self.grade, self.grade_sub, self.up_date, self.down_date, zone=self.zone) self.assertEqual(models.RouteSet.objects.filter(id=1).exists(), False)
def test_create_route_set_grade(self): sampledata.create_auth_user() sampledata.add_gym() service_rs.create_route_set(self.gym_id, self.grade[0], self.grade_sub, self.up_date, self.down_date, zone=self.zone) rs = models.RouteSet.objects.get(id=1) r_all = models.Route.objects.all() self.assertEqual(r_all[0].grade, self.grade[0]) self.assertEqual(r_all[1].grade, self.grade[0])
def process_route_set_form(form, gym_id): grade = int(form.cleaned_data['grade']) up_date = form.cleaned_data['up_date'] down_date = form.cleaned_data['down_date'] grade_sub = [] number = [] for ind, field in enumerate(form.fields): if 'grade_sub' in field: grade_sub_temp = int(form.cleaned_data[field]) if grade_sub_temp != 0: grade_sub.append(grade_sub_temp) number.append(ind) service_rs.create_route_set( gym_id, grade, grade_sub, up_date, down_date=down_date)
def add_route_set(self, gym_id, colour, grade_list, up_date, down_date=None): grade = self.DataMap.grade(colour) up_date = datetime.strptime(up_date, "%d/%m/%Y").date() if down_date: down_date = datetime.strptime(down_date, "%d/%m/%Y").date() grade_sub_list = [ self.DataMap.grade_sub(grade_sub_str) for grade_sub_str in grade_list ] service_rs.create_route_set(gym_id, grade, grade_sub_list, up_date, down_date=down_date)