implmented app

This commit is contained in:
owengavi2
2026-03-31 14:41:56 -04:00
parent 4a820741fa
commit 7c12f67d9f
6 changed files with 102 additions and 2 deletions

47
car_spotting/app/views.py Normal file
View File

@@ -0,0 +1,47 @@
from banjo.urls import route_get, route_post
from banjo.http import BadRequest, NotFound
from app.models import SpottingEvent
@route_get('all', args={})
def list_events(params):
events = sorted(SpottingEvent.objects.all(), key=lambda event: event.make)
return {'events': [event.to_dict() for event in events]}
@route_post('new', args={'make': str, 'model': str, 'color': str, 'bodystyle': str, 'location': str, 'note': str, 'instances': int} )
def create_event(params):
event = SpottingEvent.from_dict(params)
event.save()
return {'event': event.to_dict()}
@route_get('search', args={'category': str, 'value': str})
def search_events(params):
validcat = ['id', 'make', 'model', 'color', 'bodystyle', 'location']
if params['category'] not in validcat:
raise BadRequest('Invalid category: can search by id, make, model, bodystyle, or location')
if params['category'] == 'make':
matches = SpottingEvent.objects.filter(make=params['value'])
elif params['category'] == 'model':
matches = SpottingEvent.objects.filter(model=params['value'])
elif params['category'] == 'bodystyle':
matches = SpottingEvent.objects.filter(bodystyle=params['value'])
elif params['category'] == 'location':
matches = SpottingEvent.objects.filter(location=params['value'])
elif params['category'] == 'color':
matches = SpottingEvent.objects.filter(color=params['value'])
elif params['category'] == 'id':
matches = SpottingEvent.objects.filter(id=int(params['value']))
if not matches:
raise NotFound("No Event Found (make and model start with capital letters, e.g. Honda Civic)")
return {'events': [event.to_dict() for event in matches]}
@route_post('addnotes', args={'id': int, 'new_note': str})
def add_note(params):
try:
event = SpottingEvent.objects.get(id=params['id'])
except:
raise NotFound('Event not found')
event.note = params['new_note']
event.save()
return {'event': event.to_dict()}