generated from mwc/project_banjo_app
implmented app
This commit is contained in:
BIN
car_spotting/app/database.sqlite
Normal file
BIN
car_spotting/app/database.sqlite
Normal file
Binary file not shown.
37
car_spotting/app/migrations/0001_initial.py
Normal file
37
car_spotting/app/migrations/0001_initial.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# Generated by Django 5.1.4 on 2026-03-31 16:15
|
||||
|
||||
import banjo.models
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = []
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="SpottingEvent",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("make", banjo.models.StringField(default="")),
|
||||
("model", banjo.models.StringField(default="")),
|
||||
("color", banjo.models.StringField(default="")),
|
||||
("bodystyle", banjo.models.StringField(default="")),
|
||||
("location", banjo.models.StringField(default="")),
|
||||
("instances", banjo.models.IntegerField(default=0)),
|
||||
("note", banjo.models.StringField(blank=True, default="", null=True)),
|
||||
],
|
||||
options={
|
||||
"abstract": False,
|
||||
},
|
||||
),
|
||||
]
|
||||
0
car_spotting/app/migrations/__init__.py
Normal file
0
car_spotting/app/migrations/__init__.py
Normal file
16
car_spotting/app/models.py
Normal file
16
car_spotting/app/models.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from banjo.models import Model, StringField, IntegerField
|
||||
|
||||
|
||||
class SpottingEvent(Model):
|
||||
make = StringField()
|
||||
model = StringField()
|
||||
color = StringField()
|
||||
bodystyle = StringField()
|
||||
location = StringField()
|
||||
instances = IntegerField()
|
||||
note = StringField(blank=True, null=True)
|
||||
|
||||
def __repr__(self):
|
||||
rep = self.to_dict()
|
||||
return rep
|
||||
|
||||
47
car_spotting/app/views.py
Normal file
47
car_spotting/app/views.py
Normal 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()}
|
||||
|
||||
Reference in New Issue
Block a user