def test_markovify_when_scrape_configured(): app = create_app(TextScrapeTestConfig) with app.app_context(): sample_text = placeholder_or_random_sample_text() # Give markovify a few more tries, if it fails to generate # text the first time. i = 0 while i < 3 and (not sample_text): sample_text = placeholder_or_random_sample_text() i += 1 assert sample_text is not None assert len(sample_text) assert sample_text != app.config['EDITABLE_PLACEHOLDER_TEXT'] assert ( ('and' in sample_text) or ('the' in sample_text) or ('that' in sample_text) or ('you' in sample_text) or ('man' in sample_text) or ('was' in sample_text) or ('with' in sample_text) or ('said' in sample_text) or ('him' in sample_text) or ('were' in sample_text))
def get_default_gallery_items(): """Gets the default gallery items.""" default_gallery_items = GalleryItem.default_content() if app.config.get('USE_SESSIONSTORE_NOT_DB'): session['gallery_item'] = [''] for o in default_gallery_items: # If this model isn't currently saved to session storage, # set its image and text content now (could be random # samples) and save. session['gallery_item'].append({ 'title': o.title, 'image': placeholder_or_random_sample_image(), 'content': placeholder_or_random_sample_text(), 'date_taken': o.date_taken}) else: curr_weight = 0 for o in default_gallery_items: # If this model isn't currently saved to the DB, # set its image and text content now (could be random # samples) and save. o.image = placeholder_or_random_sample_image() o.content = placeholder_or_random_sample_text() o.weight = curr_weight try: o.save() curr_weight += 1 except IntegrityError as e: db.session.rollback() raise e return default_gallery_items
def add_func(model_name, is_autosave=False): try: v = app.config['EDITABLE_MODELS'][model_name] except KeyError: abort(404) if not (('is_createable' in v) and v['is_createable']): abort(404) try: model_classpath = v['classpath'] except KeyError: raise ValueError( ('No class path defined in app config\'s ' 'EDITABLE_MODELS for model name "{0}"').format(model_name)) try: title_field_name = v['title_field'] except KeyError: raise ValueError( ('No title field defined in app config\'s ' 'EDITABLE_MODELS for model name "{0}"').format(model_name)) try: weight_field_name = v['weight_field'] except KeyError: weight_field_name = None model_class = get_model_class(model_classpath, model_name) form = Form() if form.validate_on_submit(): model_name_friendly = model_name.replace('_', ' ').title() model = model_class.new_item() if ('image_fields' in v) and v['image_fields']: for k in v['image_fields']: setattr(model, k, placeholder_or_random_sample_image()) if ('long_text_fields' in v) and v['long_text_fields']: for k in v['long_text_fields']: setattr(model, k, placeholder_or_random_sample_text()) if (((not app.config.get('USE_SESSIONSTORE_NOT_DB')) and weight_field_name)): max_weight = model_class.max_weight() setattr(model, weight_field_name, (max_weight is not None and (max_weight + 1) or 0)) try: if app.config.get('USE_SESSIONSTORE_NOT_DB'): if not session.get(model_name, None): session[model_name] = [] fields_to_save = [] for k in ('text_fields', 'long_text_fields', 'image_fields'): if (k in v) and v[k]: fields_to_save.extend(v[k]) values_to_save = {} for k in fields_to_save: values_to_save[k] = getattr(model, k) if ('date_fields' in v) and v['date_fields']: for k in v['date_fields']: val = getattr(model, k, '') if val: val = val.strftime('%Y-%m-%d') values_to_save[k] = val if ('time_fields' in v) and v['time_fields']: for k in v['time_fields']: val = getattr(model, k, '') if val: val = val.strftime('%H:%M:%S') values_to_save[k] = val session[model_name].append(values_to_save) else: model.save() app.logger.info('{0} added: {1}; user: {2}'.format( model_name_friendly, model, current_user)) if is_autosave: return Response('OK') else: flash( '"{0}" has been added.'.format( getattr(model, title_field_name)), 'success') except IntegrityError as e: db.session.rollback() if is_autosave: return Response('ERROR') else: msg = (('violates unique constraint' in e.message) and (('Error: a {0} with title "{1}" ' 'already exists.').format( model_name_friendly, getattr(model, title_field_name))) or "Error adding {0}.".format( getattr(model, title_field_name))) flash(msg, 'danger') else: if is_autosave: return Response('ERROR') else: flash_errors(form) return redirect(url_for("public.home"))
def test_placeholder_when_no_scrape_configured(app): sample_text = placeholder_or_random_sample_text() assert sample_text == app.config['EDITABLE_PLACEHOLDER_TEXT']
def add_func(model_name, is_autosave=False): try: v = app.config['EDITABLE_MODELS'][model_name] except KeyError: abort(404) if not(('is_createable' in v) and v['is_createable']): abort(404) try: model_classpath = v['classpath'] except KeyError: raise ValueError(( 'No class path defined in app config\'s ' 'EDITABLE_MODELS for model name "{0}"').format( model_name)) try: title_field_name = v['title_field'] except KeyError: raise ValueError(( 'No title field defined in app config\'s ' 'EDITABLE_MODELS for model name "{0}"').format( model_name)) try: weight_field_name = v['weight_field'] except KeyError: weight_field_name = None model_class = get_model_class(model_classpath, model_name) form = Form() if form.validate_on_submit(): model_name_friendly = model_name.replace('_', ' ').title() model = model_class.new_item() if ('image_fields' in v) and v['image_fields']: for k in v['image_fields']: setattr(model, k, placeholder_or_random_sample_image()) if ('long_text_fields' in v) and v['long_text_fields']: for k in v['long_text_fields']: setattr(model, k, placeholder_or_random_sample_text()) if ( ( (not app.config.get('USE_SESSIONSTORE_NOT_DB')) and weight_field_name)): max_weight = model_class.max_weight() setattr( model, weight_field_name, (max_weight is not None and (max_weight+1) or 0)) try: if app.config.get('USE_SESSIONSTORE_NOT_DB'): if not session.get(model_name, None): session[model_name] = [] fields_to_save = [] for k in ('text_fields', 'long_text_fields', 'image_fields'): if (k in v) and v[k]: fields_to_save.extend(v[k]) values_to_save = {} for k in fields_to_save: values_to_save[k] = getattr(model, k) if ('date_fields' in v) and v['date_fields']: for k in v['date_fields']: val = getattr(model, k, '') if val: val = val.strftime('%Y-%m-%d') values_to_save[k] = val if ('time_fields' in v) and v['time_fields']: for k in v['time_fields']: val = getattr(model, k, '') if val: val = val.strftime('%H:%M:%S') values_to_save[k] = val session[model_name].append(values_to_save) else: model.save() app.logger.info('{0} added: {1}; user: {2}'.format( model_name_friendly, model, current_user)) if is_autosave: return Response('OK') else: flash('"{0}" has been added.'.format( getattr(model, title_field_name)), 'success') except IntegrityError as e: db.session.rollback() if is_autosave: return Response('ERROR') else: msg = ( ('violates unique constraint' in e.message) and (( 'Error: a {0} with title "{1}" ' 'already exists.').format( model_name_friendly, getattr(model, title_field_name))) or "Error adding {0}.".format( getattr(model, title_field_name))) flash(msg, 'danger') else: if is_autosave: return Response('ERROR') else: flash_errors(form) return redirect(url_for("public.home"))