from tinydb import TinyDB, Query

class TinyDBConnector:
    def __init__(self, database_file):
        self.userdb = TinyDB(database_file)
        pass

    def is_allowed(self, rfid):
        User = Query()
        rfid_match = False
        rfid_contact_found = None
        rfid_contacts = self.userdb.search(User['custom_fields'] != None)
        #TODO: error handling
        for rfid_contact in rfid_contacts:
            for custom_field in rfid_contact['custom_fields']:
                if custom_field['title'] == 'RFID Tag':
                    if custom_field['value'] == str(rfid):
                        rfid_match = True
                        rfid_contact_found = rfid_contact
                    #print "Tag id: " + custom_field['value'] + " name: " + rfid_contact['first_name'] + " " + rfid_contact['last_name'] + " Tag state: " + rfid_contact['membership_state']

        if rfid_match is True:
            if rfid_contact_found['membership_state'] == 'activated':
                return (rfid_contact_found, True)
            else:
                # Check for temp member field (temp hack..)
                for custom_field in rfid_contact['custom_fields']:
                    if custom_field['title'] == 'RFID Tag':
                        if custom_field['value'] == 'true':
                            return (rfid_contact_found, True)

                return (rfid_contact_found, False)
        else:
            return (None, False)
@@ -925,138 +925,28 @@
     
-if rfid_contact_found%5B'membership_state'%5D == 'activated':%0A                return (rfid_contact_found, True)%0A            else:%0A    
+temp_member = False%0A
     
@@ -1001,36 +1001,32 @@
 ..)%0A            
-    
 for custom_field
@@ -1035,33 +1035,32 @@
 n rfid_contact%5B'
-c
 ustom_fields'%5D:%0A
@@ -1067,36 +1067,32 @@
                 
- 
-   
 if custom_field%5B
@@ -1107,36 +1107,32 @@
  == 'RFID Tag':%0A
-    
                 
@@ -1171,32 +1171,297 @@
 'true':%0A        
+                temp_member = True%0A                        print %22Temp member allowed%22%0A            if rfid_contact_found%5B'membership_state'%5D == 'activated':%0A                return (rfid_contact_found, True)%0A            else:%0A                if temp_member is True:%0A
                 

Test temp member..