def test_count_number_of_trainings(self): training = Training() start = datetime.now(timezone.utc) - timedelta(seconds=405) training.save() training.start_training_date = start training.save() number, rest = count_number_of_trainings(training) self.assertEqual(number, 20) self.assertEqual(rest, 5)
def test_delete_training(self): training = Training() training.character = self.hero training.save() self.assertEqual( Training.objects.filter(character=self.hero).count(), 1) self.assertTrue(delete_training(self.hero)) self.assertEqual( Training.objects.filter(character=self.hero).count(), 0)
def test_add_training_points_to_character(self): training = Training() training.character = self.hero training.save() training.start_training_date = datetime.now( timezone.utc) - timedelta(seconds=885) training.save() self.hero.busy = True set_is_ending_training(training) add_training_points_to_character(self.hero) self.assertEqual(self.hero.growth_points, 45)
def test_training_creation(self): hero = Character.objects.create(name='jan', level=10, health_points=10, strength=3) training = Training() training.character = hero training.save() self.assertTrue(isinstance(training, Training)) self.assertEqual(training.__str__(), hero.name) self.assertIsNotNone(training.start_training_date) self.assertEqual(training.end_training_date, None) self.assertEqual(training.number_completed_training, 0) self.assertEqual(training.is_ending, False)
def create(request): """ --- parameters: - name: place_point type: string request_serializer: TrainingCreateSerializer """ sdata = get_validated_serializer(request=request, serializer=TrainingCreateSerializer).validated_data user = get_user_from_validated_data(sdata) training = Training(learner=user,status=S_WAITS_FOR_COUCH, **sdata) training.save() return Response("", status=HTTP_OK)
def training_form(request, pk=None): if pk: item = get_object_or_404(Training, pk=pk) scenario = 'Update' else: item = Training() scenario = 'Add' if request.POST: form = TrainingForm(data=request.POST, instance=item) if form.is_valid(): item = form.save(commit=False) if item.starts == '': item.starts = None if item.ends == '': item.ends = None item.save() form.save_m2m() item.participants.clear() participants = request.POST.get('selected_participants').split(',') for participant in participants: if participant == u'': continue participant_obj = Participant.objects.get(pk=participant) item.participants.add(participant_obj) # return redirect('/inventory/items/') descriptions = request.POST.getlist('descriptions') file_ids = request.POST.getlist('file_ids') if request.POST.get('deleted_files') == '': deleted_files = [] else: deleted_files = request.POST.get('deleted_files').split(',') for index in request.POST.getlist('indices'): ind = int(index) file_id = file_ids[ind] if file_id == u'': the_file = File() else: the_file = File.objects.get(id=file_id) the_file.description = descriptions[ind] if request.FILES.get('files[' + index + ']'): the_file.file = request.FILES.get('files[' + index + ']') elif request.POST.get('clears[' + index + ']'): the_file.file = None the_file.training = item the_file.save() for deleted_file_id in deleted_files: # if deleted_file_id == '': # continue # try: the_file = File.objects.get(id=deleted_file_id) the_file.delete() # except File.DoesNotExist: # pass return redirect(reverse('update_training', kwargs={'pk': item.id})) else: form = TrainingForm(instance=item) if scenario == 'Update': participants = [x.id for x in item.participants.all()] training_files = item.files.all() else: participants = [] training_files = [] files = FileSerializer(training_files).data return render(request, 'training_form.html', { 'scenario': scenario, 'form': form, 'base_template': 'training_base.html', 'category_form': CategoryForm(instance=Category()), 'resource_person_form': ResourcePersonForm(instance=ResourcePerson()), 'target_group_form': TargetGroupForm(instance=TargetGroup()), 'participant_form': ParticipantForm(instance=Participant()), 'organization_form': OrganizationForm(instance=Organization()), 'participants': participants, 'files': files, 'training_files': training_files, })
# del d['gdpr'] # del d['days_since'] del d["blogpost"] d["received"] = dateparser.parse(d["received"]).date() d["start"] = dateparser.parse(d["start"]).date() d["end"] = dateparser.parse(d["end"]).date() if d["processed"].lower().startswith("y"): d["processed"] = "AP" else: d["processed"] = "UN" if d["advertise_eu"].lower().startswith("y"): d["advertise_eu"] = "Y" else: d["advertise_eu"] = "N" if d["use_gtn"].lower().startswith("y"): d["use_gtn"] = "Y" else: d["use_gtn"] = "N" d["training_identifier"] = d["training_identifier"][0:20] d["location"] = [x.strip().split(" ")[0] for x in d["location"].split(",")] __import__("pprint").pprint(d) t = Training(**d) t.save()
def training_form(request, pk=None): if pk: item = get_object_or_404(Training, pk=pk) scenario = 'Update' else: item = Training() scenario = 'Add' if request.POST: form = TrainingForm(data=request.POST, instance=item) if form.is_valid(): item = form.save(commit=False) if item.starts == '': item.starts = None if item.ends == '': item.ends = None item.save() form.save_m2m() item.participants.clear() participants = request.POST.get('selected_participants').split(',') for participant in participants: if participant == u'': continue participant_obj = Participant.objects.get(pk=participant) item.participants.add(participant_obj) # return redirect('/inventory/items/') descriptions = request.POST.getlist('descriptions') file_ids = request.POST.getlist('file_ids') if request.POST.get('deleted_files') == '': deleted_files = [] else: deleted_files = request.POST.get('deleted_files').split(',') for index in request.POST.getlist('indices'): ind = int(index) file_id = file_ids[ind] if file_id == u'': the_file = File() else: the_file = File.objects.get(id=file_id) the_file.description = descriptions[ind] if request.FILES.get('files[' + index + ']'): the_file.file = request.FILES.get('files[' + index + ']') elif request.POST.get('clears[' + index + ']'): the_file.file = None the_file.training = item the_file.save() for deleted_file_id in deleted_files: # if deleted_file_id == '': # continue # try: the_file = File.objects.get(id=deleted_file_id) the_file.delete() # except File.DoesNotExist: # pass return redirect(reverse('update_training', kwargs={'pk': item.id})) else: form = TrainingForm(instance=item) if scenario == 'Update': participants = [x.id for x in item.participants.all()] training_files = item.files.all() else: participants = [] training_files = [] files = FileSerializer(training_files, many=True).data return render( request, 'training_form.html', { 'scenario': scenario, 'form': form, 'base_template': 'training_base.html', 'category_form': CategoryForm(instance=Category()), 'resource_person_form': ResourcePersonForm(instance=ResourcePerson()), 'target_group_form': TargetGroupForm(instance=TargetGroup()), 'participant_form': ParticipantForm(instance=Participant()), 'organization_form': OrganizationForm(instance=Organization()), 'participants': participants, 'files': files, 'training_files': training_files, })