示例#1
0
    def test_user_approved_buttons_click(self):
        """
        Check volunteer clicks links under My volunteer hours.

        Expected:
        - status_code == 200
        - user name, description with 'npf_org_1' and 3.0 hrs are on the page
        - user name, description with 'npf_org_2' and 2.0 hrs are on the page
        """
        oc_user = User.objects.get(username="******")
        self.client.login(username=oc_user.username, password='******')

        response = self.client.get(
            '/hours-detail/?user_id=1&org_id=1&type=approved')

        self.assertEqual(response.status_code, 200)
        self.assertIn('hours_detail', response.context)
        expected_list_of_hours_len = 1
        self.assertEqual(len(response.context['hours_detail']),
                         expected_list_of_hours_len)

        self.assertEqual(
            response.context['hours_detail'][0].usertimelog.user.username,
            'volunteer_1')
        self.assertEqual(
            response.context['hours_detail']
            [0].usertimelog.event.project.org.name, 'NPF_org_1')

        self.assertEqual(
            diffInHours(
                response.context['hours_detail'][0].usertimelog.datetime_start,
                response.context['hours_detail'][0].usertimelog.datetime_end),
            3.0)

        response = self.client.get(
            '/hours-detail/?user_id=1&org_id=2&type=approved')
        expected_list_of_hours_len = 1
        self.assertEqual(len(response.context['hours_detail']),
                         expected_list_of_hours_len)

        self.assertEqual(
            response.context['hours_detail'][0].usertimelog.user.username,
            'volunteer_1')
        self.assertEqual(
            response.context['hours_detail']
            [0].usertimelog.event.project.org.name, 'NPF_org_2')

        self.assertEqual(
            diffInHours(
                response.context['hours_detail'][0].usertimelog.datetime_start,
                response.context['hours_detail'][0].usertimelog.datetime_end),
            2.0)
示例#2
0
    def get_total_hours_pending(self):
        """
        returns admin's pending hour total
        """
        hours_pending_admin = sum([
            common.diffInHours(x.usertimelog.event.datetime_start,
                               x.usertimelog.event.datetime_end)
            for x in self.get_hours_requested()
        ])

        return round(hours_pending_admin, 2)
示例#3
0
    def get_total_hours_issued(self):
        """
        returns admin's issued hour total
        """
        hours_app = self.get_hours_approved()

        hours_issued_admin = sum([
            common.diffInHours(x.usertimelog.event.datetime_start,
                               x.usertimelog.event.datetime_end)
            for x in hours_app
        ])

        return round(hours_issued_admin, 2)
示例#4
0
    def __unicode__(self):
        tz = self.event.project.org.timezone
        status = ' '.join([
            self.user.username,
            'contributed %s',
            'to',
            self.event.project.org.name,
            'starting',
            self.datetime_start.astimezone(
                pytz.timezone(tz)).strftime('%Y-%m-%d %I-%M %p'),
        ])

        if self.event.event_type == 'GR':
            status = ' '.join([status, 'at event', self.event.project.name])

            hours = diffInHours(self.event.datetime_start,
                                self.event.datetime_end)
        else:
            hours = diffInHours(self.datetime_start, self.datetime_end)

        status %= str(hours) + ' hr.'
        return status
示例#5
0
    def __unicode__(self):
        if self.usertimelog.datetime_end:
            hours = diffInHours(self.usertimelog.datetime_start,
                                self.usertimelog.datetime_end)
        else:
            hours = diffInHours(self.usertimelog.event.datetime_start,
                                self.usertimelog.event.datetime_end)

        tz = self.usertimelog.event.project.org.timezone

        if self.action_type == 'req':
            return ' '.join([
                self.usertimelog.user.username,
                'REQUESTED approval of',
                str(hours),
                'hours starting on',
                self.usertimelog.datetime_start.astimezone(
                    pytz.timezone(tz)).strftime('%Y-%m-%d %I-%M %p'),
                'from',
                self.usertimelog.event.project.org.name,
                'admin',
                self.user.username,
            ])
        else:
            return ' '.join([
                self.usertimelog.event.project.org.name,
                'admin',
                self.user.email,
                self.get_action_type_display().upper(),
                str(hours),
                'hours by',
                self.usertimelog.user.email,
                'starting on',
                self.usertimelog.datetime_start.astimezone(
                    pytz.timezone(tz)).strftime('%Y-%m-%d %I-%M %p'),
            ])
def setup_volunteer_requests(users, orgs):
    npf_orgs = [org for org in orgs if org.status == 'npf']
    usertimelogs = UserTimeLog.objects.filter(user__in=users,
                                              event__event_type='MN')
    if len(usertimelogs) >= 30:
        print 'Sufficient number of existing hour requests already'
        return

    for i in xrange(30):
        user = _get_random_item(users)
        org = random.choice(npf_orgs)
        action = random.choice(['req', 'app', 'dec'])
        is_verified = action == 'app'

        project, created = Project.objects.get_or_create(org=org,
                                                         name='ManualTracking')

        datetime_start = datetime.now(tz=pytz.utc) - \
            timedelta(days=random.randint(7)) + \
            timedelta(hours=random.randint(24)) + \
            timedelta(minutes=random.randint(4) * 15)
        datetime_end = datetime_start + \
            timedelta(minutes=random.randint(1,16) * 15)

        event = Event.objects.create(project=project,
                                     description=_get_random_string(),
                                     event_type='MN',
                                     datetime_start=datetime_start,
                                     datetime_end=datetime_end)

        usertimelog = UserTimeLog.objects.create(user=user,
                                                 event=event,
                                                 datetime_start=datetime_start,
                                                 datetime_end=datetime_end,
                                                 is_verified=is_verified)

        admin = _get_random_item(
            User.objects.filter(last_name=org.name + 'Admin'))
        actiontimelog = AdminActionUserTime.objects.create(
            user=admin, usertimelog=usertimelog, action_type=action)
        amount = diffInHours(datetime_start, datetime_end)
        print str(actiontimelog)

        if is_verified:
            OcLedger().issue_currents(entity_id_from=org.orgentity.id,
                                      entity_id_to=user.userentity.id,
                                      action=actiontimelog,
                                      amount=amount)
示例#7
0
    def _split_by_org(self, actions):
        hours_by_org = {}
        temp_orgs_set = set()

        for action in actions:
            event = action.usertimelog.event
            org = event.project.org
            approved_hours = common.diffInHours(event.datetime_start,
                                                event.datetime_end)

            if approved_hours > 0:
                if org not in temp_orgs_set:
                    temp_orgs_set.add(org)
                    hours_by_org[org] = approved_hours
                else:
                    hours_by_org[org] += approved_hours

        return hours_by_org
示例#8
0
from datetime import datetime, timedelta
from pytz import utc

dt = utc.localize(datetime(2017, 6, 5))
m = set()
d = {}

while dt < datetime.now(tz=utc):
    aauts = AdminActionUserTime.objects.filter(date_created__gte=dt).filter(
        date_created__lt=dt + timedelta(weeks=1)).filter(
            usertimelog__is_verified=True).filter(action_type='app')

    uts = set()
    d[dt.strftime('%Y-%m-%d')] = 0
    for aaut in aauts:
        ut = aaut.usertimelog
        if ut.id not in uts and ut.user.has_usable_password():
            d[dt.strftime('%Y-%m-%d')] += diffInHours(ut.event.datetime_start,
                                                      ut.event.datetime_end)
            uts.add(ut.id)

    dt += timedelta(weeks=1)

print sum(d.values())

with open('./metrics/scripts/kpi/currents/currents_issued_verified.csv',
          'w') as f:
    wr = writer(f)
    for key, val in d.iteritems():
        wr.writerow([key, val])
def setup_events(users, orgs):
    npf_orgs = [org for org in orgs if org.status == 'npf']
    names = random.choice(list(string.letters), 10, replace=False)

    projects = Project.objects.filter(org__in=npf_orgs)

    if len(projects) >= 10:
        print 'Sufficient number of projects created already'
        return

    for name in names:
        org = _get_random_item(npf_orgs)
        project, created = Project.objects.get_or_create(name=' '.join(
            ['Let\'s', name]),
                                                         org=org)

        datetime_start = datetime.now(tz=pytz.utc) + \
            _get_random_item([-1, 1]) * timedelta(days=random.randint(60)) + \
            timedelta(hours=random.randint(12, 24))
        datetime_end = datetime_start + timedelta(hours=random.randint(4))
        num_locations = random.randint(3)

        for loc in xrange(num_locations):
            admin = _get_random_item(
                User.objects.filter(last_name=org.name + 'Admin'))
            event = Event.objects.create(project=project,
                                         description=_get_random_string(),
                                         location='Location' + str(loc),
                                         coordinator=admin,
                                         is_public=True,
                                         datetime_start=datetime_start,
                                         datetime_end=datetime_end)
            print str(event)

            # register for event
            users_reg = random.choice(users,
                                      random.randint(1, len(users)),
                                      replace=False)
            for user in users_reg:
                try:
                    uer = UserEventRegistration.objects.create(
                        user=user, event=event, is_confirmed=True)
                    print str(uer)
                except Exception as e:
                    print e.message
                    pass

            # checkin at random time
            users_checkin = random.choice(users_reg,
                                          random.randint(len(users_reg)),
                                          replace=False)
            event_duration = datetime_end - datetime_start

            for user_chk in users_checkin:
                try:
                    utl = UserTimeLog.objects.create(
                        user=user_chk,
                        event=event,
                        is_verified=True,
                        datetime_start=datetime_start +
                        _get_random_item([-1, 0, 1]) * random.randint(4) *
                        event_duration)

                    # randomly checkout
                    if random.randint(2):
                        utl.datetime_end = datetime_start + timedelta(
                            hours=random.randint(12))
                        utl.save()
                    print str(utl)

                    actiontimelog = AdminActionUserTime.objects.create(
                        user=admin, usertimelog=utl, action_type='app')

                    OcLedger().issue_currents(
                        entity_id_from=org.orgentity.id,
                        entity_id_to=user_chk.userentity.id,
                        action=actiontimelog,
                        amount=diffInHours(datetime_start, datetime_end))

                except Exception as e:
                    print e.message
                    pass
示例#10
0
            pass
        is_approved_with_org = orguserinfo.is_org_admin(
            req.usertimelog.event.project.org.id)

        data[req.id] = {
            'user_id':
            req.usertimelog.user.id,
            'user_email':
            req.usertimelog.user.email,
            'admin_email':
            req.user.email,
            'is_approved':
            is_approved,
            'is_approved_with_org':
            is_approved_with_org,
            'org':
            req.usertimelog.event.project.org.name,
            'hours':
            common.diffInHours(req.usertimelog.datetime_start,
                               req.usertimelog.datetime_end),
            'date_created':
            req.date_created.strftime('%m/%d/%Y')
        }

with open('./metrics/scripts/user/user-hours-data.csv', 'w') as f:
    wr = writer(f)
    wr.writerow(['id'] + data[data.keys()[0]].keys())

    for idx, metrics in data.iteritems():
        wr.writerow([idx] + metrics.values())
示例#11
0
orgusers = OrgUser.objects.filter(org__status='npf')

data = {}
for orguser in orgusers:
    user = orguser.user
    orguserinfo = OrgUserInfo(user.id)
    is_approved = orguserinfo.is_org_admin()

    orgadmin = OrgAdmin(user.id)
    hours_req = orgadmin.get_hours_requested()
    hours_app = orgadmin.get_hours_approved()
    hours_total_pending = orgadmin.get_total_hours_pending()
    hours_total_approved_all = orgadmin.get_total_hours_issued()
    hours_total_approved_last_week = sum([
        common.diffInHours(rec.usertimelog.event.datetime_start,
                           rec.usertimelog.event.datetime_end)
        for rec in hours_app
        if rec.date_created > datetime.now(tz=utc) - timedelta(weeks=1)
    ])

    data[user.id] = {
        'org':
        orguser.org.name,
        'admin_email':
        user.email,
        'admin_first_name':
        user.first_name.encode('utf-8'),
        'admin_last_name':
        user.last_name.encode('utf-8'),
        'is_approved':
        is_approved,