Randomly select someone

This commit is contained in:
Feliciaan De Palmenaer 2015-03-28 02:01:30 +01:00
parent a0b79b68ac
commit 09e3aece73
2 changed files with 37 additions and 1 deletions

View file

@ -6,6 +6,9 @@
<div class="row"> <div class="row">
<div class="col-md-7"><!-- Shitty html--> <div class="col-md-7"><!-- Shitty html-->
<h3>Order {{ order.id }}</h3> <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 }} Courrier: {{ order.courrier.username }}
{% if order.courrier == None %} {% if order.courrier == None %}
<a href="{{ url_for('.volunteer', id=order.id) }}" class="btn btn-primary">Volunteer</a> <a href="{{ url_for('.volunteer', id=order.id) }}" class="btn btn-primary">Volunteer</a>

View file

@ -1,6 +1,7 @@
__author__ = 'feliciaan' __author__ = 'feliciaan'
from flask import url_for, render_template, abort, redirect, Blueprint, flash from flask import url_for, render_template, abort, redirect, Blueprint, flash
from flask.ext.login import current_user, login_required from flask.ext.login import current_user, login_required
import random
from datetime import datetime from datetime import datetime
from app import app, db from app import app, db
@ -90,4 +91,36 @@ def volunteer(id):
return redirect(url_for('.order', id=id)) return redirect(url_for('.order', id=id))
abort(404) abort(404)
app.register_blueprint(order_bp, url_prefix='/order')
@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