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