Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Everyday Refactoring of a Django Project

Ana Yankova
September 04, 2015

Everyday Refactoring of a Django Project

Ana Yankova

September 04, 2015
Tweet

More Decks by Ana Yankova

Other Decks in Programming

Transcript

  1. EVERYDAY REFACTORING
    OF A DJANGO PROJECT

    View Slide

  2. We all want to build
    new features

    View Slide

  3. View Slide

  4. The fast pace is fun
    but…

    View Slide

  5. Haste is waste

    View Slide

  6. What
    about
    technical
    debt?

    View Slide

  7. If it ain't broke,
    don't fix it.
    Rewrite the
    whole codebase!

    View Slide

  8. Test it!

    View Slide

  9. The later
    the change
    the higher
    the costs

    View Slide

  10. Everyday refactoring

    View Slide

  11. Usable
    TRUE
    Transparent
    Reasonable
    Exemplary

    View Slide

  12. Exemplary

    View Slide

  13. !

    View Slide

  14. class BarcodeScan(models.Model):
    barcode = models.CharField(unique=True, max_length=100)
    user = models.ForeignKey(‘accounts.UserProfile')
    created = models.DateTimeField(auto_now_add=True)

    View Slide

  15. class Food(models.Model):
    name = models.CharField(max_length=100)
    calories = models.Integer(default=0)
    class FoodBarcode(models.Model):
    barcode = models.CharField(unique=True, max_length=100)
    food = models.ForeignKey(Food)
    foodipedia/models.py

    View Slide

  16. "
    Foodipedia
    app
    ! Food Barcode
    model
    # Food
    model
    #
    ! Barcode Scan
    model
    "
    Barcodes
    app
    !

    View Slide

  17. View Slide

  18. 1
    add the new model
    to Foodipedia

    View Slide

  19. 1
    add the new model
    to Foodipedia
    2
    Create the new app but
    leave FoodBarcode in
    Foodipedia

    View Slide

  20. 1
    add the new model
    to Foodipedia
    2
    3
    Create the new app but
    leave FoodBarcode in
    Foodipedia
    Create the new app and
    migrate FoodBarcode
    and its data

    View Slide

  21. 1
    add the new model
    to Foodipedia
    2
    3 4
    Create the new app but
    leave FoodBarcode in
    Foodipedia
    Create the new app and
    refactor but use db_table
    to avoid schema changes
    Create the new app and
    migrate FoodBarcode
    and its data

    View Slide

  22. class FoodBarcode(models.Model):
    barcode = models.CharField(unique=True, max_length=100)
    food = models.ForeignKey('foodipedia.Food')
    class Meta:
    db_table = u'foodipedia_foodbarcode'
    class BarcodeScan(models.Model):
    barcode = models.CharField(unique=True, max_length=100)
    user = models.ForeignKey(‘accounts.UserProfile')
    created = models.DateTimeField(auto_now_add=True)
    barcodes/models.py

    View Slide

  23. class FoodBarcode(models.Model):
    barcode = models.CharField(unique=True, max_length=100)
    food = models.ForeignKey('foodipedia.Food')
    class Meta:
    db_table = u'foodipedia_foodbarcode'
    class BarcodeScan(models.Model):
    barcode = models.CharField(unique=True, max_length=100)
    user = models.ForeignKey(‘accounts.UserProfile')
    created = models.DateTimeField(auto_now_add=True)
    barcodes/models.py

    View Slide

  24. class Migration(DataMigration):
    def forwards(self, orm):
    orm['contenttypes.contenttype'].objects.filter(
    app_label='foodipedia',
    model='foodbarcode',
    ).update(app_label='barcodes')
    barcodes/migrations/0001_initial.py
    ./manage.py datamigration barcodes 0001_initial --frozen
    contenttypes

    View Slide

  25. class Migration(SchemaMigration):
    depends_on = (('barcodes', '0001_initial'),)
    def forwards(self, orm):
    pass
    foodipedia/migrations/0012_move_foodbarcode.py
    ./manage.py schemamigration foodipedia --auto

    View Slide

  26. Geek & Poke
    Font Awesome
    Practical Object-Oriented Design in Ruby by Sandi Metz
    hilite.me
    RESOURCES
    licensed under $
    licensed under SIL OFL
    Stack Overflow: Easiest way to rename a model using Django/South?
    Two Scoops of Django: Best Practices for Django 1.6 by Daniel Greenfeld & Audrey Roy
    %
    %
    %
    %
    %
    %
    %
    %

    View Slide

  27. [email protected]
    anah
    @anhristova
    &
    '
    (

    View Slide