1

Hi can someone please help me in reducing the complexity of the below mentioned code as I am new to this I need it to reduce the amount of code and improve the code and to improve simplicity and reduce duplications in the overall coding any any help in this regard can be of great help and thanks in advance for your time and consideration in helping me in this regard.

def update(self, instance, validated_data):

    instance.email_id = validated_data.get('email_id', instance.email_id)
    instance.email_ml_recommendation = validated_data.get('email_ml_recommendation',
                                                          instance.email_ml_recommendation)
    instance.ef_insured_name = validated_data.get('ef_insured_name', instance.ef_insured_name)
    instance.ef_broker_name = validated_data.get('ef_broker_name', instance.ef_broker_name)
    instance.ef_obligor_name = validated_data.get('ef_obligor_name', instance.ef_obligor_name)
    instance.ef_guarantor_third_party = validated_data.get('ef_guarantor_third_party',
                                                           instance.ef_guarantor_third_party)
    instance.ef_coverage = validated_data.get('ef_coverage', instance.ef_coverage)
    instance.ef_financials = validated_data.get('ef_financials', instance.ef_financials)
    instance.ef_commercial_brokerage = validated_data.get('ef_commercial_brokerage',
                                                          instance.ef_commercial_brokerage)
    # fixing bug of pipeline

    instance.ef_underwriter_decision = validated_data.get('ef_underwriter_decision',
                                                          instance.ef_underwriter_decision)
    instance.ef_decision_nty_fields = validated_data.get('ef_decision_nty_fields', 
                                                   instance.ef_decision_nty_fields)
    instance.ef_feedback = validated_data.get('ef_feedback', instance.ef_feedback)

    instance.relation_id = validated_data.get('relation_id', instance.relation_id)
    instance.email_outlook_date = validated_data.get('email_outlook_date', 
                                                           instance.email_outlook_date)
    instance.ef_decision_nty_fields = validated_data.get('ef_decision_nty_fields', 
                                                           instance.ef_decision_nty_fields)
    instance.ef_pl_est_premium_income = validated_data.get('ef_pl_est_premium_income',
                                                           instance.ef_pl_est_premium_income)
    instance.ef_pl_prob_closing = validated_data.get('ef_pl_prob_closing', 
                                                                       instance.ef_pl_prob_closing)
    instance.ef_pl_time_line = validated_data.get('ef_pl_time_line', instance.ef_pl_time_line)
    instance.ef_pipeline = validated_data.get('ef_pipeline', instance.ef_pipeline)
    instance.el_insured_margin = validated_data.get('el_insured_margin', instance.el_insured_margin)
    instance.ef_last_updated = validated_data.get('ef_last_updated', instance.ef_last_updated)
    instance.relation_id = validated_data.get('relation_id', instance.relation_id)
    # CR3.2 Primium and basis point addition
    instance.broker_email_id = validated_data.get('broker_email_id', instance.broker_email_id)
    instance.premium = validated_data.get('premium', instance.premium)
    instance.basis_points = validated_data.get('basis_points', instance.basis_points)
    instance.basis_points_decision = validated_data.get('basis_points_decision', 
                                                           instance.basis_points_decision)

    embedded_json = validated_data.get('email_embedd', instance.email_embedd)
    instance.email_embedd = json.dumps(embedded_json)
Murthy P
  • 15
  • 1
  • 3
  • 8

1 Answers1

1

If all items in your dictionary validated_data that have a corresponding attribute in instance have to be copied to that instance, then iterate those items and use setattr to set the corresponding attributes of your instance object.

You seem to have one special case where a value needs to be stringified as JSON. So you'll need specific code to deal with that scenario:

def update(self, instance, validated_data):
    for key, value in validated_data.items():
        if hasattr(instance, key):
            if key == "email_embedd":  # special case
                instance.email_embedd = json.dumps(value)
            else:
                setattr(instance, key, value) 

A Logical Error...

There is a problem in your code for the special case:

embedded_json = validated_data.get('email_embedd', instance.email_embedd)
instance.email_embedd = json.dumps(embedded_json)

If this gets executed when validated_data does not have the key email_embedd, then embedded_json will default to instance.email_embedd. But that value is already JSON encoded! So if you now proceed with json.dumps(embedded_json) you'll end up with a JSON string that itself has been stringified again!

This problem will not occur with the code proposed above.

trincot
  • 317,000
  • 35
  • 244
  • 286