Randomly select someone
This commit is contained in:
parent
a0b79b68ac
commit
09e3aece73
2 changed files with 37 additions and 1 deletions
|
@ -6,6 +6,9 @@
|
|||
<div class="row">
|
||||
<div class="col-md-7"><!-- Shitty html-->
|
||||
<h3>Order {{ order.id }}</h3>
|
||||
{% if (current_user.id == order.courrier_id or current_user.is_admin()) and not order.stoptime -%}
|
||||
<a class="btn btn-danger" href="{{ url_for('.close_order', id=order.id) }}">Close</a><br/>
|
||||
{%- endif %}
|
||||
Courrier: {{ order.courrier.username }}
|
||||
{% if order.courrier == None %}
|
||||
<a href="{{ url_for('.volunteer', id=order.id) }}" class="btn btn-primary">Volunteer</a>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
__author__ = 'feliciaan'
|
||||
from flask import url_for, render_template, abort, redirect, Blueprint, flash
|
||||
from flask.ext.login import current_user, login_required
|
||||
import random
|
||||
from datetime import datetime
|
||||
|
||||
from app import app, db
|
||||
|
@ -90,4 +91,36 @@ def volunteer(id):
|
|||
return redirect(url_for('.order', id=id))
|
||||
abort(404)
|
||||
|
||||
|
||||
@order_bp.route('/<id>/close')
|
||||
@login_required
|
||||
def close_order(id):
|
||||
order = Order.query.filter(Order.id == id).first()
|
||||
if order is not None:
|
||||
if (current_user.id == order.courrier_id or current_user.is_admin()) \
|
||||
and order.stoptime is None:
|
||||
order.stoptime = datetime.now()
|
||||
print(order.courrier_id)
|
||||
if order.courrier_id == 0 or order.courrier_id is None:
|
||||
order.courrier_id = select_user(order.orders).id
|
||||
print(order.courrier_id)
|
||||
db.session.commit()
|
||||
return redirect(url_for('.order', id=id))
|
||||
abort(401)
|
||||
|
||||
app.register_blueprint(order_bp, url_prefix='/order')
|
||||
|
||||
|
||||
def select_user(items):
|
||||
user = None
|
||||
items = list(items)
|
||||
if len(items) <= 0:
|
||||
return None
|
||||
|
||||
while user is None:
|
||||
item = random.choice(items)
|
||||
user = item.user
|
||||
if random.randint(user.bias, 100) < 80:
|
||||
user = None
|
||||
|
||||
return user
|
||||
|
|
Loading…
Reference in a new issue