vlask
This commit is contained in:
parent
d960a1413c
commit
51af80d6ba
8 changed files with 220 additions and 150 deletions
16
.gitignore
vendored
16
.gitignore
vendored
|
@ -1,5 +1,13 @@
|
||||||
.idea/
|
.idea/
|
||||||
*.pyc
|
*.db
|
||||||
venv/
|
|
||||||
*.pickle
|
# vlang
|
||||||
*.db
|
main
|
||||||
|
v
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
vls.log
|
||||||
|
.env
|
|
@ -1 +0,0 @@
|
||||||
python 3.11.6
|
|
|
@ -2,4 +2,4 @@
|
||||||
|
|
||||||
De pannenkoekenwachtrij voor wanneer er pannenkoeken worden gebakken in de kelder.
|
De pannenkoekenwachtrij voor wanneer er pannenkoeken worden gebakken in de kelder.
|
||||||
|
|
||||||
Server opzetten doe je door `FLASK_APP=main.py flask run` te runnen.
|
Server opzetten doe je door `v run .` te runnen.
|
113
main.py
113
main.py
|
@ -1,113 +0,0 @@
|
||||||
import pickle
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
import humanize
|
|
||||||
from flask import Flask, render_template, request, redirect
|
|
||||||
from os.path import exists
|
|
||||||
from requests import post, exceptions
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class Person:
|
|
||||||
statusses = [
|
|
||||||
"Pannenkoek besteld",
|
|
||||||
"Pannenkoek aan het bakken",
|
|
||||||
"Pannenkoek klaar",
|
|
||||||
"Pannenkoek afgegeven",
|
|
||||||
]
|
|
||||||
|
|
||||||
def __init__(self, name, remark="", order_time=None):
|
|
||||||
self.name = name
|
|
||||||
self.remark = remark.strip()
|
|
||||||
self.status = 0
|
|
||||||
self.order_time = order_time
|
|
||||||
|
|
||||||
def get_remark(self):
|
|
||||||
return "({})".format(self.remark) if self.remark else self.remark
|
|
||||||
|
|
||||||
def get_name(self):
|
|
||||||
return self.name
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
return self.name.lower() == other.name.lower()
|
|
||||||
|
|
||||||
def get_status(self):
|
|
||||||
return self.statusses[self.status]
|
|
||||||
|
|
||||||
def next_status(self):
|
|
||||||
return self.statusses[self.status + 1]
|
|
||||||
|
|
||||||
def order_time_humanized(self):
|
|
||||||
return humanize.naturaltime(self.order_time)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return "Persoon: {} met status: {}".format(self.name, self.status)
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return str(self)
|
|
||||||
|
|
||||||
|
|
||||||
people_filename = "people.pickle"
|
|
||||||
people = []
|
|
||||||
|
|
||||||
if exists(people_filename):
|
|
||||||
people_file = open(people_filename, "rb")
|
|
||||||
people = pickle.load(people_file)
|
|
||||||
people_file.close()
|
|
||||||
|
|
||||||
|
|
||||||
def save_people():
|
|
||||||
people_file = open(people_filename, "wb")
|
|
||||||
pickle.dump(people, people_file)
|
|
||||||
people_file.close()
|
|
||||||
|
|
||||||
|
|
||||||
def update_index(index):
|
|
||||||
people[index].status += 1
|
|
||||||
if people[index].status == 2:
|
|
||||||
try:
|
|
||||||
post('http://10.1.0.224:8080/blink')
|
|
||||||
post('http://10.1.2.3', data=f"ScrollingText >>> {people[index].name} <<< Enjoy! ")
|
|
||||||
post('http://10.1.2.3', data="Option text_trailingWhitespace 1")
|
|
||||||
except exceptions.ConnectionError:
|
|
||||||
print('Failed to blink the light')
|
|
||||||
elif people[index].status == 3:
|
|
||||||
people.remove(people[index])
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def home():
|
|
||||||
return render_template("home.html", people=people)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/status_update", methods=["POST", "GET"])
|
|
||||||
def status_update():
|
|
||||||
if people and request.method == "POST":
|
|
||||||
result = request.form
|
|
||||||
if "index" in result:
|
|
||||||
index = int(request.form["index"])
|
|
||||||
update_index(index)
|
|
||||||
if "name" in result:
|
|
||||||
index = people.index(Person(request.form["name"]))
|
|
||||||
update_index(index)
|
|
||||||
save_people()
|
|
||||||
return redirect("/")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/add_person", methods=["POST", "GET"])
|
|
||||||
def add_person():
|
|
||||||
if request.method == "POST":
|
|
||||||
result = request.form
|
|
||||||
if result["name"]:
|
|
||||||
new_person = Person(
|
|
||||||
result["name"], result["remark"], order_time=datetime.now()
|
|
||||||
)
|
|
||||||
if new_person not in people:
|
|
||||||
people.append(new_person)
|
|
||||||
save_people()
|
|
||||||
return redirect("/")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run()
|
|
|
@ -1,3 +0,0 @@
|
||||||
flask
|
|
||||||
humanize
|
|
||||||
requests
|
|
165
src/main.v
Normal file
165
src/main.v
Normal file
|
@ -0,0 +1,165 @@
|
||||||
|
module main
|
||||||
|
|
||||||
|
import vweb
|
||||||
|
import db.sqlite
|
||||||
|
import time
|
||||||
|
import net.http
|
||||||
|
|
||||||
|
const http_port = 8080
|
||||||
|
|
||||||
|
enum Status {
|
||||||
|
besteld = 0
|
||||||
|
bakken = 1
|
||||||
|
klaar = 2
|
||||||
|
afgegeven = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (s Status) str() string {
|
||||||
|
return match s {
|
||||||
|
.besteld { 'Pannenkoek besteld' }
|
||||||
|
.bakken { 'Pannenkoek aan het bakken' }
|
||||||
|
.klaar { 'Pannenkoek klaar' }
|
||||||
|
.afgegeven { 'Pannenkoek afgegeven' }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Person {
|
||||||
|
id int @[primary; sql: serial]
|
||||||
|
status int
|
||||||
|
name string
|
||||||
|
remark string
|
||||||
|
order_time time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (p Person) str() string {
|
||||||
|
return 'Person[id:${p.id}, status:${p.status}]'
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (p Person) order_time_humanized() string {
|
||||||
|
return p.order_time.relative()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (p Person) remark() string {
|
||||||
|
return if p.remark.len > 0 {
|
||||||
|
'(${p.remark})'
|
||||||
|
} else {
|
||||||
|
''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (p Person) status_str() string {
|
||||||
|
unsafe {
|
||||||
|
s := Status(p.status)
|
||||||
|
return s.str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Database ===
|
||||||
|
|
||||||
|
pub fn create_db_connection() !sqlite.DB {
|
||||||
|
return sqlite.connect('pancakes.db')!
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_people() ![]Person {
|
||||||
|
mut db := create_db_connection()!
|
||||||
|
status_filter := int(Status.afgegeven)
|
||||||
|
people := sql db {
|
||||||
|
select from Person where status < status_filter
|
||||||
|
}!
|
||||||
|
return people
|
||||||
|
}
|
||||||
|
|
||||||
|
fn status_update(user_id int) !Person {
|
||||||
|
mut db := create_db_connection()!
|
||||||
|
people := sql db {
|
||||||
|
select from Person where id == user_id
|
||||||
|
}!
|
||||||
|
person := people.first()
|
||||||
|
sql db {
|
||||||
|
update Person set status = person.status + 1 where id == person.id
|
||||||
|
}!
|
||||||
|
return person
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_person(name string, remark string) ! {
|
||||||
|
mut db := create_db_connection()!
|
||||||
|
people := sql db {
|
||||||
|
select from Person where name == name && status < 3
|
||||||
|
}!
|
||||||
|
|
||||||
|
if people.len == 0 {
|
||||||
|
p := Person{
|
||||||
|
status: 0
|
||||||
|
order_time: time.now()
|
||||||
|
name: name
|
||||||
|
remark: remark
|
||||||
|
}
|
||||||
|
sql db {
|
||||||
|
insert p into Person
|
||||||
|
}!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === WEB ===
|
||||||
|
|
||||||
|
struct App {
|
||||||
|
vweb.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (app App) before_request() {
|
||||||
|
println('[Vweb] ${app.Context.req.method} ${app.Context.req.url}')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println('Start pannenkoeken webserver')
|
||||||
|
mut db := create_db_connection() or { panic(err) }
|
||||||
|
|
||||||
|
sql db {
|
||||||
|
create table Person
|
||||||
|
} or { panic('error on create table: ${err}') }
|
||||||
|
|
||||||
|
db.close() or { panic(err) }
|
||||||
|
|
||||||
|
vweb.run(&App{}, http_port)
|
||||||
|
}
|
||||||
|
|
||||||
|
@['/'; get]
|
||||||
|
pub fn (mut app App) home() vweb.Result {
|
||||||
|
people := get_people() or {
|
||||||
|
app.set_status(400, '')
|
||||||
|
return app.text('${err}')
|
||||||
|
}
|
||||||
|
return $vweb.html()
|
||||||
|
}
|
||||||
|
|
||||||
|
@['/status_update'; post]
|
||||||
|
pub fn (mut app App) status_update() vweb.Result {
|
||||||
|
if person := status_update(app.form['id'].int()) {
|
||||||
|
if person.status == 1 {
|
||||||
|
spawn fn () {
|
||||||
|
http.post('http://10.1.0.224:8080/blink', '') or {}
|
||||||
|
}()
|
||||||
|
spawn fn (name string) {
|
||||||
|
http.post('http://10.1.2.3', 'ScrollingText >>> ${name} <<< Enjoy! ') or {}
|
||||||
|
http.post('http://10.1.2.3', 'Option text_trailingWhitespace 1') or {}
|
||||||
|
}(person.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return app.redirect('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
@['/add_person'; post]
|
||||||
|
pub fn (mut app App) add_person() vweb.Result {
|
||||||
|
name := app.form['name']
|
||||||
|
if name.len == 0 {
|
||||||
|
return app.redirect('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
add_person(app.form['name'], app.form['remark']) or {
|
||||||
|
app.set_status(400, '')
|
||||||
|
return app.text('${err}')
|
||||||
|
}
|
||||||
|
|
||||||
|
return app.redirect('/')
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.5">
|
<meta name="viewport" content="width=device-width, initial-scale=1.5">
|
||||||
<title>Pannenkoekenwachtrij</title>
|
<title>Pannenkoekenwachtrij</title>
|
||||||
<link href="https://cdn.jsdelivr.net/npm/tuicss@2.1.1/dist/tuicss.min.css" rel="stylesheet"/>
|
<link href="https://cdn.jsdelivr.net/npm/tuicss@@2.1.1/dist/tuicss.min.css" rel="stylesheet"/>
|
||||||
<style>
|
<style>
|
||||||
.tui-window {
|
.tui-window {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
@ -66,18 +66,20 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@if people.len > 0
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col m12">
|
<div class="col m12">
|
||||||
<div class="tui-window red-168">
|
<div class="tui-window red-168">
|
||||||
<fieldset class="tui-fieldset">
|
<fieldset class="tui-fieldset">
|
||||||
<form action="/status_update" method="POST">
|
<form action="/status_update" method="POST">
|
||||||
<input type="hidden" name="index" value="0">
|
<input type="hidden" name="id" value="@{people[0].id}">
|
||||||
<input class="tui-button green-168" type="submit" value="Update First Person" style="width: 100%">
|
<input class="tui-button green-168" type="submit" value="Update First Person" style="width: 100%">
|
||||||
</form>
|
</form>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@end
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col m12">
|
<div class="col m12">
|
||||||
<div class="tui-window red-168">
|
<div class="tui-window red-168">
|
||||||
|
@ -88,35 +90,39 @@
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="4" style="text-align:center; vertical-align:middle; padding-top: 20px">Volgende persoon</td>
|
<td colspan="4" style="text-align:center; vertical-align:middle; padding-top: 20px">Volgende persoon</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% for person in people[:1] %}
|
@if people.len > 0
|
||||||
<tr>
|
@for person in people[..1]
|
||||||
<td>{{ person.get_name() + person.get_remark() }}</td>
|
<tr>
|
||||||
<td>{{ person.get_status() }}</td>
|
<td>@person.name @person.remark()</td>
|
||||||
<td>{{ person.order_time_humanized() if person.order_time else "Unknown" }}</td>
|
<td>@person.status_str()</td>
|
||||||
<td>
|
<td>@person.order_time_humanized()</td>
|
||||||
<form action="/status_update" method="POST">
|
<td>
|
||||||
<input type="hidden" name="name" value="{{ person.get_name() }}">
|
<form action="/status_update" method="POST">
|
||||||
<input class="tui-button green-168" type="submit" value="Update">
|
<input type="hidden" name="id" value="@person.id">
|
||||||
</form>
|
<input class="tui-button green-168" type="submit" value="Update">
|
||||||
</td>
|
</form>
|
||||||
</tr>
|
</td>
|
||||||
{% endfor %}
|
</tr>
|
||||||
|
@end
|
||||||
|
@end
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="4" style='text-align:center; vertical-align:middle'>Andere personen</td>
|
<td colspan="4" style='text-align:center; vertical-align:middle'>Andere personen</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% for person in people[1:] %}
|
@if people.len > 1
|
||||||
<tr>
|
@for person in people[1..]
|
||||||
<td>{{ person.get_name() + person.get_remark() }}</td>
|
<tr>
|
||||||
<td>{{ person.get_status() }}</td>
|
<td>@person.name @person.remark()</td>
|
||||||
<td>{{ person.order_time_humanized() if person.order_time else "Unknown" }}</td>
|
<td>@person.status_str()</td>
|
||||||
<td>
|
<td>@person.order_time_humanized()</td>
|
||||||
<form action="/status_update" method="POST">
|
<td>
|
||||||
<input type="hidden" name="name" value="{{ person.get_name() }}">
|
<form action="/status_update" method="POST">
|
||||||
<input class="tui-button green-168" type="submit" value="Update">
|
<input type="hidden" name="id" value="@person.id">
|
||||||
</form>
|
<input class="tui-button green-168" type="submit" value="Update">
|
||||||
</td>
|
</form>
|
||||||
</tr>
|
</td>
|
||||||
{% endfor %}
|
</tr>
|
||||||
|
@end
|
||||||
|
@end
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</fieldset>
|
</fieldset>
|
8
v.mod
Normal file
8
v.mod
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
Module {
|
||||||
|
name: 'pannenkoekenwachtrij'
|
||||||
|
description: 'pannenkoekenvvachtrij'
|
||||||
|
version: '2.0.0'
|
||||||
|
license: 'MIT'
|
||||||
|
repo_url: 'https://git.zeus.gent/Kelder/pannenkoekenwachtrij'
|
||||||
|
dependencies: []
|
||||||
|
}
|
Loading…
Reference in a new issue