Untitled
unknown
plain_text
10 months ago
3.3 kB
2
Indexable
from import_export import resources,fields
from import_export.fields import Field
from import_export.widgets import ForeignKeyWidget, BooleanWidget
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
from .models import *
from django.db.models import F
from import_export.formats import base_formats
from import_export.widgets import CharWidget
from import_export import resources, widgets
# Widget for program_name with custom validation
class CNWIDGET(widgets.CharWidget):
def __init__(self, max_length=200, required=True, *args, **kwargs):
# Accept dynamic parameters for max_length and required
self.max_length = max_length
self.required = required
super(CNWIDGET, self).__init__(*args, **kwargs)
def clean(self, value):
# Ensure value is stripped of whitespace
value = value.strip()
# Check for required field
if self.required and not value:
raise ValidationError(u'program_name is mandatory')
# Enforce max_length constraint
if len(value) > self.max_length:
raise ValidationError(u'program_name can have a max length of {} characters'.format(self.max_length))
return value
# Widget for program_code with custom validation
class CIWIDGET(widgets.CharWidget):
def clean(self, value):
value = value.strip()
if not value:
raise ValidationError(u'program_code is mandatory')
if len(value) > 20:
raise ValidationError(u'program_code can have a max length of 20 characters')
return value
class ProgramResource(resources.ModelResource):
program_name = fields.Field(
column_name='program_name',
attribute='program_name',
widget=CNWIDGET(max_length=200, required=True)
)
program_code = fields.Field(
column_name='program_code',
attribute='program_code',
widget=CIWIDGET()
)
program_name = fields.Field(column_name='program_name', attribute='program_name', widget=CNWIDGET())
specific_program = fields.Field(column_name='specific_program', attribute='specific_program')
program_code = fields.Field(column_name='program_code', attribute='program_code', widget=CIWIDGET())
client_organization = fields.Field(column_name='client_organization', attribute='client_organization')
current_semester = fields.Field(column_name='current_semester', attribute='current_semester')
semester_start_date = fields.Field(column_name='semester_start_date', attribute='semester_start_date')
semester_end_date = fields.Field(column_name='semester_end_date', attribute='semester_end_date')
ofr_letter_esc_days = fields.Field(column_name='ofr_letter_esc_days', attribute='ofr_letter_esc_days')
class Meta:
model = Program
fields = (
'program_id','specific_program','program_code', 'program_name',
'client_organization', 'current_semester', 'semester_start_date',
'semester_end_date', 'ofr_letter_esc_days'
)
export_order = (
'program_id', 'specific_program','program_code', 'program_name',
'client_organization', 'current_semester', 'semester_start_date',
'semester_end_date', 'ofr_letter_esc_days'
)
Editor is loading...
Leave a Comment