Last active
November 7, 2018 06:08
-
-
Save rg3915/fd8ab5378dec24db6bbfab189c501972 to your computer and use it in GitHub Desktop.
Convert simple annotation to Django annotation model Class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Convert simple annotation to Django annotation model Class. | |
Type: | |
cat << EOF > file.txt | |
first_name, char, null, blank | |
last_name, char, null, blank | |
description, text, null, blank | |
active, bool | |
gender, nullbool | |
birthday, date, null, blank | |
created, datetime, null, blank | |
price, decimal, null, blank | |
email, email, null, blank | |
quantity, int, null, blank | |
age, posint, null, blank | |
height, float, null, blank | |
slug, slug, null, blank | |
user, fk(User), null, blank | |
book, m2m(Book) | |
contract, o2o(Contract) | |
EOF | |
python3 convert_annotation.py | |
cat output.txt | |
xclip -sel clip < output.txt | |
https://docs.djangoproject.com/pt-br/2.1/ref/models/fields/ | |
''' | |
with open('file.txt') as f: | |
_text = f.read() | |
text = _text.replace(' ', '').split('\n') | |
if not text[-1]: | |
text = text[:-1] | |
def transform(item): | |
t = item.split(',') | |
field = t[0] | |
_type_field = t[1] | |
_model = '' | |
if 'null' or 'blank' in t[2:]: | |
_nb = ['%s=True,' % j for j in t[2:]] | |
nb = ''.join(_nb)[:-1] | |
if 'fk' in t[1]: | |
_type_field = 'fk' | |
_model = t[1][3:-1] | |
if 'm2m' in t[1]: | |
_type_field = 'm2m' | |
_model = t[1][4:-1] | |
if 'o2o' in t[1]: | |
_type_field = 'o2o' | |
_model = t[1][4:-1] | |
type_field = { | |
'char': 'models.CharField(, max_length=100,{})'.format(nb), | |
'text': 'models.TextField({})'.format(nb), | |
'bool': 'models.BooleanField()', | |
'nullbool': 'models.NullBooleanField()', | |
'date': 'models.DateField({})'.format(nb), | |
'datetime': 'models.DateTimeField({})'.format(nb), | |
'decimal': 'models.DecimalField(\n\tmax_digits=5,\n\tdecimal_places=2,\n\t{}\n)'.format(nb), | |
'email': 'models.EmailField({})'.format(nb), | |
'int': 'models.IntegerField({})'.format(nb), | |
'posint': 'models.PositiveIntegerField({})'.format(nb), | |
'float': 'models.FloatField({})'.format(nb), | |
'slug': 'models.SlugField({})'.format(nb), | |
'fk': "models.ForeignKey(\n\t{},\n\tverbose_name='',\n\ton_delete=models.CASCADE,{}\n)".format(_model, nb), | |
'm2m': "models.ManyToManyField(\n\t{},\n\tverbose_name=''\n)".format(_model), | |
'o2o': "models.OneToOneField(\n\t{},\n\tverbose_name='',\n\ton_delete=models.CASCADE\n)".format(_model), | |
} | |
res = '{} = {}'.format(field, type_field[_type_field]) | |
return res | |
result = [transform(t) for t in text] | |
for item in result: | |
print(item) | |
print('\n' + 'Type:' + '\n' + 'xclip -sel clip < output.txt') | |
with open('output.txt', 'w') as f: | |
for item in result: | |
f.write(item + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment