tilføj understøttelse for mongodb
This commit is contained in:
parent
88521ef067
commit
789d8ef7a0
36
klandr.py
36
klandr.py
|
@ -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:
|
||||
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 == {}:
|
||||
global date
|
||||
date_entries = [()]
|
||||
else:
|
||||
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()
|
||||
|
||||
if file_storage == True:
|
||||
klandringer = complete_klandringer[date]
|
||||
if mongo == True:
|
||||
klandringer = collection.find({"date":date})[0]["data"]
|
||||
menu_main()
|
||||
|
||||
def menu_main():
|
||||
|
@ -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():
|
||||
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)
|
||||
if file_storage == True:
|
||||
complete_klandringer[date] = []
|
||||
save()
|
||||
if mongo == True:
|
||||
collection.insert({"date":date,"data":[]})
|
||||
return date
|
||||
else:
|
||||
menu_select_date()
|
||||
|
|
Loading…
Reference in New Issue
Block a user