def __init__(self): #try: # os.remove("applications/server/testdatabases") #except: # pass #os.system("cp -r applications/server/databases applications/server/testdatabases") # This approach won't work for GAE because it isn't using SQLite. #self.db = DAL('sqlite://storage.sqlite', folder='applications/server/testdatabases/', # pool_size="10", # migrate_enabled="true", # check_reserved=['all'] # ) self.db = current.db # The database will be empty until we populate it with table structure #filename = "applications/server/models/tables.py" #with open(filename, 'rb+') as tables_code: # exec (tables_code.read()) #print "tables!!", self.db.tables self.test_device_id = "test" auth = Auth(globals(), self.db) self.test_user_email = "*****@*****.**" #auth.user.email access.add_permission(self.test_device_id, self.test_user_email, perm_type="a") self.proc_table = self.db.procedures self.revisions_table = self.db.procedure_revisions
def create_procedure(procedure_name, device_id): """ This function should be called when a new procedure is created in the editor. :param procedure_name: Name of the new procedure :type procedure_name: str :param device_id: Device ID associated with device on which user wants to create proc :type device_id: str :return: ID associated with new procedure (procedure_id in revisions table) :rtype: long """ db = current.db auth = current.auth proc_table = db.procedures user_email = auth.user.email if not _check_name_and_perms_(user_email, device_id, procedure_name): return None pid = proc_table.insert(device_id=device_id, name=procedure_name) access.add_permission(device_id=device_id, user_email=user_email, procedure_id=pid) return pid
def modaladd(): """ Description: Controller for the add page, which lets you add a device into the DB Returns: A form that lets you add things into db.devices (use by including {{=form}}) """ db.device.device_id.writable = False db.device.device_id.readable = False # We don't want to display it here. db.device.user_email.readable = False # We know who we are. form2 = SQLFORM(db.device) form2.custom.widget.name['requires'] = IS_NOT_EMPTY() if form2.process().accepted: device_id = form2.vars.device_id access.add_permission(user_email=auth.user.email, perm_type='a', device_id=device_id) session.flash = "Device added!" redirect(URL('default', 'new_device', args=[form2.vars.id], user_signature=True)) return dict(form2=form2)
def edit_edit(): """"This allows us to edit permissions for a device. We imagine we deal only with the view permission here.""" device_id = request.vars['device_id'] procedure_id = request.vars['procedure_id'] # validate them: user has to be manager. if not access.can_share_procedure(auth.user.email, device_id, procedure_id): raise HTTP(403) # Gets list of users who can view. user_list = db((db.user_permission.device_id == device_id) & (db.user_permission.procedure_id == procedure_id)).select() user_emails = [ u.perm_user_email for u in filter(lambda x: x.perm_type == 'e', user_list) ] # Let's get a nice form for editing this. form = SQLFORM.factory( Field('users', 'list:string', requires=IS_LIST_OF(IS_EMAIL(error_message='must be email!')), default=user_emails)) if form.process().accepted: new_users = set(form.vars.users) if type(form.vars.users) == type( []) else set([form.vars.users]) old_users = set(user_emails) # Delete old permissions of users who can no longer access. for u in old_users - new_users: if u != '': access.delete_permission(device_id=device_id, user_email=u, procedure_id=procedure_id) # Add permissions of users who can newly access. for u in new_users - old_users: if u != '': access.add_permission(device_id, u, 'e', procedure_id=procedure_id) redirect( URL('edit_permission', 'edit_edit', vars={ 'device_id': device_id, 'procedure_id': procedure_id })) return dict(form=form)
def create_new_proc_for_synch(): if request.env.HTTP_HOST.find( 'localhost') == -1 and request.env.HTTP_HOST.find( '127.0.0.1') == -1: raise (HTTP(403)) random.seed(time.time()) name = "new_procedure" + str(random.randint(0, 10000)) db = current.db auth = current.auth test_user_email = auth.user.email logger.info("test_user_email" + str(test_user_email)) access.add_permission(test_device_id, test_user_email, perm_type="a") logger.info("added perm") proc_id = phm.create_procedure(name, test_device_id) logger.info("created proc") phm.save(proc_id, "SOME DATA!", True) logger.info("look for proc_id and name: " + str(proc_id) + " " + str(name))
def modalprocedure(): # Device ID should not be changeable val1 = request.vars['device_id'] if val1 is None: print "val1 is None" else: print "val1 is: " + val1 val = None if val1 is not None: val = db(db.device.id == val1).select()[0].device_id print val try: db.procedures.device_id.default = val except: session.flash = T('No such device') # TODO: Make it so the form itself doesn't add anything into the database form3 = SQLFORM(db.procedures_trash) # form3.vars.name # set the logger logger ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) logger.addHandler(ch) # initialize name variable name = "" # Generate a name to be passed on to add_permission # Only works with device ID specified in URL, try in editor name1 = request.vars.name if name1 is not None: name = name1.replace(" ", "") else: name = name1 print name """ if db(db.device.device_id == val).select(): name = db(db.device.device_id == val).select()[0].name + "_procedure" # db(db.device.device_id == val).select()[0].id """ if auth.is_logged_in() is False: placeholder_email = '*****@*****.**' # to make web2py shut up when you're not logged in else: placeholder_email = auth.user.email access.add_permission(val, placeholder_email, perm_type="a") if form3.process().accepted: print "Procedure Accepted" # create procedure name=demo_1 for the device whose id = 1 and saved it proc_id = proc_harness_module.create_procedure(name, val) print "Procedure values are: " + name proc_harness_module.save( proc_id, "#" + name + " stable edition code\nfrom procedureapi import Procedure\n\nclass DeviceProdecure (Procedure):\n\n def init (self):\n # initialize variables\n # Add schedule. For example:\n # self.api.add_schedule(repeats=10, period_between_runs=86400)\n\n def run (self):\n # Called each time the schedule is triggered", True) time.sleep(1) proc_harness_module.save( proc_id, "#" + name + " temporary edition code\nfrom procedureapi import Procedure\n\nclass DeviceProdecure (Procedure):\n\n def init (self):\n # initialize variables\n # Add schedule. For example:\n # self.api.add_schedule(repeats=10, period_between_runs=86400)\n\n def run (self):\n # Called each time the schedule is triggered", False) # Go back to the home page. session.flash = "Procedure added!" redirect( URL('default', 'test_edit', vars=dict(device_id=val1, procedure_id=proc_id, stable='true'))) return dict(form3=form3)