Ejemplo n.º 1
0
def trainsearch(request):
	if request.method == 'POST':
		form = TrainSearchForm(request.POST)

		if form.is_valid():
			data = form.cleaned_data
			
			query = dict(search_operator = data['operator'] , search_number = data['number'])

			if data['operator']:
				ss = data['operator'] + ' ' + str(data['number'])
			else:
				ss = str(data['number'])
			sr = Train.search(ss)

			if len(sr) == 0:
				query.update(dict(message = 'Brak pociągów w bazie danych spełniających kryteria wyszukiwania'))
				return query
			elif len(sr) > 1:
				query.update(dict(trains = sr))
				return query
			else:
				train, = sr
				
				raise Redirect(reverse('kolstat-train', args=[train.category.name, train.number]))


		else:
			return dict(message = 'Musisz podać numer pociągu')

	return dict()
Ejemplo n.º 2
0
def train(request, operator, number, variant = None):
	res = Train.search(operator + ' ' + number, variant)
	if len(res) != 1:
		raise Redirect(reverse('kolstat-trains-search'))

	ktrain, = res

	return dict(train = ktrain)
Ejemplo n.º 3
0
def train_add(request):
	if request.method == 'POST':
		form = AddTrainForm(request.POST)
		if form.is_valid():
			name = form.cleaned_data['name']
			
			train = Train.add_train(name)

			raise Redirect(reverse('kolstat-train', args=[train.operator, train.number]))
	else:
		form = AddTrainForm()

	return dict(form = form)
Ejemplo n.º 4
0
def train_date(request, operator, number, variant = None, date = None):
	res = Train.search(operator + ' ' + number, variant)
	if len(res) != 1:
		raise Redirect(reverse('kolstat-trains-search'))

	ktrain, = res
	
	d = datetime.datetime.strptime(date, '%Y-%m-%d').date()

	try:
		tt, = ktrain.traintimetable_set.filter(date = d).select_related()
	except ValueError:
		raise KolstatError('Pociąg nie kursuje w tym dniu')


	table = {}
	stations = []

	trains = [tt] + tt.get_coupled()

	juz = 0

	for t in trains:
		ans = []
		for s in t.stops():
			ans.append((s.station, s))

		not_added=[]

		last = -1

		for st, s in ans:
			if st in list(table.keys()):
				while len(table[st]) <= juz:
					table[st].append("")
				table[st].append(s)
				idx = stations.index(st)
				stations[idx:idx] = not_added
				last = idx + len(not_added)
				not_added = []
			else:
				table[st] = [""] * juz + [s]

				not_added.append(st)

		stations[last+1:last+1] = not_added
		not_added = []
		juz += 1

	pres = []


	for st in stations:
		while len(table[st]) < juz:
			table[st].append("")
		km = []
		stops = []
		short = False

		pocz = True
		kon = True
		
		for s in table[st]:
			if type(s) == type(""):
				km.append("")
				stops.append(None)
			else:
				km.append(s.distance)
				stops.append(s)
				short |= s.is_short()
				if s.arrival is not None:
					pocz = False
				if s.departure is not None:
					kon = False

		pres.append((km, st, short, pocz, kon, stops))


	return dict(train = ktrain, timetable = tt, table = pres, ile = juz, trains = [x.train for x in trains])
Ejemplo n.º 5
0
def train_delete(request, train_id):
	train = get_object_or_404(Train, pk = train_id)

	train.delete_train()

	raise Redirect(reverse('admin-trains'))
Ejemplo n.º 6
0
def print_month(train, month):
	res = '<table class="ttmonth">'

	res += '<tr><th colspan="7">{}</th></tr>'.format(MONTH_NAMES[month])


	if month >= 0:
		d = date(settings.TIMETABLE_YEAR, month+1, 1)
	else:
		month += 12
		d = date(settings.TIMETABLE_YEAR -1 , month + 1, 1)

	offset = d.weekday()

	if offset:
		res += '<tr>'
		while offset:
			res += '<td />'
			offset -= 1
	
	tts = { x.date for x in train.timetables() }

	while d.month == month +1:
		if d.weekday() == 0:
			res += '<tr>'

		cl = []

		if d == date.today():
			cl.append('today')

		kursuje = d in tts

		if kursuje:
			cl.append('valid')
		elif d <= settings.TIMETABLE_END.date() and d >= settings.TIMETABLE_START.date():
			cl.append('invalid')
		else:
			cl.append('outside')

		if cl:
			cls = ' class="{}"'.format(' '.join(cl))
		else:
			cls = ''

		if train.has_variants():
			url = reverse('kolstat-train-date-variant', args=[train.category.name, train.number, train.variant, d.strftime('%Y-%m-%d')])
		else:
			url = reverse('kolstat-train-date', args=[train.category.name, train.number, d.strftime('%Y-%m-%d')])

		res += '<td{}>{}{}{}</td>'.format(cls,'' if not kursuje else '<a href="{}">'.format(url), d.day, '' if not kursuje else '</a>')

		if d.weekday() == 6:
			res += '</tr>'
			
		d += DZIEN
	
	if d.weekday() != 0:
		while d.weekday() != 0:
			res += '<td />'
			d += DZIEN

		res += '</tr>'

	res += '</table>'
	return res