Пример #1
0
    def bulk_dept_import(self,
                         session,
                         target_server='',
                         api_token='',
                         message='',
                         **kwargs):

        service, message, target_url = get_api_service_from_server(
            target_server, api_token)
        uri = '{}/jsonrpc/'.format(target_url)

        if not message and service and cherrypy.request.method == 'POST':
            from_departments = [(id, name) for id, name in sorted(
                service.dept.list().items(), key=lambda d: d[1])]

            for id, name in from_departments:
                from_department = service.dept.jobs(department_id=id)
                to_department = session.query(Department).filter_by(
                    name=from_department['name']).first()
                if not to_department:
                    to_department = _create_copy_department(from_department)
                    session.add(to_department)

                _copy_department_roles(to_department, from_department)

            message = 'Successfully imported all departments and roles from {}'.format(
                uri)
            raise HTTPRedirect(
                'import_shifts?target_server={}&api_token={}&message={}',
                target_server, api_token, message)
Пример #2
0
    def import_shifts(self,
                      session,
                      target_server='',
                      api_token='',
                      to_department_id='',
                      from_department_id='',
                      message='',
                      **kwargs):

        service, message, target_url = get_api_service_from_server(
            target_server, api_token)
        uri = '{}/jsonrpc/'.format(target_url)

        department = {}
        from_departments = []
        if not message and service:
            from_departments = [(id, name) for id, name in sorted(
                service.dept.list().items(), key=lambda d: d[1])]
            if cherrypy.request.method == 'POST':
                from_department = service.dept.jobs(
                    department_id=from_department_id)
                shifts_text = ' shifts' if 'skip_shifts' not in kwargs else ''

                if to_department_id == "None":
                    existing_department = session.query(Department).filter_by(
                        name=from_department['name']).first()
                    if existing_department:
                        raise HTTPRedirect(
                            'import_shifts?target_server={}&api_token={}&message={}',
                            target_server, api_token,
                            "Cannot create a department with the same name as an existing department"
                        )
                    to_department = _create_copy_department(from_department)
                    session.add(to_department)
                else:
                    to_department = session.query(Department).get(
                        to_department_id)

                dept_role_map = _copy_department_roles(to_department,
                                                       from_department)

                if shifts_text:
                    _copy_department_shifts(service, to_department,
                                            from_department, dept_role_map)

                message = '{}{}successfully imported from {}'.format(
                    to_department.name, shifts_text, uri)
                raise HTTPRedirect(
                    'import_shifts?target_server={}&api_token={}&message={}',
                    target_server, api_token, message)

        return {
            'target_server': target_server,
            'target_url': uri,
            'api_token': api_token,
            'department': department,
            'to_departments': c.DEPARTMENT_OPTS,
            'from_departments': from_departments,
            'message': message,
        }
Пример #3
0
    def lookup_departments(self,
                           session,
                           target_server='',
                           api_token='',
                           **kwargs):
        service, message, target_url = get_api_service_from_server(
            target_server, api_token)
        uri = '{}/jsonrpc/'.format(target_url)

        if not message:
            try:
                results = [(id, name)
                           for id, name in sorted(service.dept.list().items(),
                                                  key=lambda d: d[1])]
            except Exception as ex:
                message = str(ex)

        if message:
            return {
                'error': message,
                'target_url': uri,
            }

        return {
            'departments': results,
            'target_url': uri,
        }
Пример #4
0
    def import_attendees(self, session, target_server='', api_token='', query='', message=''):
        service, service_message, target_url = get_api_service_from_server(target_server, api_token)
        message = message or service_message

        attendees, existing_attendees, results = {}, {}, {}

        if service:
            try:
                results = service.attendee.export(query=query)
            except Exception as ex:
                message = str(ex)

        if cherrypy.request.method == 'POST' and not message:
            attendees = results.get('attendees', [])
            for attendee in attendees:
                attendee['href'] = '{}/registration/form?id={}'.format(target_url, attendee['id'])

            if attendees:
                attendees_by_name_email = groupify(attendees, lambda a: (
                    a['first_name'].lower(),
                    a['last_name'].lower(),
                    normalize_email(a['email']),
                ))

                filters = [
                    and_(
                        func.lower(Attendee.first_name) == first,
                        func.lower(Attendee.last_name) == last,
                        Attendee.normalized_email == email,
                    )
                    for first, last, email in attendees_by_name_email.keys()
                ]

                existing_attendees = session.query(Attendee).filter(or_(*filters)).all()
                for attendee in existing_attendees:
                    existing_key = (attendee.first_name.lower(), attendee.last_name.lower(), attendee.normalized_email)
                    attendees_by_name_email.pop(existing_key, {})
                attendees = list(chain(*attendees_by_name_email.values()))

        return {
            'target_server': target_server,
            'api_token': api_token,
            'query': query,
            'message': message,
            'unknown_ids': results.get('unknown_ids', []),
            'unknown_emails': results.get('unknown_emails', []),
            'unknown_names': results.get('unknown_names', []),
            'unknown_names_and_emails': results.get('unknown_names_and_emails', []),
            'attendees': attendees,
            'existing_attendees': existing_attendees,
        }
Пример #5
0
    def confirm_import_attendees(self, session, badge_type, admin_notes,
                                 target_server, api_token, query,
                                 attendee_ids):
        if cherrypy.request.method != 'POST':
            raise HTTPRedirect(
                'import_attendees?target_server={}&api_token={}&query={}',
                target_server, api_token, query)

        service, message, target_url = get_api_service_from_server(
            target_server, api_token)

        try:
            results = service.attendee.export(query=','.join(
                listify(attendee_ids)),
                                              full=True)
        except Exception as ex:
            raise HTTPRedirect(
                'import_attendees?target_server={}&api_token={}&query={}&message={}',
                target_server, remote_api_token, query, str(ex))

        depts = {}

        def _guess_dept(id_name):
            id, name = id_name
            if id in depts:
                return (id, depts[id])

            dept = session.query(Department).filter(
                or_(
                    Department.id == id, Department.normalized_name ==
                    Department.normalize_name(name))).first()

            if dept:
                depts[id] = dept
                return (id, dept)
            return None

        badge_type = int(badge_type)
        badge_label = c.BADGES[badge_type].lower()

        attendees = results.get('attendees', [])
        for d in attendees:
            import_from_url = '{}/registration/form?id={}\n\n'.format(
                target_url, d['id'])
            new_admin_notes = '{}\n\n'.format(
                admin_notes) if admin_notes else ''
            old_admin_notes = 'Old Admin Notes:\n{}\n'.format(
                d['admin_notes']) if d['admin_notes'] else ''

            d.update({
                'badge_type':
                badge_type,
                'badge_status':
                c.NEW_STATUS,
                'paid':
                c.NEED_NOT_PAY,
                'placeholder':
                True,
                'requested_hotel_info':
                True,
                'admin_notes':
                'Imported {} from {}{}{}'.format(badge_label, import_from_url,
                                                 new_admin_notes,
                                                 old_admin_notes),
                'past_years':
                d['all_years'],
            })
            del d['id']
            del d['all_years']

            if badge_type != c.STAFF_BADGE:
                attendee = Attendee().apply(d, restricted=False)

            else:
                assigned_depts = {
                    d[0]: d[1]
                    for d in map(_guess_dept,
                                 d.pop('assigned_depts', {}).items()) if d
                }
                checklist_admin_depts = d.pop('checklist_admin_depts', {})
                dept_head_depts = d.pop('dept_head_depts', {})
                poc_depts = d.pop('poc_depts', {})
                requested_depts = d.pop('requested_depts', {})

                d.update({
                    'staffing':
                    True,
                    'ribbon':
                    str(c.DEPT_HEAD_RIBBON) if dept_head_depts else '',
                })

                attendee = Attendee().apply(d, restricted=False)

                for id, dept in assigned_depts.items():
                    attendee.dept_memberships.append(
                        DeptMembership(
                            department=dept,
                            attendee=attendee,
                            is_checklist_admin=bool(
                                id in checklist_admin_depts),
                            is_dept_head=bool(id in dept_head_depts),
                            is_poc=bool(id in poc_depts),
                        ))

                requested_anywhere = requested_depts.pop('All', False)
                requested_depts = {
                    d[0]: d[1]
                    for d in map(_guess_dept, requested_depts.items()) if d
                }

                if requested_anywhere:
                    attendee.dept_membership_requests.append(
                        DeptMembershipRequest(attendee=attendee))
                for id, dept in requested_depts.items():
                    attendee.dept_membership_requests.append(
                        DeptMembershipRequest(
                            department=dept,
                            attendee=attendee,
                        ))

            session.add(attendee)

        attendee_count = len(attendees)
        raise HTTPRedirect(
            'import_attendees?target_server={}&api_token={}&query={}&message={}',
            target_server, api_token, query,
            '{count} attendee{s} imported with {a}{badge_label} badge{s}'.
            format(
                count=attendee_count,
                s=pluralize(attendee_count),
                a=pluralize(
                    attendee_count,
                    singular='an ' if badge_label.startswith('a') else 'a ',
                    plural=''),
                badge_label=badge_label,
            ))