Example #1
0
 def get_username_from_system_user_id(self, envid, system_user_id):
     # pylint: disable=invalid-name
     profile = UserProfile.get_by_system_user_id(envid, system_user_id)
     if profile:
         return profile.display_name()
     else:
         # FIXME: probably not correct.
         return UNDEFINED
Example #2
0
    def _eventgen(self, key, agent, entry, body=None):
        if body is None:
            body = {}
        body = dict(body.items() +\
                        agent.todict().items() +\
                        entry.todict().items())

        body['http_status'] = responses[entry.status]

        system_user_id = entry.system_user_id
        if system_user_id != -1:
            profile = UserProfile.get_by_system_user_id(
                entry.envid, entry.system_user_id)
            if profile:
                # This is the browser/viewer user, and not the
                # workbook publisher/owner.
                body['username'] = profile.display_name()

        # An event in the event_control table expects these to exist,
        # but if workbook archiving is off, or the workbook isn't
        # found they will not be set.
        body['owner'] = None
        body['workbook'] = None

        body['userid'] = None  # default

        if 'repository_url' in body:
            self._translate_workbook(body, entry)

        if 'http_referer' in body:
            body['http_referer'] = unquote(body['http_referer']).decode('utf8')

        if 'http_request_uri' in body:
            # Remove query string from the http_request_uri.
            urip = urlparse(body['http_request_uri'])
            http_request_uri = urip.scheme + "://" + urip.netloc + urip.path

            http_request_uri = unquote(http_request_uri)
            body['http_request_uri'] = http_request_uri.decode('utf8')

        userid = body['userid']

        completed_at = utc2local(entry.completed_at)
        self.server.event_control.gen(key,
                                      body,
                                      userid=userid,
                                      timestamp=completed_at)
Example #3
0
    def _eventgen(self, key, data, entry):
        """Fill in information into the data dictionary and send the event."""
        # Placeholder to be set by the next steps.
        entry.system_user_id = -1

        item_entry = self._add_info(entry)

        if key == EventControl.EXTRACT_OK:
            if item_entry:
                self.server.extract_archive.add(item_entry, entry)
            else:
                logger.info("extract OK: Can't archive workbook/datasource "
                            "row  - not found.")

        data = dict(data.items() + entry.todict().items())

        if entry.completed_at is not None and entry.started_at is not None:
            duration = timedelta_total_seconds(entry.completed_at,
                                               entry.started_at)
            data['duration'] = duration
            data['duration_hms'] = to_hhmmss(duration)
            timestamp = utc2local(entry.completed_at)
        else:
            timestamp = None

        envid = self.server.environment.envid
        profile = UserProfile.get_by_system_user_id(envid,
                                                    data['system_user_id'])
        if profile:
            data['username'] = profile.display_name()
            userid = profile.userid
        else:
            data['username'] = "******"
            userid = None

        self.server.event_control.gen(key,
                                      data,
                                      userid=userid,
                                      site_id=data['site_id'],
                                      timestamp=timestamp)
Example #4
0
    def _translate_workbook(self, body, entry):
        envid = self.server.environment.envid

        url = body['repository_url']
        try:
            workbook = WorkbookEntry.get_by_url(envid,
                                                url,
                                                entry.site_id,
                                                default=None)
        except MultipleResultsFound:
            logger.warning("Multiple rows found for url '%s', site_id: '%s'",
                           url, entry.site_id)
            body['workbook'] = 'Unknown: Duplicate repository url'
            body['owner'] = 'Unknown: Duplicate repository url'
            return

        if not workbook:
            logger.warning("repository_url '%s' Not Found.", url)
            return
        body['workbook'] = workbook.name

        # Send this userid on event generation (workbook owner, not viewer).
        user = UserProfile.get_by_system_user_id(envid,
                                                 workbook.system_user_id)
        if user:
            body['userid'] = user.userid
            body['owner'] = user.display_name()
        else:
            logger.warning("system user '%d' Not Found.",
                           workbook.system_user_id)
        body['project'] = workbook.project

        if workbook.site:
            body['site'] = workbook.site
        else:
            if entry.site_id:
                site = Site.get(entry.envid, entry.site_id)
                if site:
                    body['site'] = site.name
Example #5
0
    def sendevent(self, key, system_user_id, error, data):
        """Send the event."""

        if 'embedded' in data:
            del data['embedded']
        if error:
            logger.error(error)
            data['error'] = error

        profile = UserProfile.get_by_system_user_id(
            self.server.environment.envid, system_user_id)

        if profile:
            username = profile.display_name()
            userid = profile.userid
        else:
            username = None
            userid = None

        if username and not 'owner' in data:
            data['owner'] = username

        return self.server.event_control.gen(key, data, userid=userid)