def set_findlock_lock_state( #using device_uuid, new_lock_state, #mEventType auth_value, # auth_type): #userentries if not v.verify_uuid(device_uuid).success: return m_LogicReturn.f_error_msg( f"{device_uuid} is not a valid uuid!") event_types = t.get_types(m_EventTypes()) if new_lock_state not in event_types: return m_LogicReturn.f_error_msg( f"{new_lock_state} is not a valid event type!") auth_types = t.get_types(m_UserEntries()) if auth_type not in auth_types: return m_LogicReturn.f_error_msg( f"{auth_type} is not a valid userentry!") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, device_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _findlock = db_resp.addon_data _findlock: m_Findlock db_resp = db_User().get_all_users() if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _users = db_resp.addon_data user_have_rights = False if auth_type == m_UserEntries.finger_id: for user in _users: if user.finger_id == auth_value: if user.user_uuid == _findlock.FindlockPermission.master_uuid: user_have_rights = True if user.user_uuid in _findlock.FindlockPermission.allowed_uuids: user_have_rights = True elif auth_type == m_UserEntries.nfc_id: for user in _users: if user.nfc_id == auth_value: if user.user_uuid == _findlock.FindlockPermission.master_uuid: user_have_rights = True if user.user_uuid in _findlock.FindlockPermission.allowed_uuids: user_have_rights = True else: return m_LogicReturn.f_error_msg("Unsupported auth type!") if not user_have_rights: return m_LogicReturn.f_error_msg("Permission error!") return RestLogic.set_findlock_event(device_uuid, new_lock_state)
def update_findlock(device_uuid, update_type, update_data) -> m_LogicReturn: if v.value_empty(device_uuid) \ or v.value_empty(update_data) \ or v.value_empty(update_type): return m_LogicReturn.f_error_msg("post data is missing!") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, device_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) Findlock = db_resp.addon_data Findlock: m_Findlock valid_update_types = t.get_types(m_FindlockUpdateTypes()) if update_type not in valid_update_types: return m_LogicReturn.f_error_msg("Unsupported update type!") if update_type == m_FindlockUpdateTypes.gps_location: try: update_data = json.loads(update_data) update_data = m_GpsLocation.fromDict(update_data) except: return m_LogicReturn.f_error_msg("Update data is malformed!") if update_type == m_FindlockUpdateTypes.gps_location: Findlock.GpsLocationHistory.add_location(update_data) else: return m_LogicReturn.f_error_msg( "this update type is not implemented...") db_resp = db_Findlock().update_findlock(Findlock) return m_LogicReturn.f_success_msg("Findlock updated!")
def get_admin_email(to_addr, voornaam, subject, content): message = MIMEMultipart("alternative") message["Subject"] = subject message["From"] = config.get_email_config().sender_mail message["To"] = to_addr text = """\ Hoi {voornaam}, {content} (Bericht verstuurd via Admin Panel) Met vriendelijke groet, Het Findlock Team """.format(content=content, voornaam=voornaam) html = Tools.read_txt_to_str( "filoModules/Emails/admin_mail.html").replace( "<!voornaam!>", voornaam).replace("<!content!>", content) part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") message.attach(part1) message.attach(part2) return message
def google_register(google_user_id, user_email, avatar_link) -> m_LogicReturn: if v.value_empty(google_user_id) \ or v.value_empty(user_email): return m_LogicReturn.f_error_msg("post data is missing!") if not v.verify_email(user_email): return m_LogicReturn.f_error_msg("E-mail is not valid!") _User = m_User( first_name=None, surname=None, email=user_email, password_hash=None, phone_number=None, nfc_id=None, finger_id=None, friend_code=Tools.generate_friend_code(), google_user_id=google_user_id, avatar_link=avatar_link, verified=True ) db_resp = db_User().create_user(_User) if db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) return m_LogicReturn.f_success_msg(db_resp.content)
def get_register_email(to_addr, auth_code): message = MIMEMultipart("alternative") message["Subject"] = "Findlock - Verify" message["From"] = config.get_email_config().sender_mail message["To"] = to_addr hostname = config.get_hostname() text = """\ Hoi, Welkom bij Findlock! Bedankt voor uw aanmelding bij Findlock. Om veiligheidsredenen vragen wij u uw aanmelding compleet te maken door op de onderstaande linkt te klikken. Uw account wordt dan geactiveerd en u kunt meteen gebruik gaan maken van Findlock. Aanmelding Bevestigen https://{hostname}/auth/verify/{auth_code} Met vriendelijke groet, Het Findlock Team """.format(auth_code=auth_code, hostname=hostname) html = Tools.read_txt_to_str( "filoModules/Emails/register_mail.html").replace( "<!hostname!>", hostname).replace("<!auth_code!>", auth_code) part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") message.attach(part1) message.attach(part2) return message
def read_from_line(start_line): f = open(log_file, 'r') lines = f.readlines() for i in range(start_line, len(lines)): start_line += 1 width = Tools.get_terminal_size()[0] print(lines[i].strip()[:width]) return start_line
def set_findlock_event(device_uuid, event_type) -> m_LogicReturn: #using valid_event_types = t.get_types(m_EventTypes()) print(valid_event_types) if event_type not in valid_event_types: return m_LogicReturn.f_error_msg( f"{event_type} is not a valid event type!") if not v.verify_uuid(device_uuid).success: return m_LogicReturn.f_error_msg( f"{device_uuid} is not a valid uuid") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, device_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _findlock = db_resp.addon_data _findlock: m_Findlock _findlock.Event = m_Event(event_type, t.get_unix_time()) db_resp = db_Findlock().update_findlock(_findlock) return m_LogicReturn.f_success_msg("Event updated!")
def add_gps_location_to_findlock(fl_uuid, lat, lng) -> m_LogicReturn: db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, fl_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _Findlock = db_resp.addon_data _Findlock: m_Findlock _new_gps_location = m_GpsLocation(lat, lng, str(Tools.get_unix_time())) _Findlock.GpsLocationHistory.add_location(_new_gps_location) db_Findlock().update_findlock(_Findlock) return m_LogicReturn.f_success_msg("ok!")
def facebook_register(name, userid) -> m_LogicReturn: if v.value_empty(name) or v.value_empty(userid): return m_LogicReturn.f_error_msg("data is missing!") _User = m_User( first_name=str(name).split(" ")[0], surname=str(name).split(" ")[1], email=None, password_hash=None, phone_number=None, nfc_id=None, finger_id=None, friend_code=Tools.generate_friend_code(), facebook_user_id=userid, verified=True ) db_resp = db_User().create_user(_User) if db_resp.success: return m_LogicReturn.f_success_msg(db_resp.content) return m_LogicReturn.f_error_msg(db_resp.content)
def findlock_update(findlock_uuid, data_keys, data_values) -> m_LogicReturn: if not v.verify_uuid(findlock_uuid).success: return m_LogicReturn.f_error_msg( f"{findlock_uuid} is not a valid uuid") if len(data_keys) != len(data_values): return m_LogicReturn.f_error_msg("Mismatch in data!") for i in range(0, len(data_values)): data_val = data_values[i] if data_val == "None": data_values[i] = None elif data_val == "True": data_values[i] = True elif data_val == "False": data_values[i] = False valid_data_keys = Tools.get_types(m_FindlockEntries()) db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, findlock_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _findlock = db_resp.addon_data _findlock: m_Findlock _findlock_dict = _findlock.toDict() for i in range(0, len(data_keys)): key = data_keys[i] if key not in valid_data_keys: return m_LogicReturn.f_error_msg(f"{key} is not a valid key!") val = data_values[i] _findlock_dict[key] = val _findlock = m_Findlock.fromDict(_findlock_dict) db_resp = db_Findlock().update_findlock(_findlock) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) return m_LogicReturn.f_success_msg(db_resp.content)
def register(user_email, user_password, user_password2) -> m_LogicReturn: if v.value_empty(user_email) \ or v.value_empty(user_password) \ or v.value_empty(user_password2): return m_LogicReturn.f_error_msg("post data is missing") v_pass = v.verify_password(user_password) if not v_pass.success: return m_LogicReturn.f_error_msg(v_pass.content) if not v.verify_email(user_email): return m_LogicReturn.f_error_msg("E-mail is invalid") if user_password != user_password2: return m_LogicReturn.f_error_msg("Passwords are not the same!") password_hash = Encryption.encrypt_password( user_password) _User = m_User( first_name=None, surname=None, email=user_email, password_hash=password_hash, phone_number=None, nfc_id=None, finger_id=None, friend_code=Tools.generate_friend_code(), avatar_link="https://www.clevelanddentalhc.com/wp-content/uploads/2018/03/sample-avatar-300x300.jpg" ) db_resp = db_User().create_user(_User) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) token_code = Token.generate_confirm_token(user_email) print(f"Token code: {token_code}") email_message = Email.get_register_email(user_email, token_code) email_resp = Email.send_email(user_email, email_message) if not email_resp.success: return m_LogicReturn.f_error_msg("Failed to send email!") return m_LogicReturn.f_success_msg(db_resp.content)
def out(self, log_type: DebugTypes, content): if log_type not in self.allowed_debug_types or not self.enabled: return content_plain = content if log_type == DebugTypes.ERROR: content_plain = f"{content}" content = f"{colorama.Fore.RED}{content}{colorama.Fore.RESET}" if log_type == DebugTypes.WARNING: content_plain = f"{content}" content = f"{colorama.Fore.YELLOW}{content}{colorama.Fore.RESET}" log_type = log_type.name stack = traceback.extract_stack(None, 3)[0] code_file = Tools.find_between_r(stack[0], "\\", ".py") code_line = stack[1] code_func = stack[2] str_to_print = f"{colorama.Fore.YELLOW}[0] {colorama.Fore.GREEN}<{code_file}/{code_func}:{code_line}> {colorama.Fore.CYAN}({log_type}): {colorama.Fore.RESET}{str(content)}" str_to_print_plain = f"[0] <{code_file}/{code_func}:{code_line}> ({log_type}): {content_plain}" self.append_to_log_file(str_to_print_plain) print(str_to_print_plain)
def user_update(user_uuid, data_keys, data_values) -> m_LogicReturn: if not v.verify_uuid(user_uuid).success: return m_LogicReturn.f_error_msg( f"{user_uuid} is not a valid uuid") if len(data_keys) != len(data_values): return m_LogicReturn.f_error_msg("Mismatch in data!") for i in range(0, len(data_values)): data_val = data_values[i] if data_val == "None": data_values[i] = None elif data_val == "True": data_values[i] = True elif data_val == "False": data_values[i] = False valid_data_keys = Tools.get_types(m_UserEntries()) db_resp = db_User().get_user(m_UserEntries.user_uuid, user_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _user = db_resp.addon_data _user: m_User _user_dict = _user.toDict() for i in range(0, len(data_keys)): key = data_keys[i] if key not in valid_data_keys: return m_LogicReturn.f_error_msg(f"{key} is not a valid key!") val = data_values[i] _user_dict[key] = val _user = m_User.fromDict(_user_dict) db_resp = db_User().update_user(_user) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) return m_LogicReturn.f_success_msg(db_resp.content)
def get_all_user_entries() -> m_LogicReturn: return m_LogicReturn.f_success("Returned list of entries", Tools.get_types(m_UserEntries()))
def get_all_findlock_entries() -> m_LogicReturn: return m_LogicReturn.f_success("Returned list of entries", Tools.get_types(m_FindlockEntries()))