listfix/db.py (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import json
from os.path import exists
class DB:
def __init__(self, path):
self.json = {}
self.path = path
if (exists(path)):
with open(path) as f:
self.json = json.load(f)
def check_list_exists(self, list_email):
if (list_email in self.json.keys()):
return True
else:
return False
def create_list(self, list_email, list_name):
if (self.check_list_exists(list_email)):
raise ValueError(f"Email list already exists: {list_email}")
self.json[list_email] = { 'name': list_name, 'recipients': {} }
def get_lists(self):
return list(self.json.keys())
def get_list_name(self, list_email):
if (not self.check_list_exists(list_email)):
raise ValueError(f"Email list does not exist: {list_email}")
return self.json[list_email]['name']
def check_recipient_exists(self, list_email, recipient_email):
if (not self.check_list_exists(list_email)):
raise ValueError(f"Email list does not exist: {list_email}")
if (recipient_email in self.json[list_email]['recipients'].keys()):
return True
else:
return False
def create_recipient(self, list_email, recipient_email, recipient_name):
if (self.check_recipient_exists(list_email, recipient_email)):
raise ValueError(f"Recipient already exists: {recipient_email}")
self.json[list_email]['recipients'][recipient_email] = { 'name': recipient_name }
def get_list_recipients(self, list_email):
if (not self.check_list_exists(list_email)):
raise ValueError(f"Email list does not exist: {list_email}")
return list(self.json[list_email]['recipients'].keys())
def get_recipient_name(self, list_email, recipient_email):
if (not self.check_list_exists(list_email)):
raise ValueError(f"Email list does not exist: {list_email}")
if (not self.check_recipient_exists(list_email, recipient_email)):
raise ValueError(f"Recipient does not exist: {recipient_email}")
return self.json[list_email]['recipients'][recipient_email]['name']
def destroy_recipient(self, list_email, recipient_email):
if (not self.check_list_exists(list_email)):
raise ValueError(f"Email list does not exist: {list_email}")
if (not self.check_recipient_exists(list_email, recipient_email)):
raise ValueError(f"Recipient does not exist: {recipient_email}")
del self.json[list_email]['recipients'][recipient_email]
def destroy_list(self, list_email):
if (not self.check_list_exists(list_email)):
raise ValueError(f"Email list does not exist: {list_email}")
del self.json[list_email]
def lower_emails(self):
lists = self.get_lists()
for x in lists:
self.json[x.lower()] = self.json.pop(x)
lists = self.get_lists()
for x in lists:
recipients = self.get_list_recipients(x)
for y in recipients:
self.json[x]['recipients'][y.lower()] = self.json[x]['recipients'].pop(y)
def save(self):
f = open(self.path, "w")
json.dump(self.json, f, indent=4)
f.close()
|