Example #1
0
def get_gallery_items():
    """Gets all gallery items."""

    if app.config.get('USE_SESSIONSTORE_NOT_DB'):
        if session.get('gallery_item') is None:
            session['gallery_item'] = ['']

        for id, o in enumerate(session.get('gallery_item', [])):
            if o:
                filepath = os.path.join(
                    app.config['MEDIA_FOLDER'],
                    session['gallery_item'][id]['image'])

                # If the placeholder image defined in session storage
                # doesn't exist on the filesystem (e.g. if Heroku has
                # wiped the filesystem due to app restart), set a new
                # image for this model.
                if not os.path.exists(filepath):
                    session['gallery_item'][id]['image'] = (
                        placeholder_or_random_sample_image())

        gallery_items = []
        for id, o in enumerate(session['gallery_item']):
            if o:
                model = GalleryItem(**o)
                model.id = id
                model.weight = id-1
                gallery_items.append(model)
    else:
        gallery_items = (
            GalleryItem.query
                       .filter_by(active=True)
                       .order_by(GalleryItem.weight))

    return gallery_items
Example #2
0
def get_gallery_items():
    """Gets all gallery items."""

    if app.config.get('USE_SESSIONSTORE_NOT_DB'):
        if session.get('gallery_item') is None:
            session['gallery_item'] = ['']

        for id, o in enumerate(session.get('gallery_item', [])):
            if o:
                filepath = os.path.join(app.config['MEDIA_FOLDER'],
                                        session['gallery_item'][id]['image'])

                # If the placeholder image defined in session storage
                # doesn't exist on the filesystem (e.g. if Heroku has
                # wiped the filesystem due to app restart), set a new
                # image for this model.
                if not os.path.exists(filepath):
                    session['gallery_item'][id]['image'] = (
                        placeholder_or_random_sample_image())

        gallery_items = []
        for id, o in enumerate(session['gallery_item']):
            if o:
                model = GalleryItem(**o)
                model.id = id
                model.weight = id - 1
                gallery_items.append(model)
    else:
        gallery_items = (GalleryItem.query.filter_by(active=True).order_by(
            GalleryItem.weight))

    return gallery_items
Example #3
0
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
Example #4
0
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