Exemple #1
0
    def with_organisation(self, organisation):
        valid_election_types = organisation.election_types.all()
        if self.election_type not in valid_election_types:
            raise Organisation.ValidationError(
                "'%s' is not a valid organisation for election type '%s'" %\
                (organisation, self.election_type)
            )

        if organisation.start_date and organisation.start_date > self.date:
            raise Organisation.ValidationError(
                'Organisation start date after election date')
        if organisation.end_date and organisation.end_date < self.date:
            raise Organisation.ValidationError(
                'Organisation end date before election date')

        # if this is a top-level group id
        # we associate the election object with an organisation
        # but the organisation doesn't form part of the id
        if organisation.organisation_type == self.election_type.election_type:
            self._use_org = False
            self.organisation = Organisation.objects.get(
                organisation_type=self.election_type.election_type)
        else:
            self._use_org = True
            self.id = self.id.with_organisation(organisation.slug)
            self.organisation = organisation
        return self