Django Model’s Methods Django.How

Author avatar wrote on 08/06/2022

FAT models THIN view

1. Can be called from any where by selecting the object (No need to repeat)
2.Can be accessed on templates if they return value (No need to pass to the context)


class Job(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    source_file_path = models.ForeignKey(File, on_delete=models.CASCADE)
    translation_file_path = models.ForeignKey(FileTranslation, on_delete=models.SET_NULL, null=True, blank=True)

    def __str__(self):
        return str(self.id)

    def is_job_ready_to_start(self):
        is_ready = False
        previous_job = None
        if Job.objects.filter(pk__lt=self.id, project=self.project, source_file_path=self.source_file_path).exists():
            previous_job = Job.objects.filter(pk__lt=self.id).order_by('id').last()
            if previous_job.status == 'AP' or previous_job.status == 'ST':
                is_ready = True
                previous_job = previous_job
        else:
            is_ready = True

        return is_ready, previous_job

Returns the job's active assignment(s)

    def assigned_translators(self, get_first=True):
        # this to be used as long as we have 1 translator per job
        assigned_translators = JobAssignment.objects.filter(job=self, is_active=True)

        if not assigned_translators:
            return None

        # if only one (default), return first
        if get_first:
            if assigned_translators:
                assigned_translators = assigned_translators.first()

        return assigned_translators

    def translator(self):
        assignment = self.assigned_translators()
        if assignment:
            return assignment.translator
        else:
            return None

    def get_translator_cost(self, translator):
        # If not the job is assignable we return 0
        if not self.sub_service.sub_service.is_assigned:
            return 0, 'Not assignable'

        translator_price_to_pay = 0

1. Calculate translator prices for that pair

        if TranslatorPrice.objects.filter(translator=translator, source_language=self.project.lang_from, target_language=self.target_language).exists():
            translator_price = TranslatorPrice.objects.filter(translator=translator, source_language=self.project.lang_from, target_language=self.target_language).first()
            print('(5) there is translator price')
            print(self.sub_service)
            if str(self.sub_service) == 'Translation':
                translator_price_to_pay = translator_price.translation
            elif str(self.sub_service) == 'Proofreading':
                print('called')
                translator_price_to_pay = translator_price.proofreading
            elif str(self.sub_service) == 'Post editing':
                translator_price_to_pay = translator_price.post_editing
        else:
            # print('(5) there is no translator price, we will use default 0.2')
            translator_price_to_pay = 0

        # if price is 0
        if translator_price_to_pay == 0:
            return 0, 'No price'

        print(translator_price_to_pay)

2. Check Unit type

        if self.sub_service.sub_service.base_unit == 'WD':
            order_amount = translator_price_to_pay * self.source_file_path.word_count
        else:
            order_amount = translator_price_to_pay * self.project.order.quote_price.quote.request.word_count                                
        print('     order_amount', order_amount)

        return order_amount, 'Got price'

    def get_assigned_translator_cost(self):
        if not self.sub_service.sub_service.is_assigned:
            return 0, 'Not assignable'

        if self.assigned_translators():
            active_assignment = self.assigned_translators()
            cost, status = self.get_translator_cost(active_assignment.translator)
            return cost, 'Assigned'
        else:
            return 0, 'Not assigned'


In any view you proccess or access any method

job_cost = job.get_translator_cost()

In template you can access job.translator

{{ job.translator }}

CRUDgen Django CRUD generator