def component_new(request): """Add a new component to the database""" new_component = Component() edit_form = ComponentEditForm(request.POST or None) tips_form = TipsForm(request.POST or None) demo_vids = ComponentDemoVideoFormset(request.POST or None, instance=new_component) tutorial_vids = ComponentTutorialVideoFormset(request.POST or None, instance=new_component) if demo_vids.is_valid() and tutorial_vids.is_valid() and edit_form.is_valid(): new_component.name = edit_form.cleaned_data.get("name") existing_components = Component.objects.filter(slug=slugify(new_component.name)) if existing_components: return HttpResponse("Error saving: component with slug {0} already exists!".format(slugify(new_component.name))) else: new_component.save() demo_vids.save() tutorial_vids.save() if tips_form.is_valid(): ComponentTips.objects.create( component=new_component, tips=tips_form.cleaned_data.get("tips"), tips_markup_type='markdown', ) return HttpResponseRedirect(reverse('component_detail', args=[new_component.slug])) context = RequestContext(request, { 'add_new': True, #Flag for template 'edit_form': edit_form, 'tips_form': tips_form, 'demo_vids': demo_vids, 'tutorial_vids': tutorial_vids, }) template = loader.get_template('footbagmoves/component_modify.html') return HttpResponse(template.render(context))
def test_creating_component_and_saving_to_db(self): """Test that we can create a component and save it to the database""" comp1 = Component() comp1.name = "Toe stall" comp1.save() all_components_in_db = Component.objects.all() self.assertEquals(len(all_components_in_db), 1) only_component_in_db = all_components_in_db[0] self.assertEquals(only_component_in_db, comp1) #Test the component saved it's name properly in the DB self.assertEquals(only_component_in_db.name, "Toe stall")