#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import render_template, url_for, redirect, request, session, flash, Blueprint
from models import User, Collection
from helpers import user_required
from database import db
import json

bp = Blueprint('collection', __name__, url_prefix='/collection/')


@bp.route('')
@user_required
def mine():
    """ Affiche la collection de l’utilisateur connecté """

    username = session["username"]
    collections = Collection.query.filter_by(login_user=session["username"])

    return render_template("collection/index.html", **locals())


@bp.route('update', methods=["POST"])
@user_required
def update():
    """ Met à jour la collection de l’utilisateur connecté """

    text = request.form["json"]
    data = json.loads(text)
    for id, modifs in data.items():
        modification = {}
        for modif in modifs:
            modification[modif["type"]] = modif["value"]
        if modification["collection"] == 1:
            modification["souhaite"] = 0
        else:
            modification["echange"] = 0

        collec = Collection().query.get(id)
        collec.in_collection = modification["collection"]
        collec.souhaite = modification["souhaite"]
        collec.accepte_echange = modification["echange"]

        db.session.commit()
    return redirect(url_for("echange.compute"))


@bp.route("compare/<string:usr>")
@user_required
def compare(usr):
    username = session["username"]

    if usr == username:
        flash(u"Ça sert à rien de se comparer avec soi-même …", "info")
        return redirect(url_for("collection.mine"))

    user = User.query.get(usr)

    if user is None:
        flash(u"L’utilisateur saisi n’existe pas", "danger")
        return redirect(url_for("collection.mine"))

    collection_connected = Collection.query.filter_by(login_user=username).all()
    collection_other = Collection.query.filter_by(login_user=usr).all()

    collections = zip(collection_connected, collection_other)

    collections = [(c, o) for c, o in collections if c.in_collection != o.in_collection]

    return render_template("collection/compare.html", **locals())

@@ -517,16 +517,22 @@
 rname%22%5D)
+.all()
 %0A%0A    re
@@ -926,24 +926,25 @@
 if%5B%22value%22%5D%0A
+%0A
         if m
@@ -956,26 +956,23 @@
 cation%5B%22
-collection
+possede
 %22%5D == 1:
@@ -1016,62 +1016,8 @@
  = 0
-%0A        else:%0A            modification%5B%22echange%22%5D = 0
 %0A%0A  
@@ -1073,29 +1073,23 @@
  collec.
-in_collection
+possede
  = modif
@@ -1097,26 +1097,23 @@
 cation%5B%22
-collection
+possede
 %22%5D%0A     
@@ -1177,23 +1177,14 @@
 lec.
-accepte_echange
+ngoods
  = m
@@ -1200,23 +1200,18 @@
 on%5B%22
-echange%22%5D%0A%0A    
+ngoods%22%5D%0A%0A
     
@@ -1230,16 +1230,17 @@
 ommit()%0A
+%0A
     retu
@@ -1280,801 +1280,7 @@
 ute%22
-))%0A%0A%0A@bp.route(%22compare/%3Cstring:usr%3E%22)%0A@user_required%0Adef compare(usr):%0A    username = session%5B%22username%22%5D%0A%0A    if usr == username:%0A        flash(u%22%C3%87a sert %C3%A0 rien de se comparer avec soi-m%C3%AAme %E2%80%A6%22, %22info%22)%0A        return redirect(url_for(%22collection.mine%22))%0A%0A    user = User.query.get(usr)%0A%0A    if user is None:%0A        flash(u%22L%E2%80%99utilisateur saisi n%E2%80%99existe pas%22, %22danger%22)%0A        return redirect(url_for(%22collection.mine%22))%0A%0A    collection_connected = Collection.query.filter_by(login_user=username).all()%0A    collection_other = Collection.query.filter_by(login_user=usr).all()%0A%0A    collections = zip(collection_connected, collection_other)%0A%0A    collections = %5B(c, o) for c, o in collections if c.in_collection != o.in_collection%5D%0A%0A    return render_template(%22collection/compare.html%22, **locals(
 ))%0A

remove comparison feature