#!/usr/bin/python
from flask import Flask, render_template, request, redirect
import os
from pymongo import MongoClient
from bson.objectid import ObjectId
#from json import dumps
import json

def connect():
    connection = MongoClient("10.0.1.10", 27017,connectTimeoutMS=10000)
    #connection = MongoClient("127.0.0.1", 27017,connectTimeoutMS=10000)
    handle = connection["dpTapeDB"]
    handle.authenticate("gui", "GahN7chi")
    return handle

app = Flask(__name__)
handle = connect()


@app.route("/index", methods=['GET'])
@app.route("/", methods=['GET'])
def index():
    #results = [x for x in handle.digilst.find()[0:5]]
    return render_template('index.html')
    #, results=results)

@app.route("/index_old", methods=['GET'])
def index_old():
    results = [x for x in handle.digilst.find()[0:5]]
    return render_template('index_old.html', results=results)

@app.route("/loaddata", methods=['GET'])
def loaddata():
    results = [x for x in handle.digilst.find()[0:100]]
    return render_template('data.json', results=results)

@app.route("/load", methods=['GET'])
def load():
    results = [x for x in handle.digilst.find()[0:10]]
    return json.dumps(results)

@app.route("/insert", methods=['GET'])
def insert():
    return render_template("insert.html")


@app.route("/update", methods=['GET'])
def update():
    return render_template("update.html")


@app.route("/delete", methods=['GET'])
def delete():
    return render_template("delete.html")


@app.route("/write_insert", methods=['POST'])
def write_insert():
    date = request.form.get("date")
    date2 = request.form.get("date2")
    titel = request.form.get("titel")
    languages = request.form.get("languages")
    country = request.form.get("country")
    city = request.form.get("city")
    place = request.form.get("place")
    category = request.form.get("category")
    duration = request.form.get("duration")
    location = request.form.get("location")
    format = request.form.get("format")
    note = request.form.get("note")
    status = request.form.get("status")

    handle.digilst.insert({"Date": date, "Date2": date2,
                           "Titel": titel, "Languages": languages,
                           "Country": country, "City": city, "Place": place,
                           "Category": category, "Duration": duration,
                           "Location": location, "Format": format,
                           "Note": note, "Status": status})
    return render_template("ok.txt")

@app.route("/write_update", methods=['POST'])
def write_update():
    mongoid = ObjectId(request.form.get("mongoid"))
    field = request.form.get("field")
    value = request.form.get("value")
    handle.digilst.update({"_id": mongoid}, {"$set": {field: value}}, upsert=False)
    return render_template("ok.txt")


@app.route("/write_delete", methods=['POST'])
def write_delete():
    mongoid = ObjectId(request.form.get("mongoid"))
    handle.digilst.remove({"_id": mongoid})
    return render_template("ok.txt")


if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))

    app.run(host='0.0.0.0', port=port, debug=True)
