Ejemplo n.º 1
0
 def handle(self, *args, **options):
     HUBSTAFF_APP_TOKEN = "S-HAXY_8ZU996f1xGEX-OATcWaAwb51HqlnwN6oi4vU"
     hubstaff = HubstaffClient(app_token=HUBSTAFF_APP_TOKEN,
                               username='******',
                               password='******')
     os.environ['HUBSTAFF_AUTH_TOKEN'] = hubstaff.authenticate()
     hubstaff = HubstaffClient(app_token=HUBSTAFF_APP_TOKEN,
                               auth_token=os.getenv('HUBSTAFF_AUTH_TOKEN'))
     hubstaff.authenticate()
     users_list = hubstaff.get_users_list(include_projects=True,
                                          include_organizations=True)
     ids = map(lambda user: (user['id'], user['email']), users_list)
     for user_id in ids:
         try:
             user = CustomUser.objects.get(email=user_id[1])
             HubstaffUser.objects.get_or_create(user=user,
                                                hubstaff_id=user_id[0])
         except Exception as e:
             print("user not found")
Ejemplo n.º 2
0
class Command:
    def __init__(self, **opts):
        self._logger = logging.getLogger(__name__)
        self._logger.setLevel(logging.WARNING)
        self._config = Config(**opts)
        self._hubstaff = None

    def _load_config(self):
        self._config.load()

    def _init_client(self):
        self._hubstaff = HubstaffClient(
            app_token=self._config.hubstaff_app_token,
            auth_token=self._config.hubstaff_auth_token,
            username=self._config.hubstaff_username,
            password=self._config.hubstaff_password)
        # set given auth_token to the config
        self._config.hubstaff_auth_token = self._hubstaff.authenticate()

    def _save_config(self):
        self._config.save()

    def _get_report_data(self, date_from, date_to):
        users_list = self._hubstaff.get_users_list(include_projects=True)

        users_dict = {}
        projects_dict = {}
        for user_item in users_list:
            users_dict[user_item['id']] = {
                'id': user_item['id'],
                'name': user_item['name'],
            }
            for project_item in user_item['projects']:
                projects_dict[project_item['id']] = {
                    'id': project_item['id'],
                    'name': project_item['name'],
                }

        activities_list = self._hubstaff.get_activities_list(
            date_from, date_to)

        spent_time_dict = defaultdict(lambda: 0)
        for activity_item in activities_list:
            spent_time_dict[(
                activity_item['user_id'],
                activity_item['project_id'])] += activity_item['tracked']

        report_data = {
            'date_from': date_from,
            'date_to': date_to,
            'users': users_dict,
            'projects': projects_dict,
            'spent_time': spent_time_dict,
        }
        return report_data

    @classmethod
    def _render_report_to_html(cls, data):
        template = jinja2.Template('''<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>rt-bot-34 report {{ date_from }} - {{ date_to }}</title>
  </head>
  <body>
    <h1>{{ date_from }} - {{ date_to }}</h1>
    <table>
      <thead>
        <tr>
          <th>&nbsp;</th>
        {% for user_id, user in users.items() %}
          <th>{{ user.name }}</th>
        {% endfor %}
        </tr>
      </thead>
      <tbody>
      {% for project_id, project in projects.items() %}
        <tr>
          <td>{{ project.name }}</td>
        {% for user_id, user in users.items() %}
          <td>{{ spent_time.get((user_id, project_id), 0) }}</td>
        {% endfor %}
        </tr>
      {% endfor %}
      </tbody>
    </table>
  </body>
</html>
''')
        html = template.render(**data)
        return html

    @classmethod
    def _save_report_html_to_file(cls, html, filename):
        with open(filename, 'w+') as f:
            f.write(html)

    def _build_report(self):
        report_data = self._get_report_data(
            date_from=self._config.report_date_from,
            date_to=self._config.report_date_to)
        report_html = self._render_report_to_html(data=report_data)
        self._save_report_html_to_file(html=report_html,
                                       filename=self._config.report_filename)
        # here you can add sending report html by email ...

    def handle(self):
        try:
            self._load_config()
            self._init_client()
            self._save_config()
            self._build_report()
        except HubstaffAuthError:
            self._logger.error('hubstaff error: authentication failed')
        except ma.ValidationError as e:
            self._logger.error('validation error: %s' % e.messages)