def test_partial_update(self): """ Simiulate a partial update process. """ old_recipe = copy.copy(good_recipe) sc = SousChef(**sous_chef) db_session.add(sc) db_session.commit() old_recipe = recipe_schema.validate(old_recipe, sc.to_dict()) old_id = old_recipe['options']['set_content_items'][0]['id'] old_recipe['slug'] += "-{}".format(gen_short_uuid()) r = Recipe(sc, **old_recipe) db_session.add(r) db_session.commit() new_recipe = { 'owner_screen_name': 'johnoliver', 'last_job': { 'foo': 'bar' }, 'status': 'stable', "set_content_items": [{ 'id': 2, 'title': 'foobar' }] } new_recipe = recipe_schema.update(r, new_recipe, sc.to_dict()) assert (new_recipe['options']['owner_screen_name'] == 'johnoliver') assert (new_recipe['last_job']['foo'] == 'bar') assert (new_recipe['status'] == 'stable') assert (new_recipe['options']['set_content_items'][0]['id'] != old_id) db_session.delete(r) db_session.delete(sc) db_session.commit()
def init(): # create the database db.configure_mappers() db.create_all() # create the super user u = User(name=settings.SUPER_USER, email=settings.SUPER_USER_EMAIL, password=settings.SUPER_USER_PASSWORD, admin=True, super_user=True) # optionally add super user apikey if getattr(settings, 'SUPER_USER_APIKEY', None): u.apikey = settings.SUPER_USER_APIKEY db_session.add(u) # load sql extensions + functions for sql in load_sql(): db_session.execute(sql) # load built-in sous-chefs for sc in load_sous_chefs(): sc = sous_chef_schema.validate(sc) s = SousChef(**sc) db_session.add(s) # commit db_session.commit()
def gen_built_in_recipes(org): recipes = [] metrics = [] u = choice(org.users) # add default recipes for recipe in load_default_recipes(): sous_chef_slug = recipe.pop('sous_chef') if not sous_chef_slug: raise RecipeSchemaError( 'Default recipe "{}" is missing a "sous_chef" slug.'.format( recipe.get('name', ''))) sc = SousChef.query\ .filter_by(slug=sous_chef_slug)\ .first() if not sc: raise RecipeSchemaError( '"{}" is not a valid SousChef slug or the SousChef does not yet exist.' .format(sous_chef_slug)) recipe = recipe_schema.validate(recipe, sc.to_dict()) recipe['user_id'] = u.id recipe['org_id'] = org.id r = Recipe(sc, **recipe) db_session.add(r) db_session.commit() recipes.append(r) # if the recipe creates metrics add them in here. if 'metrics' in sc.creates: for name, params in sc.metrics.items(): m = Metric(name=name, recipe_id=r.id, org_id=org.id, **params) metrics.append(m) db_session.add(m) db_session.commit() return recipes, metrics
def gen_content_item(org, recipes, subject_tags, authors): r = choice(recipes) st = choice(subject_tags) c = choice(authors) provenance = choice(CONTENT_ITEM_PROVENANCES) t = ContentItem(org_id=org.id, recipe_id=r.id, url=random_url(), type=choice(CONTENT_ITEM_TYPES), site_name=choice(["ProPalpatine", "Tatooine Times"]), favicon="http://propublica.org/favicon.ico", provenance=provenance, created=random_date(10, 100), updated=random_date(1, 9), domain='example.com', title=random_text(20), description=random_text(100), body=random_text(500), img_url=IMG_URL, thumbnail=thumbnail, meta=random_meta()) if provenance == 'manual': t.recipe_id = None t.tags.append(st) t.authors.append(c) db_session.add(t) db_session.commit() return t
def test_partial_update(self): """ Simiulate a partial update process. """ old_recipe = copy.copy(good_recipe) sc = SousChef(**sous_chef) db_session.add(sc) db_session.commit() old_recipe = recipe_schema.validate(old_recipe, sc.to_dict()) old_id = old_recipe['options']['set_content_items'][0]['id'] old_recipe['slug'] += "-{}".format(gen_short_uuid()) r = Recipe(sc, **old_recipe) db_session.add(r) db_session.commit() new_recipe = { 'owner_screen_name': 'johnoliver', 'last_job': {'foo': 'bar'}, 'status': 'stable', "set_content_items": [{'id': 2, 'title': 'foobar'}] } new_recipe = recipe_schema.update( r, new_recipe, sc.to_dict()) assert(new_recipe['options']['owner_screen_name'] == 'johnoliver') assert(new_recipe['last_job']['foo'] == 'bar') assert(new_recipe['status'] == 'stable') assert(new_recipe['options']['set_content_items'][0]['id'] != old_id) db_session.delete(r) db_session.delete(sc) db_session.commit()
def gen_org(users): o = Org(name=fake.company(), timezone='America/New_York', domains=['example.com', 'blog.example.com']) o.users.extend(users) db_session.add(o) db_session.commit() return o
def gen_user(): u = User(name=fake.name(), email=random_email(), admin=True, password=random_text(10), created=random_date(1, 10)) db_session.add(u) db_session.commit() return u
def gen_authors(org): authors = [] for name in AUTHORS: c = Author(org_id=org.id, name=name, created=random_date(100, 200), meta=random_meta()) db_session.add(c) db_session.commit() authors.append(c) return authors
def test_partial_update(self): """ Simiulate a partial update process. """ sc = { "name": "Twitter List", "slug": "twitter-list", "description": "Extracts events from a twitter list.", "runs": "newslynx.sc.events.twitter.List", "creates": "events", "options": { "owner_screen_name": { "input_type": "text", "value_types": ["string"], "accepts_list": True, "required": True, "help": { "placeholder": "cspan" }, }, "min_followers": { "input_type": "number", "value_types": ["numeric"], "accepts_list": False, "required": False, "default": 0, "help": { "placeholder": 1000 } } } } sc = sous_chef_schema.validate(sc) sc = SousChef(**sc) db_session.add(sc) db_session.commit() new_sc = { 'name': 'Twitter List to Event', 'options': { 'owner_screen_name': { 'accepts_list': False } } } new_sc = sous_chef_schema.update(sc, new_sc) assert (new_sc['name'] == 'Twitter List to Event') assert (new_sc['options']['owner_screen_name']['accepts_list'] is False) db_session.delete(sc) db_session.commit()
def gen_subject_tags(org, n_subject_tags): subject_tags = [] for tag in load_default_tags(): if tag['type'] == 'subject': tag['org_id'] = org.id t = db_session.query(Tag)\ .filter_by(org_id=org.id, name=tag['name'])\ .first() if not t: t = Tag(**tag) db_session.add(t) db_session.commit() subject_tags.append(t) return subject_tags
def test_partial_update(self): """ Simiulate a partial update process. """ sc = { "name": "Twitter List", "slug": "twitter-list", "description": "Extracts events from a twitter list.", "runs": "newslynx.sc.events.twitter.List", "creates": "events", "options": { "owner_screen_name": { "input_type": "text", "value_types": ["string"], "accepts_list": True, "required": True, "help": { "placeholder": "cspan" }, }, "min_followers": { "input_type": "number", "value_types": ["numeric"], "accepts_list": False, "required": False, "default": 0, "help": { "placeholder": 1000 } } } } sc = sous_chef_schema.validate(sc) sc = SousChef(**sc) db_session.add(sc) db_session.commit() new_sc = { 'name': 'Twitter List to Event', 'options': { 'owner_screen_name': { 'accepts_list': False } } } new_sc = sous_chef_schema.update(sc, new_sc) assert(new_sc['name'] == 'Twitter List to Event') assert(new_sc['options']['owner_screen_name']['accepts_list'] is False) db_session.delete(sc) db_session.commit()
def gen_events(org, recipes, impact_tags, content_items, n_events): events = [] for i in xrange(n_events): status = choice(EVENT_STATUSES) provenance = choice(EVENT_PROVENANCES) r = choice(recipes) authors = random_authors(4) if provenance == 'manual': prefix = 'manual' else: prefix = r.slug e = Event(org_id=org.id, recipe_id=r.id, source_id="{}:{}".format(prefix, str(uuid.uuid1())), title=random_text(20), description=random_text(100), url=random_url(), img_url=IMG_URL, thumbnail=thumbnail, body=random_text(500), created=random_date(10, 100), updated=random_date(1, 9), authors=authors, status=status, provenance=provenance, meta=random_meta()) t = choice(impact_tags) e.tags.append(t) c = choice(content_items) e.content_items.append(c) # if t.id not in c.tag_ids: # c.tags.append(t) # db_session.add(c) if provenance == 'manual': e.recipe_id = None db_session.add(e) events.append(e) db_session.commit() return e