Example #1
0
    def clean_overturn(self):
        string = self.cleaned_data['overturn']
        string = string.replace(' ', '')
        if string == '':
            return []
        else:
            pnrs = string.split(',')
            users = []
            for pnr in pnrs:
                melos_id = MelosClient.get_melos_id(pnr)

                if not get_user_model().objects.filter(
                    melos_id=melos_id
                ).exists() or melos_id is False:
                    raise forms.ValidationError(
                        _('No user with the person number %(pnr)s exists.'),
                        params={'pnr': pnr},
                    )
                elif self.position.applications.filter(
                    applicant__melos_id=melos_id,
                ).exclude(
                    status='draft'
                ).exists():
                    raise forms.ValidationError(
                        _('User with person number %(pnr)s already applied for'
                          ' this position and can not be appointed through the'
                          ' overturn field.'),
                        params={'pnr': pnr},
                    )
                else:
                    users.append(get_user_model().objects.filter(
                        melos_id=melos_id
                    ).first())
            return users
Example #2
0
    def clean_person_number(self):
        person_number = self.cleaned_data['person_number']
        melos_id = MelosClient.get_melos_id(person_number)
        if not melos_id or Member.find_by_melos_id(melos_id):
            raise forms.ValidationError(_("Incorrect SSN"))

        self.instance.melos_id = melos_id
        return person_number
def fetch_and_save_melos_info(melos_id):
    melos_data = MelosClient.get_user_data(melos_id)
    if melos_data is not None:
        name = "{} {}".format(melos_data['first_name'].strip(),
                              melos_data['last_name'].strip())
        person_nr = melos_data['person_number']
        return name, person_nr

    return None, None
Example #4
0
    def find_by_ssn(ssn):
        try:
            ssn = ssn.strip()
            SSNValidator()(ssn)
            melos_id = MelosClient.get_melos_id(ssn)
            return Member.find_by_melos_id(melos_id), melos_id
        except Exception:
            pass

        return None, None
Example #5
0
 def save(self):
     melos_id = MelosClient.get_melos_id(self.cleaned_data['person_number'])
     return Member.objects.create_user(
         self.cleaned_data['username'],
         self.cleaned_data['password1'],
         self.cleaned_data['email'],
         self.cleaned_data['phone_number'],
         melos_id,
         section=self.cleaned_data['section']
     )
Example #6
0
    def find_by_ssn(ssn):
        try:
            ssn = ssn.strip()
            SSNValidator()(ssn)
            user = Member.objects.filter(person_nr=ssn).first()

            if user is None:
                melos_id = MelosClient.get_melos_id(ssn)
                return Member.find_by_melos_id(melos_id), melos_id
            else:
                return user, user.melos_id
        except Exception:
            pass

        return None, None
Example #7
0
def member_check_api(request):
    """
    Checks whether a person with a given personnummer is a member in UTN.
    """
    serializer = MemberCheckSerializer(data=request.data)
    status_code = None
    data = {}

    if serializer.is_valid():
        ssn = serializer.data.get('ssn')
        is_member = MelosClient.is_member(ssn)
        data = {"is_member": is_member}
    else:
        error = serializer.errors.get("ssn")
        data = {'error': "Personnummer: " + ", ".join(error)}
        status_code = status.HTTP_400_BAD_REQUEST

    return Response(data,
                    status=status_code,
                    headers={"Access-Control-Allow-Origin": "*"})
Example #8
0
    def _create_user(
        self, username, password,
        email, phone_number, melos_id,
        is_superuser=False, is_staff=False,
        study=None, section=None, registration_year=""
    ):
        melos_data = MelosClient.get_user_data(melos_id)

        name = ""
        person_nr = ""

        if melos_data is not None:
            name = "{} {}".format(
                melos_data['first_name'].strip(),
                melos_data['last_name'].strip()
            )

            person_nr = melos_data["person_number"]

        user = Member.objects.create(
            username=username,
            melos_id=melos_id,
            email=email,
            phone_number=phone_number,
            is_superuser=is_superuser,
            is_staff=is_staff,
            name=name,
            person_nr=person_nr,
            study=study,
            section=section,
            registration_year=registration_year
        )
        user.set_password(password)
        user.update_status()
        user.save()

        return user
Example #9
0
    def update_status(self, data=None, save=True):
        if data is None:
            # Prevent updating this value to often
            if timezone.now() - self.status_changed < timedelta(days=1):
                return

            melos_user_data = self.get_melos_user_data()
            if melos_user_data is None:
                return
            is_member = MelosClient.is_member(melos_user_data['person_number'])
            data = "member" if is_member else "nonmember"

        if data == 'member':
            self.status = 'member'
        elif data == 'nonmember':
            if self.status not in ['member', 'alumnus']:
                self.status = 'nonmember'
            else:
                self.status = 'alumnus'

        self.status_changed = timezone.now()

        if save:
            self.save()
Example #10
0
 def get_melos_user_data(self):
     if self.melos_user_data is None:
         self.melos_user_data = MelosClient.get_user_data(self.melos_id)
     return self.melos_user_data