def unit_form(request, id=None): if id: obj = get_object_or_404(Unit, id=id, company=request.company) scenario = 'Update' else: obj = Unit(company=request.company) scenario = 'Create' if request.POST: form = UnitForm(data=request.POST, instance=obj) if form.is_valid(): obj = form.save(commit=False) obj.company = request.company obj.save() if request.is_ajax(): return render(request, 'callback.html', {'obj': {'name': obj.name, 'id': obj.id}}) return redirect(reverse_lazy('list_units')) else: form = UnitForm(instance=obj) if request.is_ajax(): base_template = 'modal.html' else: base_template = 'dashboard.html' return render(request, 'unit_form.html', { 'scenario': scenario, 'form': form, 'base_template': base_template, })
def set_owner(player, territory_name, game): territory = Territory.objects( name=territory_name, game_id=str(game.id)).first() unit=Unit(owner=player, unit_type="Infantry", number=0) unit.save() print unit territory.units.append(unit) return territory
def details(request, bldg_tag, city_tag): city = City.objects.filter(tag=city_tag) #old way... this doesn't work very reliably: #address = re.sub('_', ' ', bldg_tag) #buildings = Building.objects.filter(city=city).filter(address=address) buildings = Building.objects.filter(city=city).filter(tag=bldg_tag) if buildings.count(): building = buildings[0] if not building.units.count(): #must have a building with no associated units... #may only have one unit #or others may have been incorrectly created as separate buildings #either way we can start by making a new unit here #(and then merging in any others manually) unit = Unit() unit.building = building unit.number = '' # don't want to set this unless it's different: #unit.address = building.address ## bedrooms ## bathrooms ## sqft ## max_occupants unit.save() else: building = None #print building.units.all()[0].tag #unit_url = reverse('unit_details', kwargs={'city_tag':building.city.tag, 'bldg_tag':building.tag, 'unit_tag':building.units.all()[0].tag}) #print unit_url context = { 'building': building, 'units': building.units.all(), 'user': request.user, 'redirect_field': REDIRECT_FIELD_NAME, } return render(request, 'details.html', context)
def test01(self): u1 = Unit(title="First Chapter") u1.save() self.assertEqual(u1.seq,1) u = Unit(title="First Section",parent=u1) u.save() self.assertEqual(u.seq,1) u = Unit(title="Second Section",parent=u1) u.save() self.assertEqual(u.seq,2) self.assertEqual(u.pk,3) try: #print "set parent to self" u.parent=u u.save() print "save() : done" except ModelValidationError,e: #s="\n".join([e.as_text() for m in e.messages]) self.assertEqual(str(e),"Parent cannot be self")
def test01(self): u1 = Unit(title="First Chapter") u1.save() self.assertEqual(u1.seq, 1) u = Unit(title="First Section", parent=u1) u.save() self.assertEqual(u.seq, 1) u = Unit(title="Second Section", parent=u1) u.save() self.assertEqual(u.seq, 2) self.assertEqual(u.pk, 3) try: #print "set parent to self" u.parent = u u.save() print "save() : done" except ModelValidationError, e: #s="\n".join([e.as_text() for m in e.messages]) self.assertEqual(str(e), "Parent cannot be self")
def test_infobase_add_from_sequence(self): v1 = Variable(full_name="acceleration") v1.save(no_wiki=True) u1 = Unit(full_name="ms") u1.save(no_wiki=True) u2 = Unit(full_name="meter") u2.save(no_wiki=True) u3 = Unit(full_name="second") u3.save(no_wiki=True) u1.make_composition_links("meter,second") v1.add_units_links("meter,second") try: uget1 = u1.composition_links.get(full_name="meter") uget2 = u1.composition_links.get(full_name="second") uget3 = v1.units_links.get(full_name="meter") uget4 = v1.units_links.get(full_name="second") except (ObjectDoesNotExist, MultipleObjectsReturned), e: raise AssertionError
u2.save(no_wiki=True) u3 = Unit(full_name="second") u3.save(no_wiki=True) u1.make_composition_links("meter,second") v1.add_units_links("meter,second") try: uget1 = u1.composition_links.get(full_name="meter") uget2 = u1.composition_links.get(full_name="second") uget3 = v1.units_links.get(full_name="meter") uget4 = v1.units_links.get(full_name="second") except (ObjectDoesNotExist, MultipleObjectsReturned), e: raise AssertionError v2 = Variable(full_name="Jon's Constant") v2.save(no_wiki=True) u4 = Unit(full_name="kilogram") u4.save(no_wiki=True) u4.make_composition_links("base") self.assertFalse(bool(u4.composition_links.all())) def test_definitions(self): e1 = Equation(full_name="Jon's Law") e1.save(no_wiki=True) v1 = Variable(full_name="Jon's Constant") v1.save(no_wiki=True) e1.add_defined_var(v1.full_name) self.assertEqual(e1.defined_var.full_name, v1.full_name) def test_equation_add_variables(self): e1 = Equation(full_name="Jon's Law") e1.save(no_wiki=True) v1 = Variable(full_name="Jon's Constant")