Example #1
0
    def test_add(self, mock_org):
        mock_org.return_value = Org(id=1, name='test org')

        users_count = User.objects.count()
        self.assertEqual(users_count, 1)

        core_users_count = models.User.objects.count()
        self.assertEqual(core_users_count, 1)

        params = {
            'username': '******',
            'first_name': 'Test',
            'last_name': 'User',
            'email': '*****@*****.**',
            'is_active': True,
            'new_password': '******',
            'groups': self.permission_group.id,
            'orgs': ["1", "5", "8"]
        }
        response = self.client.post(self.get_url(), params)
        self.check_response(response, status=302)
        self.assertEqual(response.url, reverse_lazy('core:user'))

        user = User.objects.all()[1]
        self.assertEqual(user.username, params['username'])

        core_user = models.User.objects.all()[1]
        self.assertEqual(core_user.django_user, user)
        self.assertEqual(core_user.org_ids, json.dumps(params['orgs']))
Example #2
0
    def get(cls, direction_id) -> 'Direction':
        url = settings.MIS_URL + f'/api/pre_record/{direction_id}/'
        headers = {'Authorization': f'Token {settings.MIS_TOKEN}'}
        response = requests.get(url, headers=headers)
        response.raise_for_status()

        result = response.json()
        direction = cls(
            number=result['id'],
            last_name=result['last_name'],
            first_name=result['first_name'],
            middle_name=result['middle_name'],
            birth=iso_to_date(result['birth']),
            gender=result['gender'],
            from_date=iso_to_date(result['date_from']),
            to_date=iso_to_date(result['date_to']),
            org=Org.get(
                org_id=result['org']['id']) if result.get('org') else None,
            pay_method=result['pay_method'],
            exam_type=result['exam_type'],
            post=result['post'],
            shop=result['shop'],
            law_items_section_1=[
                l_i for l_i in result.get('law_items', [])
                if l_i['section'] == '1'
            ],
            law_items_section_2=[
                l_i for l_i in result.get('law_items', [])
                if l_i['section'] == '2'
            ])
        return direction
Example #3
0
    def filter(cls, params: Dict = None) -> List['Direction']:
        url = settings.MIS_URL + f'/api/pre_record/'
        headers = {'Authorization': f'Token {settings.MIS_TOKEN}'}

        response = requests.get(url, params=params, headers=headers)
        response.raise_for_status()

        directions = []
        for item in response.json()['results']:
            directions.append(
                cls(number=item['id'],
                    last_name=item['last_name'],
                    first_name=item['first_name'],
                    middle_name=item['middle_name'],
                    birth=iso_to_date(item['birth']),
                    gender=item['gender'],
                    from_date=iso_to_date(item['date_from']),
                    to_date=iso_to_date(item['date_to']),
                    org=Org.get(
                        org_id=item['org']['id']) if item.get('org') else None,
                    pay_method=item['pay_method'],
                    exam_type=item['exam_type'],
                    post=item['post'],
                    shop=item['shop'],
                    law_items_section_1=[
                        l_i for l_i in item.get('law_items', [])
                        if l_i['section'] == 1
                    ],
                    law_items_section_2=[
                        l_i for l_i in item.get('law_items', [])
                        if l_i['section'] == 2
                    ]))
        return directions
Example #4
0
    def get(self, request, *args, **kwargs):
        filter_params = copy(self.request.GET)

        if self.request.user.core.org_ids:
            filter_params.update(
                {'id': json.loads(self.request.user.core.org_ids)})

        orgs = Org.filter(params=filter_params)
        results = [{'id': org.id, 'text': org.name} for org in orgs]

        return Response({'results': results})
Example #5
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        for field in ('org', 'orgs'):
            choices = []
            values = self.data.getlist(field) if hasattr(self.data,
                                                         'getlist') else None
            if not values:
                values = self.initial.get(field, [])

            if not isinstance(values, list):
                values = [values]

            if values:
                for value in values:
                    org = Org.get(org_id=value)
                    choices.append((org.id, org.name))

            self.fields[field].widget.choices = choices
Example #6
0
    def test_add(self, mock_org):
        mock_org.return_value = Org(id=1, name='test org')
        response = self.client.get(self.get_url())
        self.check_response(response)
        self.assertEqual(response.context_data['object'], self.test_user)

        params = {
            'username': '******',
            'first_name': 'Test',
            'last_name': 'User',
            'email': '*****@*****.**',
            'is_active': True,
            'new_password': '******',
            'groups': self.permission_group.id,
            'orgs': ["1", "5", "8"]
        }
        response = self.client.post(self.get_url(), params)
        self.check_response(response, status=302)
        self.assertEqual(response.url, reverse_lazy('core:user'))

        user = User.objects.get(id=self.test_user.id)
        self.assertEqual(user.username, params['username'])
        self.assertEqual(user.core.org_ids, json.dumps(params['orgs']))
Example #7
0
    def get_orgs(self) -> List[Org]:
        if not self.org_ids:
            return []

        return [Org.get(org_id=org_id) for org_id in json.loads(self.org_ids)]