如何在下面的代码中减少编码长度?

时间:2019-12-10 05:30:19

标签: django django-models

因为我是新手,所以很难理解长度很大的代码。如果我可以减少代码的长度,那么我如何减少代码的长度,请在下面的代码中解释一下。

class Attachments(models.Model):
    created_by = models.ForeignKey(
        User, related_name='attachment_created_by',
        on_delete=models.SET_NULL, null=True)
    file_name = models.CharField(max_length=60)
    created_on = models.DateTimeField(_("Created on"), auto_now_add=True)
    attachment = models.FileField(
        max_length=1001, upload_to='attachments/%Y/%m/')
    lead = models.ForeignKey(
        'leads.Lead', null=True,
        blank=True, related_name='lead_attachment',
        on_delete=models.CASCADE)
    account = models.ForeignKey(
        'accounts.Account', null=True, blank=True,
        related_name='account_attachment', on_delete=models.CASCADE)
    contact = models.ForeignKey(
        'contacts.Contact', on_delete=models.CASCADE,
        related_name='contact_attachment',
        blank=True, null=True)
    opportunity = models.ForeignKey(
        'opportunity.Opportunity', blank=True,
        null=True, on_delete=models.CASCADE,
        related_name='opportunity_attachment')
    case = models.ForeignKey(
        'cases.Case', blank=True, null=True,
        on_delete=models.CASCADE, related_name='case_attachment')

    task = models.ForeignKey('tasks.Task', blank=True, null=True,
                             related_name='tasks_attachment', on_delete=models.CASCADE)

    invoice = models.ForeignKey('invoices.Invoice', blank=True, null=True,
                                related_name='invoice_attachment', on_delete=models.CASCADE)
    event = models.ForeignKey('events.Event', blank=True, null=True,
                              related_name='events_attachment', on_delete=models.CASCADE)

    def file_type(self):
        name_ext_list = self.attachment.url.split(".")
        if (len(name_ext_list) > 1):
            ext = name_ext_list[int(len(name_ext_list) - 1)]
            if is_document_file_audio(ext):
                return ("audio", "fa fa-file-audio")
            if is_document_file_video(ext):
                return ("video", "fa fa-file-video")
            if is_document_file_image(ext):
                return ("image", "fa fa-file-image")
            if is_document_file_pdf(ext):
                return ("pdf", "fa fa-file-pdf")
            if is_document_file_code(ext):
                return ("code", "fa fa-file-code")
            if is_document_file_text(ext):
                return ("text", "fa fa-file-alt")
            if is_document_file_sheet(ext):
                return ("sheet", "fa fa-file-excel")
            if is_document_file_zip(ext):
                return ("zip", "fa fa-file-archive")
            return ("file", "fa fa-file")
        return ("file", "fa fa-file")

    def get_file_type_display(self):
        if self.attachment:
            return self.file_type()[1]
        return None

    @property
    def created_on_arrow(self):
        return arrow.get(self.created_on).humanize()

我想在不更改其功能的情况下尽可能减少上述代码。

0 个答案:

没有答案