from model import default
import hashlib

class model(default.model):
	def __init__(self, connection):
		self.connection = connection
		self.objName = 'USERS'

	def encodePassword(self, password):
		return hashlib.md5(password.encode()).hexdigest()

	def create(self, data):
		data['password'] = self.encodePassword(data['password'])
		return super(model, self).create(data)

	def update(self, id, data):
		cursor = self.connection.cursor()
		cursor.execute('SELECT * FROM {0} WHERE(id={1} AND password="{2}");'.format(self.objName, id, data['password']))
		rows = cursor.fetchall()
		if (len(rows) == 0):
			data['password'] = self.encodePassword(data['password'])

		return super(model, self).update(id, data)
