tilføj understøttelse for mongodb

This commit is contained in:
Christoffer Müller Madsen 2016-09-17 00:59:39 +02:00
parent 88521ef067
commit 789d8ef7a0

View File

@ -1,29 +1,45 @@
#! /usr/bin/env python3
# coding=utf-8
from dialog import Dialog
import json
from pymongo import MongoClient
d = Dialog(dialog="dialog")
d.set_background_title("Klandr")
klandringer = None
with open("klandringer.json", "r") as jsonfile:
contents = jsonfile.read()
if contents == "":
complete_klandringer = {}
else:
complete_klandringer = json.loads(contents)
date = None
mongo = True
file_storage = False
client = MongoClient()
db = client["dat3holdetstime"]
collection = db["klandringer"]
if file_storage == True:
with open("klandringer.json", "r") as jsonfile:
contents = jsonfile.read()
if contents == "":
complete_klandringer = {}
else:
complete_klandringer = json.loads(contents)
#####################
# Begin definitions #
#####################
def menu_select_date():
global klandringer
if complete_klandringer == {}:
date_entries = [()]
else:
date_entries = [(k,"") for k in complete_klandringer]
global date
date_entries = [()]
if file_storage == True:
if complete_klandringer != {}:
date_entries = [(k,"") for k in complete_klandringer]
if mongo == True:
for entry in collection.find():
date_entries.append((entry["date"],""))
date_entries.append(("Ny","Opret en ny dato"))
code, tag = d.menu("Vælg den dato du vil ændre",
title="Vælg dato",
@ -33,7 +49,10 @@ def menu_select_date():
if tag == "Ny":
date = new_date()
klandringer = complete_klandringer[date]
if file_storage == True:
klandringer = complete_klandringer[date]
if mongo == True:
klandringer = collection.find({"date":date})[0]["data"]
menu_main()
def menu_main():
@ -50,8 +69,8 @@ def menu_main():
menu_klandringer()
elif tag == "Afsluttende":
menu_afsluttende()
else:
quit()
else:
quit()
####################
# Begin indledende #
@ -107,6 +126,7 @@ def menu_klandringer():
menu_klandringer()
def menu_select_klandring():
print(klandringer)
klandringer_entries = [(str(i),k["klandrer"] + " v. " + k["klandret"] + " - " + k["title"]) for i,k in enumerate(klandringer)]
code, tag = d.menu("Vælg den klandring du vil ændre/slette",
title="Vælg klandring",
@ -115,6 +135,7 @@ def menu_select_klandring():
return tag
def create_klandring():
print(klandringer)
klandringer.append({"klandrer":"","klandret":"","title":"","description":"","taber":""})
return edit_klandring(-1)
@ -176,8 +197,11 @@ def batch_mode():
break
def save():
with open("klandringer.json","w") as jsonfile:
jsonfile.write(json.dumps(complete_klandringer))
if file_storage == True:
with open("klandringer.json","w") as jsonfile:
jsonfile.write(json.dumps(complete_klandringer))
if mongo == True:
collection.update_one({"date":date},{"$set": {"data":klandringer}})
###################
# End klandringer #
@ -188,8 +212,10 @@ def new_date():
if code == d.OK:
date = str(pickdate[2]).zfill(4) + "-" + str(pickdate[1]).zfill(2) + "-" + str(pickdate[0]).zfill(2)
complete_klandringer[date] = []
save()
if file_storage == True:
complete_klandringer[date] = []
if mongo == True:
collection.insert({"date":date,"data":[]})
return date
else:
menu_select_date()