def test_publisher_edit(self): """ Test editing a publisher, the initial GET should have a form with values and then the post should update the Publisher rather than creating a new one. """ publisher_name = 'Test Edit Publisher' publisher_website = 'http://www.example.com/edit-publisher/' publisher_email = '*****@*****.**' publisher = Publisher(name=publisher_name, website=publisher_website, email=publisher_email) publisher.save() self.assertEqual(Publisher.objects.all().count(), 1) c = Client() response = c.get('/publishers/{}/'.format(publisher.pk)) self.assertIn(b'value="Test Edit Publisher"', response.content) self.assertIn(b'value="http://www.example.com/edit-publisher/"', response.content) self.assertIn(b'value="*****@*****.**"', response.content) self.assertIn( b'<button type="submit" class="btn btn-primary">\n Save\n </button>', response.content) response = c.post( '/publishers/{}/'.format(publisher.pk), { 'name': 'Updated Name', 'website': 'https://www.example.com/updated/', 'email': '*****@*****.**' }) self.assertEqual(response.status_code, 302) self.assertEqual(Publisher.objects.all().count(), 1) publisher2 = Publisher.objects.first() self.assertEqual(publisher2.pk, publisher.pk) self.assertEqual(publisher2.name, 'Updated Name') self.assertEqual(publisher2.website, 'https://www.example.com/updated/') self.assertEqual(publisher2.email, '*****@*****.**') # the messages will be on the redirected to page response = c.get(response['location']) condensed_content = re.sub( r'\s+', ' ', response.content.decode('utf8').replace('\n', '')) self.assertIn( '<div class="alert alert-success" role="alert"> Publisher "Updated Name" was updated. </div>', condensed_content)
def setUp(self): publisher = Publisher(name='Packt', website='www.packt.com', email='*****@*****.**') self.p = Book(title='Book_title', publication_date='2018-10-31', isbn='24141234324', publisher=publisher)
def test_publisher_edit(self): """ Test editing a publisher, the initial GET should have a form with values and then the post should update the Publisher rather than creating a new one. """ publisher_name = 'Test Edit Publisher' publisher_website = 'http://www.example.com/edit-publisher/' publisher_email = '*****@*****.**' publisher = Publisher(name=publisher_name, website=publisher_website, email=publisher_email) publisher.save() self.assertEqual(Publisher.objects.all().count(), 1) c = Client() response = c.get('/publishers/{}/'.format(publisher.pk)) self.assertIn(b'value="Test Edit Publisher"', response.content) self.assertIn(b'value="http://www.example.com/edit-publisher/"', response.content) self.assertIn(b'value="*****@*****.**"', response.content) response = c.post( '/publishers/{}/'.format(publisher.pk), { 'name': 'Updated Name', 'website': 'https://www.example.com/updated/', 'email': '*****@*****.**' }) self.assertEqual(response.status_code, 302) self.assertEqual(Publisher.objects.all().count(), 1) publisher2 = Publisher.objects.first() self.assertEqual(publisher2.pk, publisher.pk) self.assertEqual(publisher2.name, 'Updated Name') self.assertEqual(publisher2.website, 'https://www.example.com/updated/') self.assertEqual(publisher2.email, '*****@*****.**')
#!/usr/bin/env python3 from reviews.models import Publisher publisher = Publisher(name='Packt Publishing', website='https://www.packtpub.com', email='*****@*****.**') publisher.save() publisher.email = '*****@*****.**' publisher.save()
def setUp(self): self.p = Publisher(name='Packt', website='www.packt.com', email='*****@*****.**')
def handle(self, *args, **options): m = re.compile('content:(\w+)') header = None models = dict() try: with open(options['csv']) as csvfile: model_data = csv.reader(csvfile) for i, row in enumerate(model_data): if max([len(cell.strip()) for cell in row[1:] + [''] ]) == 0 and m.match(row[0]): model_name = m.match(row[0]).groups()[0] models[model_name] = [] header = None continue if header is None: header = row continue row_dict = self.row_to_dict(row, header) if set(row_dict.values()) == set(['']): continue models[model_name].append(row_dict) except FileNotFoundError as f: raise CommandError('File "%s" does not exist' % options['csv']) for data_dict in models.get('Publisher', []): print('Publisher') p = Publisher(name=data_dict['publisher_name'], website=data_dict['publisher_website'], email=data_dict['publisher_email']) p.save() for data_dict in models.get('Book', []): print('Book') b = Book( title=data_dict['book_title'], publication_date=data_dict['book_publication_date'].replace( '/', '-'), isbn=data_dict['book_isbn'], publisher=Publisher.objects.filter( name=data_dict['book_publisher_name']).first()) b.save() for data_dict in models.get('Contributor', []): print('Contributor') c = Contributor(first_names=data_dict['contributor_first_names'], last_names=data_dict['contributor_last_names'], email=data_dict['contributor_email']) c.save() for data_dict in models.get('BookContributor', []): print('BookContributor') bc = BookContributor( book=Book.objects.filter( title=data_dict['book_contributor_book']).first(), contributor=Contributor.objects.filter( email=data_dict['book_contributor_contributor']).first(), role=data_dict['book_contributor_role']) bc.save() for data_dict in models.get('Review', []): print('Review') review = Review( content=data_dict['review_content'], rating=data_dict['review_rating'], date_created=data_dict['review_date_created'], date_edited=data_dict['review_date_edited'], creator=User.objects.get(email=data_dict['review_creator']), book=Book.objects.filter( title=data_dict['review_book']).first()) review.save()