1

I am pretty beginner with Python and Django and I would like to know How I could improve few Python lines with a better Pythonic syntax.

Reasons :

I have two tables : BirthCertificate and Identity which have a folderId field. This field corresponds to a directory ID in LogicalDOC web application. When I create different PDF documents to the same person, all documents have to go on the same directory thanks to this directory ID.

My objective :

I fill an Identity form, my function create a new directory if the person doesn't exist (and by the same way, I get a new directory ID) or takes an existing ID directory if the person already exists.

With BirthCertificate table, I want to know if the same person already exists in Identity table.

If yes : I take the Identity folderID and I update my BirthCertificate table with this number

If no : I redirect user to Identity form.

Why ?

Because users have to create Identity form before BirthCertificate form.

In my script, When I generate BirthCertificate PDF to a person X, I want to check before if the person X is already registered in Identity. If yes, I take the Identity folderID and put the same number in BirthCertificate folderID. Else, I redirect to Identity form creation.

This is the interesting part from my script :

if BirthCertificate.objects.get(pk=id).lastname == Identity.objects.get(pk=id).lastname :
    if BirthCertificate.objects.get(pk=id).firstname == Identity.objects.get(pk=id).firstname :
        if BirthCertificate.objects.get(pk=id).birthday == Identity.objects.get(pk=id).birthday :
            if BirthCertificate.objects.get(pk=id).birthcity == Identity.objects.get(pk=id).birthcity :
                if Identity.objects.exclude(folderId__isnull=True) :
                    BirthCertificate.objects.filter(pk=id).update(folderId=Identity.objects.get(pk=id).folderId)
                else : 
                    return HttpResponseRedirect(reverse('home'))

How I could improve this Python part in order to check to each step if I can continue to the next one or redirect to home ?

EDIT :

Identity models.py :

#-*- coding: utf-8 -*-

from django.db import models
from django.utils.encoding import force_text
from django_countries.fields import CountryField


######################################
# Choix à l'utilisateur pour le sexe #
######################################

SEX_CHOICES = (
    ('Masculin', 'Masculin'),
    ('Feminin', 'Feminin')
)
##########################################
# Choix à l'utilisateur pour la civilité #
##########################################

TITLE_CHOICES = (
    ('Mr', 'Monsieur'),
    ('Mlle', 'Mademoiselle'),
    ('Mme','Madame'),
    ('Dr','Docteur'),
    ('Me','Maître'),
)


####################################################################################
# Création d'une table permettant de renseigner toutes les informations concernant #
#                les parents et reprise de celles des enfants                      #
####################################################################################

class Identity(models.Model):

    title = models.CharField(max_length=12,choices=TITLE_CHOICES, verbose_name='Civilité')
    lastname = models.CharField(max_length=30, verbose_name='Nom de famille')
    firstname = models.CharField(max_length=30, verbose_name='Prénom(s)')
    sex = models.CharField(max_length=8, choices=SEX_CHOICES, verbose_name='Sexe')
    birthday = models.DateField(verbose_name='Date de naissance')
    birthcity = models.CharField(max_length=30, verbose_name='Ville de naissance')
    birthcountry = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays de naissance')
    nationality = models.CharField(max_length=30, verbose_name='Nationalité')
    job = models.CharField(max_length=30, verbose_name='Profession')
    adress = models.CharField(max_length=30, verbose_name='Adresse')
    city = models.CharField(max_length=30, verbose_name='Ville')
    zip = models.IntegerField(verbose_name='Code Postal')
    country = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays')
    mail = models.CharField(max_length=30, verbose_name='Email', blank=True)
    phone = models.CharField(max_length=20, verbose_name='Téléphone', blank=True)
    folderId = models.CharField(max_length=15, null=True)

    def __unicode__(self):
         return '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s' % (self.id, self.title, self.lastname, self.firstname, self.sex, self.birthday, self.birthcity, self.birthcountry,
                                                                     self.nationality, self.job, self.adress, self.city, self.zip, self.country, self.mail, self.phone)

BirthCertificate models.py :

#-*- coding: utf-8 -*-

from django.db import models
from Identity.models import Identity
from django.utils.encoding import force_text
from django_countries.fields import CountryField

######################################
# Choix à l'utilisateur pour le sexe #
######################################

SEX_CHOICES = (
    ('Masculin', 'Masculin'),
    ('Feminin', 'Feminin')
)

####################################################################################
# Création d'une table permettant de renseigner toutes les informations concernant #
#               l'enfant et reprise des champs pour les parents                    #
####################################################################################

class BirthCertificate(models.Model):

    lastname = models.CharField(max_length=30, null=False, verbose_name='Nom de famille')
    firstname = models.CharField(max_length=30, null=False, verbose_name='Prénom(s)')
    sex = models.CharField(max_length=8, choices=SEX_CHOICES, verbose_name='Sexe')
    birthday = models.DateField(null=False, verbose_name='Date de naissance')
    birthhour = models.TimeField(null=True, verbose_name='Heure de naissance')
    birthcity = models.CharField(max_length=30, null=False, verbose_name='Ville de naissance')
    birthcountry = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays de naissance')
    fk_parent1 = models.ForeignKey(Identity, related_name='ID_Parent1', verbose_name='ID parent1', null=False)
    fk_parent2 = models.ForeignKey(Identity, related_name='ID_Parent2', verbose_name='ID parent2', null=False)
    folderId = models.CharField(max_length=15, null=True)

This is my entire function which lets me to generate PDF file and send it to the good directory :

@login_required
def BirthCertificate_PDF(request, id) :


    birthcertificate = get_object_or_404(BirthCertificate, pk=id)

    data = {"birthcertificate" : birthcertificate}

    template = get_template('BC_raw.html')
    html  = template.render(Context(data))


    filename_directory = str(BirthCertificate.objects.get(pk=id).lastname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).firstname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).birthday)
    filename = 'Acte_Naissance_' + filename_directory + '.pdf'
    path = '/Users/valentinjungbluth/Desktop/Django/Individus/' + filename


    file = open(path, "w+b") 
    pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')
    file.close()

    # Get FolderID from Identity and fill Birthcertificate FolderID field

    if BirthCertificate.objects.get(pk=id).lastname == Identity.objects.get(pk=id).lastname :
        if BirthCertificate.objects.get(pk=id).firstname == Identity.objects.get(pk=id).firstname :
            if BirthCertificate.objects.get(pk=id).birthday == Identity.objects.get(pk=id).birthday :
                if BirthCertificate.objects.get(pk=id).birthcity == Identity.objects.get(pk=id).birthcity :
                    if Identity.objects.exclude(folderId__isnull=True) :
                        BirthCertificate.objects.filter(pk=id).update(folderId=Identity.objects.get(pk=id).folderId)
                    else : 
                        return HttpResponseRedirect(reverse('home'))

    #elif folderID is not null (corresponding to an existing folder), just save pdf file inside the folder. DON'T CREATE a new one.
    if BirthCertificate.objects.filter(pk=id).exclude(folderId__isnull=True) :
        payload = '{{ "language":"fr","fileName":"{0}","folderId": "{1}" }}'.format(filename, BirthCertificate.objects.get(pk=id).folderId)  
        upfile = path
        files = { 
        'document': (None, payload, 'application/json'),
        'content': (os.path.basename(upfile), open(upfile, 'rb'), 'application/octet-stream')
        } 
        url = 'http://demoged.datasystems.fr:8090/services/rest/document/create'
        headers = {'Content-Type': 'multipart/form-data'}
        r = requests.post(url, files=files, headers=headers, auth=('etatcivil', '100%EC67'))

        for element in glob.glob(path) :
            os.remove(element)


    context = {"birthcertificate":birthcertificate,
               "path":path,
    }


    return render(request, 'BC_PDF.html', context)
5
  • Can you post your models? Commented Feb 6, 2017 at 11:12
  • @MicroPyramid Yes of course, I will edit my question. I tried to make BirthCertificate FolderID ForeignKey from Identity but I get some problems because I need the entire ID directory in all tables. Commented Feb 6, 2017 at 11:13
  • BirthCertificate.objects.get(pk=id).lastname == Identity.objects.get(pk=id).lastname How you are getting id here and is the ID same for BirthCertificate and Identity models? Commented Feb 6, 2017 at 11:21
  • ID is not the same between BirthCertificate and Identity. BirthCertificate(pk=id) lets me to handle the current BirthCertificate. That's why I compare lastname, firstname, ... in order to use the same person. Commented Feb 6, 2017 at 11:24
  • Are you creating any relational object to BirthCertificate with Identity or just saving the them independently? Commented Feb 6, 2017 at 11:36

2 Answers 2

1

Before anything else you can at least avoid repeating db lookups. For example do this:

identity = Identity.objects.get(pk=id)
birth_certificate = BirthCertificate.objects.get(pk=id)

Then apply your conditional logic on these objects. For example:

if birth_certificate.lastname == identity.lastname and birth_certificate.firstname == identity.firstname:rstname :
  # Do something , etc.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, as you can see in my edited question, I already had something like this ;) But your answer is better
1

You can use this query for checking if the same object exist in Identity table

person = BirthCertificate.objects.get(pk=id)
try :
    identity_object = Identity.objects.get(firstname=person.firstname, lastname=person.lastname, birthcity=person.birthcity, birthday=person.birthday)
    if Identity.objects.exclude(folderId__isnull=True):
        BirthCertificate.objects.filter(pk=id).update(folderId=identity_object.folderId)
    else : 
        return HttpResponseRedirect(reverse('home'))
except Identity.DoesNotExist:
    HttpResponseRedirect(reverse('home'))

4 Comments

I'm trying your answer which seems look good ;) I'll come back in order to give you my feeling
why you are making the column for same data in different table. You can use foreign key also.
Because I have to get the real ID number and not a ForeignKey. Or, for the moment, I don't find a good way to do hat I want :/ I have to think one more time on a good process.
is It conpulsory to have same id for same person in both tables (Identity and BirthCertificate)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.