Пример #1
0
    def getPhoto(photo):
        date_diplay = {}
        date_approx = photo.get('date_photo')
        filter_date = photo.get('filter_date')
        if date_approx:
            date_diplay = {'md': date_approx, 'sm': date_approx}
        else:
            date_obj = datetime.strptime(filter_date, '%Y-%m-%d')
            date_diplay = {
                'md': format_datetime(date_obj, 'yyyy (dd MMMM)'),
                'sm': date_obj.strftime('%Y')
            }
        captions = []
        licence_photo = photo.get('dico_licence_photo')
        if licence_photo:
            captions.append(licence_photo.get('name_licence_photo'))
        """ author = photo.get('t_role')
        if author:
            captions.append('%s %s'  % (
                photo.get('t_role').get('prenom_role'),
                photo.get('t_role').get('nom_role')
            )) """
        caption = ' | '.join(captions)

        dl_caption = "%s | %s | réf. : %s | %s" % (
            site.get('name_site'), site.get('ville').get('nom_commune'),
            site.get('ref_site'), date_diplay.get('md'))

        if caption:
            dl_caption = '%s | %s' % (dl_caption, caption)

        if COMPARATOR_VERSION == 1:
            return {
                'id': photo.get('id_photo'),
                'sm': utils.getThumbnail(photo).get('output_url'),
                'md': utils.getMedium(photo).get('output_url'),
                'lg': utils.getLarge(photo, caption).get('output_url'),
                'dl': utils.getDownload(photo, dl_caption).get('output_url'),
                'date': photo.get('filter_date'),
                'date_diplay': date_diplay
            }

        return {
            'id': photo.get('id_photo'),
            'filename': photo.get('path_file_photo'),
            'shot_on': photo.get('filter_date'),
            'date_diplay': date_diplay,
            'caption': caption
        }
Пример #2
0
def home():
    """ sql = text("SELECT value FROM geopaysages.conf WHERE key = 'home_blocks'")
    rows = db.engine.execute(sql).fetchall()
    id_photos = json.loads(rows[0]['value'])
    get_photos = models.TPhoto.query.filter(
        models.TPhoto.id_photo.in_(id_photos))
    dump_pĥotos = photo_schema.dump(get_photos).data

    site_ids = [photo.get('t_site') for photo in dump_pĥotos]
    get_sites = models.TSite.query.filter(models.TSite.id_site.in_(site_ids))
    dump_sites = site_schema.dump(get_sites).data """

    sql = text("SELECT * FROM geopaysages.t_site where publish_site=true ORDER BY RANDOM() LIMIT 6")
    sites_proxy = db.engine.execute(sql).fetchall()
    sites = [dict(row.items()) for row in sites_proxy]
    diff_nb = 6 - len(sites)
    for x in range(0, diff_nb):
        sites.append(sites[x])

    photo_ids = []
    sites_without_photo = []
    code_communes = []
    for site in sites:
        photo_id = site.get('main_photo')
        if photo_id:
            photo_ids.append(site.get('main_photo'))
        else:
            sites_without_photo.append(str(site.get('id_site')))
        code_communes.append(site.get('code_city_site'))

    query_photos = models.TPhoto.query.filter(
        models.TPhoto.id_photo.in_(photo_ids)
    )
    dump_photos = photo_schema.dump(query_photos).data

    if len(sites_without_photo):
        sql_missing_photos_str = "select distinct on (id_site) * from geopaysages.t_photo where id_site IN (" + ",".join(sites_without_photo) + ") order by id_site, filter_date desc"
        sql_missing_photos = text(sql_missing_photos_str)
        missing_photos_result = db.engine.execute(sql_missing_photos).fetchall()
        missing_photos = [dict(row) for row in missing_photos_result]
        for missing_photo in missing_photos:
            missing_photo['t_site'] = missing_photo.get('id_site')
            dump_photos.append(missing_photo)

    query_commune = models.Communes.query.filter(
        models.Communes.code_commune.in_(code_communes)
    )
    dump_communes = communes_schema.dump(query_commune).data

    for site in sites:
        id_site = site.get('id_site')
        photo = next(photo for photo in dump_photos if (photo.get('t_site') == id_site))
        site['photo'] = utils.getMedium(photo).get('output_url')
        site['commune'] = next(commune for commune in dump_communes if (commune.get('code_commune') == site.get('code_city_site')))


    """ def get_photo_block(id_photo):
        try:
            photo = next(photo for photo in dump_pĥotos if photo.get(
                'id_photo') == id_photo)
            photo['url'] = url_for(
                'static', filename=DATA_IMAGES_PATH + photo.get('path_file_photo'))
            site = next(site for site in dump_sites if site.get(
                'id_site') == photo.get('t_site'))
            return {
                'photo': photo,
                'site': site
            }
        except Exception as exception:
            pass

    blocks = [
        get_photo_block(id_photo)
        for id_photo in id_photos
    ] """

    all_sites=site_schema.dump(models.TSite.query.filter_by(publish_site = True)).data
    
    return render_template('home.html', blocks=sites, sites=all_sites)