class Invitation: # Arguments # Email # Expiration Date: example 1 month 13 days 1 hour 16 mins 23 sec# active# Return# A jSon array of all invitation information# Invi.forge().Generate("example@domain.com", "2 day", True) #generate an invitation code from email and timestamp to milliseconds def generate(email, expire, active): if checkEmail=email now = strtotime(self.sql_timestamp()) format = 'MMMM Do YYYY, h:mm:ss a' expiration = datetime.date(format, strtotime('+ '.expire, now)) code = self.hash_split(hash('bycrypt', email)).self.hash_split(hash('bcrypt', time())) newInvite = Invitation(code = code, email = email, expiration = expiration, active = active, used = 0, created_at = self.sql_timestamp(), updated_at = self.sql_timestamp() ) db.session.add(newInvite) return json_encode(newInvite) else : return "This email address has an invitation." # example JSON output# { #"code": "f22c5973ebbcca99", #"email": "example@domain.com", #"expiration": "2013-05-10 15:58:41023 ", #"active": true, #"used": "0", #"created_at": "2013-05-06 15:58:41156", #"updated_at": "2013-05-06 15:58:41327"} #activate an invitation when a loggedin registered user sends an invitation to a contact an invitation will be active# Arguments: #invitation code# email def active(code, email): "" "Return ``True`` if the invitation is still valid, ``False`` otherwise. "" " return now() < self.expiration #DB::connection() - > table('invite') - > where('code', '=', code) - > where('email', '=', email) - > update(array('active' => True)) # deactivate an invitation# Arguments: #invitation code# email def deactive(code, email): "" "Filter invalid invitation." "" expiration = now() - datetime.timedelta(app_settings.EXPIRE_DAYS) return self.get_query_set().filter(date_invited = expiration) #DB::connection() - > table('invitation') - > where('code', '=', code) - > where('email', '=', email) - > update(array('active' => False)) # make an invitation used # Arguments: #invitation code # email def used(code, email): DB::connection() - > table('invitation') - > where('code', '=', code).where('email', '=', email) - > update(array('used' = True)) # make an invitation unused # Arguments: #invitation code # email def unuse(code, email): #DB::connection() - > table('invitation') - > where('code', '=', code).where('email', '=', email) - > update(array('used' = False)) #return invitation code status #Arguments # invitation code # email # Return: #Status: Active, Deactive, Used, Valid, Not Exist def status(code, email): temp = #DB::connection() - > table('invitation') - > where('code', '=', code).where('email', '=', email) - > first() if (temp) if (!temp - > active) return "deactive" else if (temp - > used) return "used" else if (self.sql_timestamp() > temp.expiration) return "expired" else : return "valid" else : return "not exist" #validate invitation code # Arguments: #invitation code # email # Return: #Boolean def check(code, email): temp = #DB::connection() - > table('invitation') - > where('code', '=', code) - > where('email', '=', email) - > first() if (temp) if (!temp - > active or temp - > used or self.sql_timestamp() > temp - > expiration) return False else : return True else : return False # delete an invitation# Arguments: #invitation code# email def delete(self, code, email): "" "Removes expired instances of ``Invitation``. Invitation keys to be deleted are identified by searching for instances of `` Invitation `` with difference between now and `date_invited` date greater than `` EXPIRE_DAYS ``. It is recommended that this method be executed regularly as part of your routine site maintenance; this application provides a custom management command which will call this method, accessible as `` manage.py cleanupinvitations ``. "" " return self.invalid().delete() temp = #DB::connection() - > table('invitation') - > where('code', '=', code) - > where('email', '=', email) - > delete(); #Check is the email has been used def checkEmail(email): temp = #DB::connection() - > table('invitation') - > where('email', '=', email) - > first() if (temp) return False else return True # Split the email time stamp hash down to 16 charaters def hash_split(hash): output = str_split(hash, 8) return output[rand(0, 1)] # Get timestamp def sql_timestamp(): return date(#DB::connection() - > grammar() - > grammar - > datetime)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question