2017-10-23 3 views
1

私はクラスメソッドを持つモデルを持っています。クラスメソッドのテストでは、私の必要に応じてモデルインスタンスを変更して変更します。問題は、このクラスメソッドを管理者で使用していることです。アプリケーションが支払遅延を支払うことができない場合、残高を変更する別の取引を作成することが適用されます。私は、ステータスが変更されたとき、またはtickboxは、それがクラスメソッドを起動アプリケーションの管理者に確認されたときにそれを取得する必要がありDjangoにモデルクラスメソッドを導入する方法

class Transactions(models.Model): 

    application = models.ForeignKey(Application, 
    related_name='application_id', blank=True, null=True) 
    transaction_type = models.CharField(max_length=56, 
    choices=TRANSACTION_TYPE, null=True) 
    transaction_source = models.CharField(max_length=56, 
    choices=TRANSACTION_SOURCE, null=True) 
    transaction_method = models.CharField(max_length=56, 
    choices=TRANSACTION_METHOD, null=True) 
    amount = models.DecimalField(max_digits=10, decimal_places=2) 
    created_date = models.DateField() 
    posted_date = models.DateField() 
    external_reference = models.CharField(max_length=100, null=True, 
    verbose_name='External Reference') 

    def __unicode__(self): 
     return self.application.forename + " " + 
     self.application.surname + "(" + str(self.application.id) + ")" 

    @classmethod 
    def late_payment_fee(cls, app, desired_action): 
     """ 
     This function enacts a late payment fee to an application as a 
     transaction 
     :param app: application the fee is going to be applied to 
     :param desired_action: either True or False, when reversing the 
     fee the transaction shouldn't be deleted, 
     just another transaction of the opposite effect in order to 
     help loans collection with tracking, True will 
     enact a feee, False will reverse the fee 
     :return: a transaction which is stored in the database 
     """ 
     today = str(date.today()) 
     if desired_action: 
      trans_type = MISSEDREPAYMENTFEE 
      amount = float(12) 
     else: 
      trans_type = MISSEDREPAYMENTFEEREVERSAL 
      amount = float(-12) 
     cls.create_trasaction(app, trans_type, INTERNALBOOKING, 
     INTERNALBOOKING, amount, today, today, None) 

:モデル内のメソッドは、@classmethodデコレータで飾られています。私は管理者のオーバーライドしたモデルをGoogleで検索しましたが、何も見つかりませんでした。管理者は次のとおりです。

class ApplicationAdmin(ImportExportModelAdmin): 
    resource_class = ApplicationResource 
    search_fields = ['forename', 'surname'] 
    list_filter = ('status', 'underwritingresult', 'test', 'signed', 
    'mandateapproved', 'dealership', 'brand') 
    list_select_related = ('instalment',) 
    list_display = ('id', 'SPV_ID', 'forename', 'surname'...... 
    inlines = [ 
     InstalmentInline, 
     AddressInline 
    ] 
    exclude = ['customer', 'accountnumber', 'sortcode'] 
    readonly_fields = ('Account_Number', 'Sort_Code', 'SPV_ID') 

    def get_readonly_fields(self, request, obj=None): 
     readonly_fields = [] 
     if obj.locked : 
      for field in self.model._meta.fields: 
       readonly_fields.append(field.name) 
     else: 
      readonly_fields = ('Account_Number', 'Sort_Code', 'SPV_ID') 
     return readonly_fields 

    def Account_Number(self, obj): 
     return Decrypt(obj.accountnumber) 

    def Sort_Code(self, obj): 
     return Decrypt(obj.sortcode) 

    def SPV_ID(self, obj): 
     return obj.spv.id 

    def has_delete_permission(self, request, obj=None): 
     return False 

これを読んでいただきありがとうございます。

+0

質問を最小限のコード例で更新してください。 – Soviut

+0

コードは私のモデルとクラスメソッドに含まれており、管理者 – max89

答えて

0

私は今解決しました(私の最初の質問があまり明確でない場合は申し訳ありません)。アプリケーションモデルのsave関数をオーバーライドする必要があることが判明しました。しかし、これを行う際に別の問題が提起されました。ユーザーが顧客の名前のように無害なものを変更した場合、実際の変更がなくてもクラスメソッドは起動して新しいトランザクションを作成します。このため、 initとモデルのsave関数をオーバーライドする必要がありますが、実際には保存されます。このモデルでは、ステータスが変更されたか、または延滞料金が適用されているかどうかを確認することができます。これを実行するために、我々はモデルでinitメソッドをオーバーライドすることにより、初期状態の値を格納する必要があります。

__initial_status = None 
__initial_fee_status = None 

def __init__(self, *args, **kwargs): 
    """ 
    This function overrides the __init__ function in order to 
    save initial_status facilitating comparisons in the save function 
    :param args: allows the function to accept an arbitrary number of 
    arguments and/or keyword arguments 
    :param kwargs: same as above 
    """ 
    super(Application, self).__init__(*args, **kwargs) 
    self.__initial_status = self.status 
    self.__initial_fee_status = self.feeadded 

今、我々は彼らが我々が保存機能を介してそれらを渡すことができます保存されています。変更があった場合は、クラスメソッドを利用することができます:

def save(self, *args, **kwargs): 
    """ 
    This function overrides the standard save function. The application 
    is still saved, however, other functions are fired when it is 
    saved. 
    :return: creates reversal commission and dealership transactions 
    if the status is CANCELLED 
    """ 
    if self.signed and self.status != self.__initial_status: 
     if self.status == Application.CANCELLED: 
      Transactions.create_commision_trasaction(app=self, 
      reverse=True) 
      Transactions.create_dealership_trasaction(app=self, 
      reverse=True) 
     elif self.status == Application.OK: 
      Transactions.create_commision_trasaction(app=self, 
      reverse=False) 
      Transactions.create_dealership_trasaction(app=self, 
      reverse=False) 

    if self.signed and self.feeadded != self.__initial_fee_status: 
     if self.feeadded: 
      Transactions.late_payment_fee(app=self, desired_action=True) 
     elif self.feeadded is False: 
      Transactions.late_payment_fee(app=self, desired_action=False) 

    super(Application, self).save(*args, **kwargs) 
関連する問題