def logout(self): """Log out, wiping session details. Also, perform periodic maintenance for the server, as this is a good time.""" # First, the logout process. pk = self.id rnc_db.blank_object(self, Session.FIELDS) # ... wipes out any user details, plus the token, so there's no way # this token is being re-used self.id = pk self.save() # Secondly, some other things unrelated to logging out. Users will not # always log out manually. But sometimes they will. So we may as well # do some slow non-critical things: delete_old_sessions() cc_user.delete_old_account_lockouts() cc_user.clear_dummy_login_failures_if_necessary() cc_analytics.send_analytics_if_necessary()
def __init__(self, pk=None, token=None, ip_address=None): """Initialize. Fetch existing session from database, or create a new session. Perform security checks if retrieving an existing session.""" # Fetch-or-create process. Fetching requires a PK and a matching token. pls.db.fetch_object_from_db_by_pk(self, Session.TABLENAME, Session.FIELDS, pk) expiry_if_before = pls.NOW_UTC_NO_TZ - pls.SESSION_TIMEOUT if (self.id is None # couldn't find one... or self.token is None # something went wrong... or self.token != token # token not what we were expecting or self.ip_address != ip_address # from wrong IP address or self.last_activity_utc < expiry_if_before): # expired # new one (meaning new not-logged-in one) for you! rnc_db.blank_object(self, Session.FIELDS) self.__set_defaults() self.token = generate_token() self.ip_address = ip_address self.save() if self.user: self.userobject = cc_user.User(self.user, create_if_not_exists=False) else: self.userobject = None
def manually_erase_record_object_and_save(obj, table, fields, username): """Manually erases a standard record and marks it so erased. The object remains _current, as a placeholder, but its contents are wiped. WRITES TO DATABASE.""" # ------------------------------------------------------------------------- # DELAYED IMPORTS # ------------------------------------------------------------------------- import cc_task if obj._pk is None or obj._era == ERA_NOW: return standard_task_fields = [x["name"] for x in cc_task.STANDARD_TASK_FIELDSPECS] erasure_fields = [ x for x in fields if x not in standard_task_fields ] rnc_db.blank_object(obj, erasure_fields) obj._current = False obj._manually_erased = True obj._manually_erased_at = cc_dt.format_datetime(pls.NOW_LOCAL_TZ, DATEFORMAT.ISO8601) obj._manually_erasing_user = username pls.db.update_object_in_db(obj, table, fields)
def __init__(self, *args, **kwargs): """Initialize. Possible methods: RecipientDefinition() RecipientDefinition(config, section) Args: config: ConfigParser INI file object section: name of recipient and of INI file section """ rnc_db.blank_object(self, RecipientDefinition.FIELDS) # HL7 fields not copied to database self.ping_first = None self.network_timeout_ms = None self.idnum_type_list = [None] * NUMBER_OF_IDNUMS self.idnum_aa_list = [None] * NUMBER_OF_IDNUMS self.keep_message = None self.keep_reply = None # File fields not copied to database (because actual filename stored): self.patient_spec_if_anonymous = None self.patient_spec = None self.filename_spec = None self.make_directory = None # Some default values we never want to be None self.include_anonymous = False # Internal use self.valid = False # Variable constructor... nargs = len(args) if nargs == 0: # dummy one self.type = RECIPIENT_TYPE.FILE self.primary_idnum = 1 self.require_idnum_mandatory = False self.finalized_only = False self.task_format = VALUE.OUTPUTTYPE_XML # File self.include_anonymous = True self.patient_spec_if_anonymous = "anonymous" self.patient_spec = "{surname}_{forename}_{idshortdesc1}{idnum1}" self.filename_spec = ( "/tmp/camcops_debug_testing/" "TestCamCOPS_{patient}_{created}_{tasktype}-{serverpk}" ".{filetype}" ) self.overwrite_files = False self.make_directory = True return elif nargs != 2: raise AssertionError("RecipientDefinition: bad __init__ call") # Standard constructor config = args[0] section = args[1] self.recipient = section try: self.type = get_config_parameter( config, section, "TYPE", str, "hl7") self.primary_idnum = get_config_parameter( config, section, "PRIMARY_IDNUM", int, None) self.require_idnum_mandatory = get_config_parameter_boolean( config, section, "REQUIRE_PRIMARY_IDNUM_MANDATORY_IN_POLICY", True) sd = get_config_parameter( config, section, "START_DATE", str, None) self.start_date = cc_dt.get_date_from_string(sd) ed = get_config_parameter( config, section, "END_DATE", str, None) self.end_date = cc_dt.get_date_from_string(ed) self.finalized_only = get_config_parameter_boolean( config, section, "FINALIZED_ONLY", True) self.task_format = get_config_parameter( config, section, "TASK_FORMAT", str, VALUE.OUTPUTTYPE_PDF) self.xml_field_comments = get_config_parameter_boolean( config, section, "XML_FIELD_COMMENTS", True) # HL7 if self.using_hl7(): self.host = get_config_parameter( config, section, "HOST", str, None) self.port = get_config_parameter( config, section, "PORT", int, DEFAULT_HL7_PORT) self.ping_first = get_config_parameter_boolean( config, section, "PING_FIRST", True) self.network_timeout_ms = get_config_parameter( config, section, "NETWORK_TIMEOUT_MS", int, 10000) for n in range(1, NUMBER_OF_IDNUMS + 1): i = n - 1 nstr = str(n) self.idnum_type_list[i] = get_config_parameter( config, section, "IDNUM_TYPE_" + nstr, str, "") self.idnum_aa_list[i] = get_config_parameter( config, section, "IDNUM_AA_" + nstr, str, "") self.keep_message = get_config_parameter_boolean( config, section, "KEEP_MESSAGE", False) self.keep_reply = get_config_parameter_boolean( config, section, "KEEP_REPLY", False) self.divert_to_file = get_config_parameter( config, section, "DIVERT_TO_FILE", str, None) self.treat_diverted_as_sent = get_config_parameter_boolean( config, section, "TREAT_DIVERTED_AS_SENT", False) if self.divert_to_file: self.host = None self.port = None self.ping_first = None self.network_timeout_ms = None self.keep_reply = None self.include_anonymous = False # File if self.using_file(): self.include_anonymous = get_config_parameter_boolean( config, section, "INCLUDE_ANONYMOUS", False) self.patient_spec_if_anonymous = get_config_parameter( config, section, "PATIENT_SPEC_IF_ANONYMOUS", str, "anonymous") self.patient_spec = get_config_parameter( config, section, "PATIENT_SPEC", str, None) self.filename_spec = get_config_parameter( config, section, "FILENAME_SPEC", str, None) self.overwrite_files = get_config_parameter_boolean( config, section, "OVERWRITE_FILES", False) self.make_directory = get_config_parameter_boolean( config, section, "MAKE_DIRECTORY", False) self.rio_metadata = get_config_parameter_boolean( config, section, "RIO_METADATA", False) self.rio_idnum = get_config_parameter( config, section, "RIO_IDNUM", int, None) self.rio_uploading_user = get_config_parameter( config, section, "RIO_UPLOADING_USER", str, None) self.rio_document_type = get_config_parameter( config, section, "RIO_DOCUMENT_TYPE", str, None) self.script_after_file_export = get_config_parameter( config, section, "SCRIPT_AFTER_FILE_EXPORT", str, None) self.check_valid() except ConfigParser.NoSectionError: logger.warning("Config file section missing: [{}]".format( section )) self.valid = False