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,12 +1,23 @@
#! /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
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: with open("klandringer.json", "r") as jsonfile:
contents = jsonfile.read() contents = jsonfile.read()
if contents == "": if contents == "":
@ -14,16 +25,21 @@ with open("klandringer.json", "r") as jsonfile:
else: else:
complete_klandringer = json.loads(contents) complete_klandringer = json.loads(contents)
##################### #####################
# Begin definitions # # Begin definitions #
##################### #####################
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:
if complete_klandringer != {}:
date_entries = [(k,"") for k in 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()
if file_storage == True:
klandringer = complete_klandringer[date] klandringer = complete_klandringer[date]
if mongo == True:
klandringer = collection.find({"date":date})[0]["data"]
menu_main() menu_main()
def menu_main(): def menu_main():
@ -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():
if file_storage == True:
with open("klandringer.json","w") as jsonfile: with open("klandringer.json","w") as jsonfile:
jsonfile.write(json.dumps(complete_klandringer)) 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)
if file_storage == True:
complete_klandringer[date] = [] complete_klandringer[date] = []
save() if mongo == True:
collection.insert({"date":date,"data":[]})
return date return date
else: else:
menu_select_date() menu_select_date()