def add(request): form = TrainForm() if request.method == 'POST': form = TrainForm(request.POST) if form.is_valid(): new_train = Train() new_train.name = form.cleaned_data['name'] new_train.from_city = form.cleaned_data['from_city'] new_train.to_city = form.cleaned_data['to_city'] new_train.travel_time = form.cleaned_data['travel_time'] new_train.save() messages.success(request, 'Поезд успешно добавлен') return redirect('train:home') return render(request, 'trains/add.html', {'form': form})
def index(request): error_messages = None if request.method == 'POST': try: train = Train(description = request.POST['description'], username = request.POST['username'], departure = request.POST['departure'], status = Status.objects.get(name = 'scheduled')) train.full_clean() if train.departure < datetime.now(): exception = ValidationError('Departure should be in future') raise exception train.save() return redirect(reverse('trains.views.view', kwargs = { 'train_id': train.pk })) except ValidationError, error: error_messages = error.messages
def setUp(self): self.city_A = City.objects.create(name='A') self.city_B = City.objects.create(name='B') self.city_C = City.objects.create(name='C') self.city_D = City.objects.create(name='D') self.city_E = City.objects.create(name='E') t1 = Train(name='t1', from_city=self.city_A, to_city=self.city_B, travel_time=9) t1.save() t2 = Train(name='t2', from_city=self.city_B, to_city=self.city_D, travel_time=8) t2.save() t3 = Train(name='t3', from_city=self.city_A, to_city=self.city_C, travel_time=7) t3.save() t4 = Train(name='t4', from_city=self.city_C, to_city=self.city_B, travel_time=6) t4.save() t5 = Train(name='t5', from_city=self.city_B, to_city=self.city_E, travel_time=3) t5.save() t6 = Train(name='t6', from_city=self.city_B, to_city=self.city_A, travel_time=11) t6.save() t7 = Train(name='t7', from_city=self.city_A, to_city=self.city_C, travel_time=10) t7.save() t8 = Train(name='t8', from_city=self.city_E, to_city=self.city_D, travel_time=5) t8.save() t9 = Train(name='t9', from_city=self.city_D, to_city=self.city_E, travel_time=4) t9.save()
name = "LLHQ", location = "Emeryville, CA", latitude = "37.842139", longitude = "-122.289215").save() dt = datetime.datetime.now() - datetime.timedelta(days = 5) numDests = Restaurant.objects.all().count() rid = random.randint(0, numDests) numDests = Restaurant.objects.all().count() numUsers = User.objects.all().count() for cap in User.objects.all(): rid = random.randint(1, numDests) dest = Restaurant.objects.get(id = rid) id1 = random.randint(1, numUsers) id2 = random.randint(1, numUsers) p1 = User.objects.get(id = id1) p2 = User.objects.get(id = id2) t = Train(departure_time = dt, captain = cap, destination = dest) t.save() t.passengers = [] t.passengers.add(p1) t.passengers.add(p2) t.save() print 'Created initial data in DB!'