from django import forms from .models import Organization import json from django.utils.safestring import mark_safe class OrganizationForm(forms.ModelForm): class Meta: model = Organization fields = [ 'name', 'email', 'employee_headcount', 'annual_revenue', 'critical_applications', 'compliance_frameworks', 'industry_sector', 'it_dependency', 'network_infrastructure', 'remote_workforce_percentage', 'third_party_vendor_access', 'internal_software_development', 'geographic_scope', 'customer_base', 'customer_type', 'product_portfolio', 'supplier_base', 'it_infrastructure', 'sensitive_data_types', 'integration_level', 'change_rate', 'threat_actors', 'expert_analysis' ] widgets = { 'compliance_frameworks': forms.CheckboxSelectMultiple(), 'it_infrastructure': forms.CheckboxSelectMultiple(), 'threat_actors': forms.CheckboxSelectMultiple(), 'sensitive_data_types': forms.CheckboxSelectMultiple(), } def clean(self): cleaned_data = super().clean() # Handle compliance_frameworks "Other" frameworks = cleaned_data.get('compliance_frameworks', []) other_framework = self.data.get('compliance_frameworks_other', '').strip() if 'other' in frameworks and other_framework: frameworks = [fw for fw in frameworks if fw != 'other'] frameworks.append(other_framework) cleaned_data['compliance_frameworks'] = frameworks # Handle industry_sector "Other" sector = cleaned_data.get('industry_sector') sector_other = self.data.get('industry_sector_other', '').strip() if sector == 'other' and sector_other: cleaned_data['industry_sector'] = sector_other # Handle Sensitive Data Types & Business Impact sensitive_data_types = {} data_types = [ ('personal', 'personal_applicable', 'personal_impact'), ('financial', 'financial_applicable', 'financial_impact'), ('ip', 'ip_applicable', 'ip_impact'), ('operational', 'operational_applicable', 'operational_impact'), ('government', 'government_applicable', 'government_impact'), ('none', 'none_applicable', None) ] for key, applicable_name, impact_name in data_types: applicable = self.data.get(applicable_name) == 'on' entry = {'applicable': applicable} if impact_name: impact = self.data.get(impact_name) entry['impact'] = int(impact) if impact and impact.isdigit() else None sensitive_data_types[key] = entry cleaned_data['sensitive_data_types'] = sensitive_data_types return cleaned_data class GenerateCodesForm(forms.Form): count = forms.IntegerField(label="How many codes to generate?", min_value=1, max_value=1000) class ContactForm(forms.Form): name = forms.CharField(label="Name", max_length=100) email = forms.EmailField(label="Email") message = forms.CharField(label="Message", widget=forms.Textarea(attrs={"rows": 6}), max_length=5000) class RiskExplanationWidget(forms.Widget): def render(self, name, value, attrs=None, renderer=None): if isinstance(value, str): try: value = json.loads(value) except Exception: value = {} value = value or {} html = ( '
| Risk ID | ' 'Explanation | ' '
|---|---|
| ' f' | ' f' |
| ' f' | ' f' |