Browse Source

Add hashing functions to quickly check if the objects have changed

master
Rick 6 years ago
parent
commit
407554dfea
4 changed files with 63 additions and 5 deletions
  1. +16
    -1
      admidio.py
  2. +19
    -1
      event.py
  3. +11
    -0
      group.py
  4. +17
    -3
      member.py

+ 16
- 1
admidio.py View File

@ -1,4 +1,5 @@
import MySQLdb
import hashlib
from .member import Member as member
from .group import Group as group
@ -9,7 +10,7 @@ from dateutil.relativedelta import relativedelta
class Admidio:
"""Creates the Admidio Class"""
"""Admidio Class"""
def __init__(self, mysql_host, mysql_user, mysql_pass, mysql_db,
db_prefix):
@ -48,6 +49,20 @@ class Admidio:
def __del__(self):
pass
def __eq__(self, other):
if isinstance(other, Admidio):
return self.hash() == other.hash()
def hash(self):
s = ''
for _, x in self.members.items():
s += x.hash()
for _, x in self.groups.items():
s += x.hash()
for _, x in self.events.items():
s += x.hash()
return hashlib.md5(s.encode('utf-8')).hexdigest()
def connect_db(self):
self.db = MySQLdb.connect(
host=self.mysql_host,


+ 19
- 1
event.py View File

@ -1,4 +1,5 @@
from .exceptions import MemberNotFoundException
import hashlib
class Event():
@ -45,7 +46,24 @@ class Event():
except MemberNotFoundException:
print(
f"Member with id {row[0]} not found, group id: {self.id}")
"Member with id {row[0]} not found, group id:" +
f"{self.id}")
def __eq__(self, other):
if isinstance(other, Event):
return self.hash() == other.hash()
def hash(self):
return hashlib.md5((str(self.rol_id)
+ self.start_time.strftime("%Y-%m-%d %H:%M:%S")
+ self.end_time.strftime("%Y-%m-%d %H:%M:%S")
+ str(self.id)
+ (''.join(str(y)
for x, y in self.participants) or '')
+ (''.join(str(y) for x, y in self.leaders) or '')
+ self.name + (self.description or '') +
(self.location or '')
+ str(self.cat_id)).encode('utf-8')).hexdigest()
def getAllMembers(self):
"""Return leaders and members in group, combined"""


+ 11
- 0
group.py View File

@ -1,4 +1,5 @@
from .exceptions import MemberNotFoundException
import hashlib
class Group:
@ -25,6 +26,16 @@ class Group:
print(
f"Member with id {row[0]} not found, group id: {self.id}")
def __eq__(self, other):
if isinstance(other, Group):
return self.hash() == other.hash()
def hash(self):
return hashlib.md5((str(self.id) + self.name
+ (''.join(str(x) for x in self.members) or '')
+ (''.join(str(x) for x in self.leaders) or '')
).encode('utf-8')).hexdigest()
def getAllMembers(self):
"""Return leaders and members in group, combined"""
return self.leaders.append(self.members)

+ 17
- 3
member.py View File

@ -1,4 +1,5 @@
import pprint
import hashlib
class Member():
@ -19,9 +20,22 @@ class Member():
# makes dict with {usf_name_intern:usd_value}
self.userdata[userfields[row[0]]] = row[1]
def __hash__(self):
"""Not sure if needed"""
return hash((self.user_id, self.username))
def __eq__(self, other):
if isinstance(other, Member):
return self.hash() == other.hash()
def hash(self):
if self.password:
return hashlib.md5((str(self.user_id)
+ self.username
+ self.password
+ str(self.userdata)
).encode('utf-8')).hexdigest()
else:
return hashlib.md5((str(self.user_id)
+ self.username
+ str(self.userdata)
).encode('utf-8')).hexdigest()
def __str__(self):
return (f"""Userid: {self.user_id}, Username: {self.username}, """


Loading…
Cancel
Save