import hashlib
import random
import string

from ext.aboard.model import *

def set_value(token):
    """Randomly create and return a value."""
    value = str(token.user) + "_" + str(token.timestamp)
    len_rand = random.randint(20, 40)
    to_pick = string.digits + string.ascii_letters + \
            "_-+^$"
    for i in range(len_rand):
        value += random.choice(to_pick)
    
    print("Private value", value)
    # Hash the value
    hashed = hashlib.sha512(value.encode())
    value = hashed.hexdigest()
    print("Public value", value)
    return value

class Token(Model):
    
    """A token model."""
    
    id = None
    user = Integer()
    timestamp = Integer()
    value = String(pkey=True, default=set_value)

import hashlib
import random
import string

from ext.aboard.model import *

class Token(Model):
    
    """A token model."""
    
    id = None
    user = Integer()
    timestamp = Integer()
    value = String(pkey=True)
    
    def __init__(self, user=None, timestamp=None):
        value = None
        if user and timestamp:
            value = Token.get_token_value(user, timestamp)
        
        Model.__init__(self, user=user, timestamp=timestamp, value=value)
    
    @staticmethod
    def get_token_value(user, timestamp):
        """Randomly create and return a token value."""
        value = str(user) + "_" + str(timestamp)
        len_rand = random.randint(20, 40)
        to_pick = string.digits + string.ascii_letters + \
                "_-+^$"
        for i in range(len_rand):
            value += random.choice(to_pick)
        
        print("Private value", value)
        
        # Hash the value
        hashed = hashlib.sha512(value.encode())
        value = hashed.hexdigest()
        print("Public value", value)
        return value

Use the Model constructor to generate a default value
[user] Use the Model constructor to generate a default value

  import hashlib
  import random
  import string
  
  from ext.aboard.model import *
- 
- def set_value(token):
-     """Randomly create and return a value."""
-     value = str(token.user) + "_" + str(token.timestamp)
-     len_rand = random.randint(20, 40)
-     to_pick = string.digits + string.ascii_letters + \
-             "_-+^$"
-     for i in range(len_rand):
-         value += random.choice(to_pick)
-     
-     print("Private value", value)
-     # Hash the value
-     hashed = hashlib.sha512(value.encode())
-     value = hashed.hexdigest()
-     print("Public value", value)
-     return value
  
  class Token(Model):
      
      """A token model."""
      
      id = None
      user = Integer()
      timestamp = Integer()
-     value = String(pkey=True, default=set_value)
+     value = String(pkey=True)
+     
+     def __init__(self, user=None, timestamp=None):
+         value = None
+         if user and timestamp:
+             value = Token.get_token_value(user, timestamp)
+         
+         Model.__init__(self, user=user, timestamp=timestamp, value=value)
+     
+     @staticmethod
+     def get_token_value(user, timestamp):
+         """Randomly create and return a token value."""
+         value = str(user) + "_" + str(timestamp)
+         len_rand = random.randint(20, 40)
+         to_pick = string.digits + string.ascii_letters + \
+                 "_-+^$"
+         for i in range(len_rand):
+             value += random.choice(to_pick)
+         
+         print("Private value", value)
+         
+         # Hash the value
+         hashed = hashlib.sha512(value.encode())
+         value = hashed.hexdigest()
+         print("Public value", value)
+         return value
  
Use the Model constructor to generate a default value
## Code Before:
import hashlib
import random
import string

from ext.aboard.model import *

def set_value(token):
    """Randomly create and return a value."""
    value = str(token.user) + "_" + str(token.timestamp)
    len_rand = random.randint(20, 40)
    to_pick = string.digits + string.ascii_letters + \
            "_-+^$"
    for i in range(len_rand):
        value += random.choice(to_pick)
    
    print("Private value", value)
    # Hash the value
    hashed = hashlib.sha512(value.encode())
    value = hashed.hexdigest()
    print("Public value", value)
    return value

class Token(Model):
    
    """A token model."""
    
    id = None
    user = Integer()
    timestamp = Integer()
    value = String(pkey=True, default=set_value)

## Instruction:
Use the Model constructor to generate a default value
## Code After:
import hashlib
import random
import string

from ext.aboard.model import *

class Token(Model):
    
    """A token model."""
    
    id = None
    user = Integer()
    timestamp = Integer()
    value = String(pkey=True)
    
    def __init__(self, user=None, timestamp=None):
        value = None
        if user and timestamp:
            value = Token.get_token_value(user, timestamp)
        
        Model.__init__(self, user=user, timestamp=timestamp, value=value)
    
    @staticmethod
    def get_token_value(user, timestamp):
        """Randomly create and return a token value."""
        value = str(user) + "_" + str(timestamp)
        len_rand = random.randint(20, 40)
        to_pick = string.digits + string.ascii_letters + \
                "_-+^$"
        for i in range(len_rand):
            value += random.choice(to_pick)
        
        print("Private value", value)
        
        # Hash the value
        hashed = hashlib.sha512(value.encode())
        value = hashed.hexdigest()
        print("Public value", value)
        return value
