def create_instrument(request): try: Administrator.objects.get(user=request.user) except ObjectDoesNotExist: raise PermissionDenied if request.method == 'POST': form = InstrumentForm(request.POST, request.FILES) if form.is_valid(): name = form.cleaned_data['name'] image = form.cleaned_data['image'] check_instrument = Instrument.objects.filter(name=name) if len(check_instrument) == 0: service.create_instrument(name, image) return redirect('/instruments') else: error = "This instrument already exists" return render(request, 'createInstrument.html', { 'form': form, 'error': error }) else: form = InstrumentForm() return render(request, 'createInstrument.html', {'form': form})
def test_negative_create_instrument(self): name = "Piano" image = "piano.jpg" service.create_instrument(name, image) result = service.create_instrument(name, image) self.assertEquals(result, "This instrument already exists")
def test_create_instrument(self): name = "Prueba" image = "prueba.jpg" instrument = service.create_instrument(name, image) instrument_saved = Instrument.objects.get(id=instrument.id) self.assertEquals(instrument_saved.name, name) self.assertEquals(instrument_saved.image, image)
def test_delete_instrument(self): name = "Piano" image = "piano.jpg" instrument = service.create_instrument(name, image) instrument.delete() instruments = Instrument.objects.all() self.assertFalse(instrument in instruments)
def test_edit_instrument(self): name = "Piano" image = "piano.jpg" instrument = service.create_instrument(name, image) new_name = "Edited" instrument.name = new_name instrument.save() edited = Instrument.objects.get(id=instrument.id) self.assertEquals(edited.name, new_name)