Exemplo n.º 1
0
class OperationsUser(AbstractUser):
    phone_number = models.CharField(null=True, max_length=50)
    signature = models.FileField(null=True,
                                 blank=True,
                                 upload_to='uploads/accounts')
    regional_staff = models.BooleanField(blank=True, default=False)
    region = ProtectedForeignKey('operations.Region',
                                 null=True,
                                 blank=True,
                                 related_name='user_region')
    district = ProtectedForeignKey('operations.Branch',
                                   null=True,
                                   blank=True,
                                   related_name='user_district')

    def __unicode__(self):
        return self.username

    @property
    def full_name(self):
        full_name = "{} {}".format(self.first_name,self.last_name) \
                    if self.first_name and self.last_name else \
                    "{}".format(self.first_name) if self.first_name \
                    else "{}".format(self.last_name) if self.last_name \
                    else None
        return full_name

    @property
    def can_edit(self):
        return self.is_superuser or self.privilege == 'create_elopsysuser' or self.privilege == 'admin'

    @property
    def is_executive(self):
        return 'Operations Executive' in self.groups.all()

    @property
    def is_emerald_employee(self):
        return 'Emerald Employee' in self.groups.all().values_list('name',
                                                                   flat=True)

    @property
    def is_technician(self):
        return 'Technician' in self.groups.all().values_list('name', flat=True)

    @property
    def is_administrator(self):
        return 'Administrator' in self.groups.all().values_list('name',
                                                                flat=True)

    @property
    def is_management(self):
        return 'Management' in self.groups.all().values_list('name', flat=True)

    @property
    def is_regional_manager(self):
        return 'Regional Manager' in self.groups.all().values_list('name',
                                                                   flat=True)

    @property
    def is_regional_user(self):
        return self.regional_staff

    @property
    def get_user_region(self):
        if self.region is None:
            return Region.objects.none()
        else:
            return self.region.id

    @property
    def get_user_district(self):
        if self.district is None:
            return Branch.objects.none()
        else:
            return self.district.id