63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
import re
|
|
from datetime import datetime
|
|
|
|
from django.db import models
|
|
from django.core.validators import MinLengthValidator
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
|
def validate_rfc(value):
|
|
l = 4
|
|
if len(value)==12:
|
|
l = 3
|
|
s = value[0:l]
|
|
r = re.match('[A-ZÑ&]{%s}' % l, s)
|
|
if not r:
|
|
raise ValidationError('Caracteres inválidos al inicio del RFC')
|
|
|
|
s = value[-3:]
|
|
r = re.match('[A-Z0-9]{3}', s)
|
|
if not r:
|
|
raise ValidationError('Caracteres inválidos al final del RFC')
|
|
|
|
s = value[l:l+6]
|
|
r = re.match('[0-9]{6}', s)
|
|
msg = 'Fecha inválida en el RFC'
|
|
if not r:
|
|
raise ValidationError(msg)
|
|
|
|
try:
|
|
datetime.strptime(s,"%y%m%d")
|
|
except:
|
|
raise ValidationError(msg)
|
|
|
|
|
|
class RFCField(models.CharField):
|
|
description = 'Field to RFC of México'
|
|
default_validators = [MinLengthValidator(12), validate_rfc]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
kwargs['max_length'] = 13
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def to_python(self, value):
|
|
return value.upper()
|
|
|
|
|
|
class Clients(models.Model):
|
|
rfc = RFCField('RFC', unique=True)
|
|
token = models.CharField('Token', max_length=100)
|
|
key = models.BinaryField('Key', default=b'', blank=True)
|
|
cer = models.BinaryField('Cer', default=b'', blank=True)
|
|
serial_number = models.CharField('Fiel Serie', default='', blank=True, max_length=100)
|
|
date_from = models.DateTimeField('Desde', null=True, blank=True)
|
|
date_to = models.DateTimeField('Hasta', null=True, blank=True)
|
|
|
|
class Meta:
|
|
ordering = ['rfc']
|
|
verbose_name = 'Cliente'
|
|
verbose_name_plural = 'Clientes'
|
|
|
|
def __str__(self):
|
|
return self.rfc
|