def prepare_changes(): path = "./" if os.environ.get("DEPLOYMENT_TARGET", None) in ["production", "staging"]: path = "%s" % env.repo_path now_datetime = datetime.datetime.now(pytz.utc) now = time.mktime(now_datetime.timetuple()) changes = 0 if os.path.exists("%s/data/changes.json" % path): os.system("rm -rf %s/.playgrounds_html/" % path) os.system("rm -rf %s/.playgrounds_gzip/" % path) os.system("cp %s/playgrounds.db data/backups/%s-playgrounds.db" % (path, now)) os.system("cp %s/data/changes.json data/backups/%s-changes.json" % (path, now)) os.system("mv %s/data/changes.json %s/changes-in-progress.json" % (path, path)) # Create our list of changed items and a revision group. changed_playgrounds, revision_group = data.process_changes() # Render updated sitemap. data.render_sitemap() # Render updated index. render() # Send the revision email. data.send_revision_email(revision_group) # Remove files and old state. os.system("rm -f %s/changes-in-progress.json" % path) os.system("rm -rf %s/.playgrounds_html/" % path) os.system("rm -rf %s/.playgrounds_gzip/" % path) # Show changes. changes = len(changed_playgrounds) else: context = {} context["total_revisions"] = 0 context["deletes"] = {} context["deletes"]["playgrounds"] = [] context["deletes"]["total_revisions"] = 0 context["inserts"] = {} context["inserts"]["playgrounds"] = [] context["inserts"]["total_revisions"] = 0 context["updates"] = {} context["updates"]["playgrounds"] = [] context["updates"]["total_revisions"] = 0 with open("%s/templates/_email.html" % path, "rb") as read_template: payload = Template(read_template.read()) payload = payload.render(**context) addresses = app_config.ADMIN_EMAILS data.send_email(addresses, payload) print "%s changes | %s" % (changes, now_datetime.isoformat())
def test_process_inserts(self): models.delete_tables() models.create_tables() new_playgrounds, revision_group = data.process_changes('tests/data/test_inserts.json') self.assertEqual(len(new_playgrounds), 1) playground = Playground.select().where(Playground.id == new_playgrounds[0].id)[0] self.assertEqual(playground.name, 'NEW NAME') revisions = Revision.select().where(Revision.revision_group == revision_group) self.assertEqual(revisions.count(), 1) revision = revisions[0] self.assertEqual(revision.playground.id, playground.id) log = revision.get_log() self.assertEqual(len(log), 1) self.assertEqual(log[0]['field'], 'name') self.assertEqual(log[0]['from'], '') self.assertEqual(log[0]['to'], 'NEW NAME') headers = revision.get_headers() self.assertEqual(headers['content_length'], '18') self.assertEqual(headers['host'], 'localhost') cookies = revision.get_cookies()
def test_process_deletes(self): """ Test the process_deletes bits. """ # Test process_changes() utils.load_test_playgrounds() deleted_playgrounds, revision_group = data.process_changes('tests/data/test_deletes.json') self.assertEqual(len(deleted_playgrounds), 0) # Test process_delete() itself. with open('tests/data/test_deletes.json', 'r') as jsonfile: test_deletes = json.loads(jsonfile.read()) playground, revisions = data.process_delete(test_deletes[0]) expected_id = 1 expected_revisions = [ {"field": "active", "from": True, "to": False}, {"field": "reason", "from": "", "to": "This playground doesn't appear to be accessible in any way whatsoever."} ] self.assertEqual(playground.id, expected_id) self.assertEqual(revisions, expected_revisions)
def test_process_deletes(self): """ Test the process_deletes bits. """ # Test process_changes() utils.load_test_playgrounds() deleted_playgrounds, revision_group = data.process_changes( 'tests/data/test_deletes.json') self.assertEqual(len(deleted_playgrounds), 0) # Test process_delete() itself. with open('tests/data/test_deletes.json', 'r') as jsonfile: test_deletes = json.loads(jsonfile.read()) playground, revisions = data.process_delete(test_deletes[0]) expected_id = 1 expected_revisions = [{ "field": "active", "from": True, "to": False }, { "field": "reason", "from": "", "to": "This playground doesn't appear to be accessible in any way whatsoever." }] self.assertEqual(playground.id, expected_id) self.assertEqual(revisions, expected_revisions)
def test_process_updates_simple(self): utils.load_test_playgrounds() updated_playgrounds, revision_group = data.process_changes('tests/data/test_updates_simple.json') self.assertEqual(len(updated_playgrounds), 1) playground = Playground.select().where(Playground.id == updated_playgrounds[0].id)[0] self.assertEqual(playground.id, 1) self.assertEqual(playground.name, 'NEW NAME') revisions = Revision.select().where(Revision.revision_group == revision_group) self.assertEqual(revisions.count(), 1) revision = revisions[0] self.assertEqual(revision.playground.id, playground.id) log = revision.get_log() self.assertEqual(len(log), 2) self.assertEqual(log[0]['field'], 'name') self.assertEqual(log[0]['from'], 'Strong Reach Playground') self.assertEqual(log[0]['to'], 'NEW NAME') self.assertEqual(log[1]['field'], 'smooth-surface-throughout') self.assertEqual(log[1]['from'], 1) self.assertEqual(log[1]['to'], 0) headers = revision.get_headers() self.assertEqual(headers['content_length'], '18') self.assertEqual(headers['host'], 'localhost') cookies = revision.get_cookies() self.assertEqual(len(cookies), 0)
def test_process_inserts(self): models.delete_tables() models.create_tables() new_playgrounds, revision_group = data.process_changes( 'tests/data/test_inserts.json') self.assertEqual(len(new_playgrounds), 1) playground = Playground.select().where( Playground.id == new_playgrounds[0].id)[0] self.assertEqual(playground.name, 'NEW NAME') revisions = Revision.select().where( Revision.revision_group == revision_group) self.assertEqual(revisions.count(), 1) revision = revisions[0] self.assertEqual(revision.playground.id, playground.id) log = revision.get_log() self.assertEqual(len(log), 1) self.assertEqual(log[0]['field'], 'name') self.assertEqual(log[0]['from'], '') self.assertEqual(log[0]['to'], 'NEW NAME') headers = revision.get_headers() self.assertEqual(headers['content_length'], '18') self.assertEqual(headers['host'], 'localhost') cookies = revision.get_cookies()
def prepare_changes(): path = './' if os.environ.get('DEPLOYMENT_TARGET', None) in ['production', 'staging']: path = '%s' % env.repo_path now_datetime = datetime.datetime.now(pytz.utc) now = time.mktime(now_datetime.timetuple()) changes = 0 if os.path.exists('%s/data/changes.json' % path): os.system('rm -rf %s/.playgrounds_html/' % path) os.system('rm -rf %s/.playgrounds_gzip/' % path) os.system('cp %s/playgrounds.db data/backups/%s-playgrounds.db' % (path, now)) os.system('cp %s/data/changes.json data/backups/%s-changes.json' % (path, now)) os.system('mv %s/data/changes.json %s/changes-in-progress.json' % (path, path)) # Create our list of changed items and a revision group. changed_playgrounds, revision_group = data.process_changes() # Render updated sitemap. data.render_sitemap() # Render updated index. render() # Send the revision email. data.send_revision_email(revision_group) # Remove files and old state. os.system('rm -f %s/changes-in-progress.json' % path) os.system('rm -rf %s/.playgrounds_html/' % path) os.system('rm -rf %s/.playgrounds_gzip/' % path) # Show changes. changes = len(changed_playgrounds) else: context = {} context['total_revisions'] = 0 context['deletes'] = {} context['deletes']['playgrounds'] = [] context['deletes']['total_revisions'] = 0 context['inserts'] = {} context['inserts']['playgrounds'] = [] context['inserts']['total_revisions'] = 0 context['updates'] = {} context['updates']['playgrounds'] = [] context['updates']['total_revisions'] = 0 with open('%s/templates/_email.html' % path, 'rb') as read_template: payload = Template(read_template.read()) payload = payload.render(**context) addresses = app_config.ADMIN_EMAILS data.send_email(addresses, payload) print '%s changes | %s' % (changes, now_datetime.isoformat())
def test_process_update_only_one(self): utils.load_test_playgrounds() updated_playgrounds, revision_group = data.process_changes('tests/data/test_updates_simple.json') self.assertEqual(len(updated_playgrounds), 1) playground = Playground.select().where(Playground.id != updated_playgrounds[0].id)[0] self.assertNotEqual(playground.id, 1) self.assertNotEqual(playground.name, 'NEW NAME')
def test_process_update_only_one(self): utils.load_test_playgrounds() updated_playgrounds, revision_group = data.process_changes( 'tests/data/test_updates_simple.json') self.assertEqual(len(updated_playgrounds), 1) playground = Playground.select().where( Playground.id != updated_playgrounds[0].id)[0] self.assertNotEqual(playground.id, 1) self.assertNotEqual(playground.name, 'NEW NAME')
def test_process_updates_features(self): utils.load_test_playgrounds() PlaygroundFeature.create( slug='transfer-stations-to-play-components', playground=Playground.get(id=1) ) # JSON adds one feature and removes the one just created updated_playgrounds, revision_group = data.process_changes('tests/data/test_updates_features.json') features = PlaygroundFeature.select().where(PlaygroundFeature.playground == 1) self.assertEqual(features.count(), 1) feature = features[0] self.assertEqual(feature.slug, 'smooth-surface-throughout')
def test_process_updates_features(self): utils.load_test_playgrounds() PlaygroundFeature.create(slug='transfer-stations-to-play-components', playground=Playground.get(id=1)) # JSON adds one feature and removes the one just created updated_playgrounds, revision_group = data.process_changes( 'tests/data/test_updates_features.json') features = PlaygroundFeature.select().where( PlaygroundFeature.playground == 1) self.assertEqual(features.count(), 1) feature = features[0] self.assertEqual(feature.slug, 'smooth-surface-throughout')
def test_process_updates_simple(self): utils.load_test_playgrounds() updated_playgrounds, revision_group = data.process_changes( 'tests/data/test_updates_simple.json') self.assertEqual(len(updated_playgrounds), 1) playground = Playground.select().where( Playground.id == updated_playgrounds[0].id)[0] self.assertEqual(playground.id, 1) self.assertEqual(playground.name, 'NEW NAME') revisions = Revision.select().where( Revision.revision_group == revision_group) self.assertEqual(revisions.count(), 1) revision = revisions[0] self.assertEqual(revision.playground.id, playground.id) log = revision.get_log() self.assertEqual(len(log), 2) self.assertEqual(log[0]['field'], 'name') self.assertEqual(log[0]['from'], 'Strong Reach Playground') self.assertEqual(log[0]['to'], 'NEW NAME') self.assertEqual(log[1]['field'], 'smooth-surface-throughout') self.assertEqual(log[1]['from'], 1) self.assertEqual(log[1]['to'], 0) headers = revision.get_headers() self.assertEqual(headers['content_length'], '18') self.assertEqual(headers['host'], 'localhost') cookies = revision.get_cookies() self.assertEqual(len(cookies), 0)