UNC BACS 350

Web Apps with Python/Django

Logo

Lesson 36 - Data Versioning

LEARN

Office Hours

Today

Views Design Patterns

Catalog of Design Patterns

Data Design Graph

Book Builder Data Design

User Author user* photo* name Book author* title doc_path Chapter book* order title markdown Note author* chapter* title text Image folder image title

Book Data Relationships

If you have more that four data models draw a simple diagram

Use https://www.gliffy.com/

Superhero Data Relationships

Use https://www.gliffy.com/

Data Versioning Design Pattern

Why - Data Versioning

When - Data Versioning

How - Data Versioning

What - Data Versioning

Export Design Pattern

```python class Chapter(models.Model): book = models.ForeignKey(Book, on_delete=models.CASCADE, editable=False) order = models.IntegerField() title = models.CharField(max_length=200) markdown = models.TextField() html = models.TextField() document = models.CharField(max_length=200)

def export_record(self):
    return [self.order, self.title, self.document]

```

Commit data changes

Object Record Creation

```python class Chapter(models.Model): book = models.ForeignKey(Book, on_delete=models.CASCADE, editable=False) order = models.IntegerField() title = models.CharField(max_length=200) markdown = models.TextField() html = models.TextField() document = models.CharField(max_length=200)

@staticmethod
def create(book, order, title, document):
    c = Chapter.objects.get_or_create(book=book, order=order)[0]
    c.title = title
    c.document = document
    c.save()
    return c

```

Import Design Pattern

```python class Chapter(models.Model): book = models.ForeignKey(Book, on_delete=models.CASCADE, editable=False) order = models.IntegerField() title = models.CharField(max_length=200) markdown = models.TextField() html = models.TextField() document = models.CharField(max_length=200)

@staticmethod
def import_record(book, values):
    Chapter.create(book, values[0], values[1], values[2])

```

Testability

```python class BookDataTest(TestCase):

def test_import_books(self):
    import_all_books()
    # print(Author.objects.all())
    # print(Book.objects.all())
    # print(Chapter.objects.all())
    self.assertEqual(len(Author.objects.all()), 3)
    self.assertEqual(len(Book.objects.all()), 2)
    self.assertEqual(len(Chapter.objects.all()), 70)

```

Create a Django Command

coder/management/commands/initdb.py

```python from django.core.management.base import BaseCommand

class Command(BaseCommand):

def handle(self, *args, **options):
    print("IMPORT BOOKS")
    create_test_user()
    import_all_books()

def create_test_user(): args = dict(username='seaman', email='me@here.com', password='secret') user = get_user_model().objects.filter(username='seaman') if user: user = user[0] else: user = get_user_model().objects.create_user(**args) return user, args

def import_all_books(): author = create_author('Mark Seaman') import_book("The Leverage Principle", author, 'Documents/Leverage', "Software Engineering Skills") description = 'Mark shares his insights and irony about the absurdity of life.' import_book("From the Edge of Reality", author, 'Documents/Poems', "A Seaman's Poems", description) ```

Invoke the command script

bash $ python manage.py initdb

BUILD

Practice