Arkadaşlar selamlar, chatgpt’ ye de danıştığım halde aşağıdaki koduma eklediğim dinamik alanlar export yaptığımda eklenmiyor. get_dynamic_calculations fonksiyonundan gelmesi lazım. Sebebi ne olabilir?
from app import db
import pandas as pd
from app.base_model import BaseModel
from masterdata.models import MasterCountry
from calculator.models import CalculatorType
from sqlalchemy import text
class Customer(BaseModel):
__tablename__ = 'customer'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
customer_no = db.Column(db.String(255), nullable=True, unique=False, index=True)
sold_to_party = db.Column(db.String(255), nullable=False)
master_country_id = db.Column(db.Integer, db.ForeignKey('master_country.id'), nullable=True, index=True)
sales_rep = db.Column(db.String(255), nullable=True)
certificates = db.relationship('GreenCertificate', secondary='customer_certificates', backref=db.backref('customers', lazy='dynamic'))
business_sub_division_id = db.Column(db.Integer, db.ForeignKey('business_sub_division.id'), nullable=True, index=True)
major_customer_group_id = db.Column(db.Integer, db.ForeignKey('major_customer_group.id'), nullable=True, index=True)
global_parent_id = db.Column(db.Integer, db.ForeignKey('global_parent.id'), nullable=True, index=True)
regional_parent_id = db.Column(db.Integer, db.ForeignKey('regional_parent.id'), nullable=True, index=True)
customer_image = db.Column(db.Text, nullable=True)
calculators = db.relationship('CalculatorType', secondary='customer_calculator', backref=db.backref('customers', lazy='dynamic'))
calculations = db.relationship('Calculation', backref='customer', uselist=True, lazy=True)
tasks = db.relationship('Task', backref='customer', lazy=True)
def __repr__(self):
return f"Customer(name='{self.name}', sold_to_party='{self.sold_to_party}', location_id='{self.location_id}', sales_rep='{self.sales_rep}')"
def get_dynamic_calculations(self, calculation_id):
sql_query = text("""
SELECT cfd.name, cfv.value
FROM calculation_field_value cfv
INNER JOIN calculation_field_definition cfd ON cfv.calculation_field_definition_id = cfd.id
WHERE cfv.calculation_id = calculation_id
""")
result = db.session.execute(sql_query, {'calculation_id': calculation_id})
calculations = {}
for row in result:
calculations[row.name] = row.value
return calculations
def serialize_for_export(self):
# Diğer statik alanlar
serialized_data = self.serialize(is_deep=True, add_calculations=True, add_calculators=True,
add_master_country=True, add_business_sub_division=True,
add_major_customer_group=True, add_global_parent=True,
add_regional_parent=True)
# Dinamik hesaplamaları alıyoruz
calculation_id = self.calculations[0].id if self.calculations else None
if calculation_id:
dynamic_calculations = self.get_dynamic_calculations(calculation_id=calculation_id)
print("Dynamic Calculations:", dynamic_calculations) # Kontrol
# Sorgudan gelen name ve value değerlerini dinamik olarak ekliyoruz
for index, (name, value) in enumerate(dynamic_calculations.items(), start=1):
serialized_data[f'calculation_field_{index}'] = value
print("Serialized Data for Export:", serialized_data) # Kontrol
return serialized_data
def serialize(self, is_deep=True,
add_calculations=True,
add_calculators=True,
add_master_country=True,
add_business_sub_division=True,
add_major_customer_group=True,
add_global_parent=True,
add_regional_parent=True):
serialized_data = {
'id': self.id,
'name': self.name,
'customer_no': self.customer_no,
'sold_to_party': self.sold_to_party,
'master_country_id': self.master_country_id,
'sales_rep': self.sales_rep,
'certificates': [certificate.serialize() for certificate in self.certificates],
'business_sub_division_id': self.business_sub_division_id,
'major_customer_group_id': self.major_customer_group_id,
'global_parent_id': self.global_parent_id,
'regional_parent_id': self.regional_parent_id,
'calculators':[calculator.serialize(is_deep=False) for calculator in self.calculators] if add_calculators else [],
'calculations': [calculation.serialize(is_deep=is_deep) for calculation in self.calculations] if add_calculations else [],
'created_at': self.created_at,
'updated_at': self.updated_at,
'customer_image': self.customer_image
}
if self.master_country and add_master_country:
serialized_data['master_country'] = self.master_country.serialize()
if self.business_sub_division and add_business_sub_division:
serialized_data['business_sub_division'] = self.business_sub_division.serialize()
if self.major_customer_group and add_major_customer_group:
serialized_data['major_customer_group'] = self.major_customer_group.serialize()
if self.global_parent and add_global_parent:
serialized_data['global_parent'] = self.global_parent.serialize()
if self.regional_parent and add_regional_parent:
serialized_data['regional_parent'] = self.regional_parent.serialize()
return serialized_data
def serialize_for_export(self):
serialized_data = {
'name': self.name,
'sold_to_party': self.sold_to_party,
'sales_rep': self.sales_rep
}
if self.master_country:
serialized_data['master_country'] = self.master_country.name
if self.certificates:
i=1
for cerificate in self.certificates:
serialized_data['certificate'+str(i)] = cerificate.name
i+=1
if self.business_sub_division:
serialized_data['business_sub_division'] = self.business_sub_division.name
if self.major_customer_group:
serialized_data['major_customer_group'] = self.major_customer_group.name
if self.global_parent:
serialized_data['global_parent'] = self.global_parent.name
if self.regional_parent:
serialized_data['regional_parent'] = self.regional_parent.name
return serialized_data
class CustomerCerificates(BaseModel):
__tablename__ = 'customer_certificates'
id = db.Column(db.Integer, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'), nullable=False)
certificate_id = db.Column(db.Integer, db.ForeignKey('green_certificate.id'), nullable=False)
def __repr__(self):
return f"CustomerCerificates(customer_id='{self.customer_id}', certificate_id='{self.certificate_id}', valid_from='{self.valid_from}', valid_to='{self.valid_to}')"
def serialize_for_export(self):
return self.serialize()
def serialize(self, is_deep=True):
return {
'id': self.id,
'customer_id': self.customer_id,
'certificate_id': self.certificate_id,
'created_at': self.created_at,
'updated_at': self.updated_at
}
class CustomerCalculator(db.Model):
__tablename__ = 'customer_calculator'
id = db.Column(db.Integer, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'), nullable=False)
calculator_type_id = db.Column(db.Integer, db.ForeignKey('calculator_type.id'), nullable=False)
def __repr__(self):
return f"CustomerCalculator(customer_id='{self.customer_id}', calculator_id='{self.calculator_id}')"
def serialize(self, is_deep=True):
return {
'id': self.id,
'customer_id': self.customer_id,
'calculator_id': self.calculator_id,
}
class GreenCertificate(BaseModel):
__tablename__ = 'green_certificate'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.String(255), nullable=False)
logo = db.Column(db.Text, nullable=True)
color = db.Column(db.String(255), nullable=True)
def __repr__(self):
return f"GreenCertificate(name='{self.name}', description='{self.description}', certificate_type='{self.certificate_type}', valid_from='{self.valid_from}', valid_to='{self.valid_to}', customer_id='{self.customer_id}')"
def serialize_for_export(self):
return self.serialize()
def serialize(self, is_deep=True):
return {
'id': self.id,
'name': self.name,
'description': self.description,
'logo': self.logo,
'color': self.color,
'created_at': self.created_at,
'updated_at': self.updated_at
}
class BusinessSubDivision(BaseModel):
__tablename__ = 'business_sub_division'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.String(255), nullable=False)
customers = db.relationship('Customer', backref='business_sub_division', lazy=True)
major_customer_groups = db.relationship('MajorCustomerGroup', backref='business_sub_division', lazy=True)
def __repr__(self):
return f"BusinessSubDivision(name='{self.name}', description='{self.description}', logo='{self.logo}')"
def serialize(self, is_deep=True):
return {
'id': self.id,
'name': self.name,
'description': self.description,
'created_at': self.created_at,
'updated_at': self.updated_at
}
class MajorCustomerGroup(BaseModel):
__tablename__ = 'major_customer_group'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.String(255), nullable=False)
business_sub_division_id = db.Column(db.Integer, db.ForeignKey('business_sub_division.id'), nullable=True)
global_parents = db.relationship('GlobalParent', backref='major_customer_group', lazy=True)
customers = db.relationship('Customer', backref='major_customer_group', lazy=True)
def __repr__(self):
return f"MajorCustmerGroup(name='{self.name}', description='{self.description}')"
def serialize(self, is_deep=True):
return {
'id': self.id,
'name': self.name,
'description': self.description,
'business_sub_division_id': self.business_sub_division_id,
'created_at': self.created_at,
'updated_at': self.updated_at
}
class GlobalParent(BaseModel):
__tablename__ = 'global_parent'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.String(255), nullable=False)
major_customer_group_id = db.Column(db.Integer, db.ForeignKey('major_customer_group.id'), nullable=True)
regional_parents = db.relationship('RegionalParent', backref='global_parent', lazy=True)
customers = db.relationship('Customer', backref='global_parent', lazy=True)
def __repr__(self):
return f"GlobalParent(name='{self.name}', description='{self.description}')"
def serialize(self, is_deep=True):
serialized_data = {
'id': self.id,
'name': self.name,
'description': self.description,
'major_customer_group_id': self.major_customer_group_id,
'created_at': self.created_at,
'updated_at': self.updated_at
}
if self.major_customer_group:
serialized_data['major_customer_group'] = self.major_customer_group.serialize(is_deep=is_deep) if is_deep else self.major_customer_group.id
return serialized_data
class RegionalParent(BaseModel):
__tablename__ = 'regional_parent'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.String(255), nullable=False)
global_parent_id = db.Column(db.Integer, db.ForeignKey('global_parent.id'), nullable=True)
customers = db.relationship('Customer', backref='regional_parent', lazy=True)
def __repr__(self):
return f"RegionalParent(name='{self.name}', description='{self.description}')"
def serialize(self, is_deep=True):
serialized_data ={
'id': self.id,
'name': self.name,
'description': self.description,
'global_parent_id': self.global_parent_id,
'created_at': self.created_at,
'updated_at': self.updated_at
}
if self.global_parent:
serialized_data['global_parent'] = self.global_parent.serialize(is_deep=is_deep) if is_deep else self.global_parent.id
return serialized_data