Hi django-developers,

a generic api for registration of new fields would be a good idea.

As IOC, FIFA and ISO-3166 codes are not the same this won't help me:
http://en.wikipedia.org/wiki/Comparison_of_IOC%2C_FIFA%2C_and_ISO_3166_country_codes

But IOC, FIFA and ISO-3166 should have the same api.
The only difference is the content validated against.

USStateField: length 2, isValidUSState-validator
OlympicNationField: length 3, isValidOlympicNation-validator
ISO3166Field: length 3, isValidISO3166-validator
FIFANationField: length 3, isValidFIFANation-validator

The length differs by 1.

Each validator as one difference compared to the others, the valid content, but 
thats all.

There should be a parent class for this fields to realize:
variable in length and variable in valid content.

Regards,
Dirk
-- 


"Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail

-- 


"Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---
>from django.db.models.fields import Field
import forms

class OlympicNationField(Field):
    def get_manipulator_field_objs(self):
        return [forms.OlympicNationField]


>from django.forms import TextField
import validators

class OlympicNationField(TextField):
    "A convenience FormField for validating Olympic Nations (e.g. 'GER')"
    def __init__(self, field_name, is_required=False, validator_list=None):
        if validator_list is None: validator_list = []
        validator_list = [self.isValidOlympicNation] + validator_list
        TextField.__init__(self, field_name, length=3, maxlength=3,
            is_required=is_required, validator_list=validator_list)

    def isValidOlympicNation(self, field_data, all_data):
        try:
            validators.isValidOlympicNation(field_data, all_data)
        except validators.ValidationError, e:
            raise validators.CriticalValidationError, e.messages

    def html2python(data):
        if data:
            return data.upper() # Should always be stored in upper case
        else:
            return None
    html2python = staticmethod(html2python)


>from django.core.validators import ValidationError, CriticalValidationError
>from django.utils.translation import gettext

def isValidOlympicNation(field_data, all_data):
    """Checks that the given string is a valid three-letter Olympic Nation abbreviation"""

    nations = [ 'AFG','AHO','ALB','ALG','AND','ANG','ANT','ARG','ARM','ARU','ASA','AUS','AUT','AZE','BAH','BAN','BAR','BEL','BEN','BER','BHU','BIH','BIZ','BLR','BOH','BOL','BOT','BRA','BRU','BUL','BUR','BWI','CAF','CAM','CAN','CAY','CEY','CGO','CHA','CHI','CHN','CIV','CMR','COK','COL','COM','CRC','CRO','CUB','CVP','CYP','CZE','DAH','DEN','DJI','DMA','DOM','ECU','EGY','ESA','ESP','EST','ETH','FIJ','FIN','FRA','FRG','GAB','GAM','GBR','GBS','GDR','GEO','GEQ','GER','GHA','GRE','GRN','GUA','GUM','GUY','HAI','HBR','HKG','HON','HUN','IAS','INA','IRI','IRL','IRQ','ISL','ISR','ITA','JAM','JOR','JPN','KEN','KGZ','KOR','KSA','KUW','LAO','LAT','LBA','LBR','LCA','LES','LIB','LIE','LTU','LUX','MAD','MAR','MAS','MAW','MDA','MDV','MEX','MGL','MKD','MLI','MLT','MON','MOZ','MRI','MTN','MYA','NAM','NCA','NED','NEP','NGR','NIG','NOR','NRU','NZL','OMA','PAK','PAN','PAR','PER','PHI','PLE','PNG','POL','POR','PUR','QAT','ROC','ROM','RSA','RUS','RWA','SAM','SEN','SEY','SIN','SLE','SLO','SMR','SOL','SOM','SRI','STP','SUD','SUI','SUR','SVK','SWE','SWZ','SYR','TAN','TCH','TGA','THA','TJK','TKM','TOG','TPE','TRI','TUN','TUR','UAE','UGA','UKR','URS','URU','USA','UZB','VAN','VEN','VIE','VIN','YEM','YUG','ZAI','ZAM','ZIM', ]

    if field_data.upper() not in nations:
        raise ValidationError, gettext("Enter a valid Olympic Nation abbreviation.")

Reply via email to