Пример #1
0
def add_gallery():
    form = GalleryForm()
    if form.validate_on_submit():
        if request.files.getlist('photos'):
            ps = []
            for file in request.files.getlist('photos'):
                if file and allowed_file(file.filename):
                    filename = secure_filename(file.filename)
                    extension = filename.rsplit('.', 1)[1].lower()
                    photo = Photo(extension=extension)
                    photo.save()
                    ps.append(photo)
                    unified_filename = str(photo.id) + '.' + extension
                    f = file
                    try:
                        picture_handler(f, unified_filename)
                    except:
                        print("picture file may not be updated")
                    flash('New picture uploaded!')
            gallery = Gallery(name=form.name.data,
                              description=form.description.data,
                              photos=ps)
            gallery.save()
            return redirect(url_for('main.galleries'))
    return render_template('admin/add_gallery.html',
                           title='Create Gallery',
                           form=form)
Пример #2
0
def upload():
    file = request.files.get('file')
    picture = Gallery()
    picture.set_picture(file)
    db.session.add(picture)
    db.session.commit()
    response = jsonify(picture.to_dict())
    response.status_code = 201
    return response
Пример #3
0
def edit(id):
    if request.method == 'POST':
        project = Project.query.filter_by(id=id).first()

        project.name = request.form['name']
        project.type_id = request.form['type_id']
        project.area_id = request.form['area_id']
        project.image = request.form['image']
        project.scale = request.form['scale']
        project.unit = request.form['unit']
        project.service = request.form['service']
        project.description = request.form['description']

        db.session.commit()

        p = project.to_dict()

        imgs = re.findall('(https.*?)\"',
                          str(request.form.getlist('imgs')))  # 获取前端上传的多张图片
        # 循环往 gallery 表中插值
        for img in imgs:
            gallery = Gallery(imgs=img, project_id=p['id'])
            db.session.add(gallery)
            db.session.commit()

        flash('编辑成功')
        return redirect('/admin/project')

    project = Project.query.filter(Project.id == id).first()
    galleries = project.gallery
    return render_template('admin/project/edit.html',
                           project=project,
                           galleries=galleries)
Пример #4
0
def galleries():
    gs = Gallery.objects()
    page = request.args.get('page', 1, type=int)
    paginated_galleries = gs.paginate(
        page=page, per_page=current_app.config['PIC_PER_PAGE'])
    current_page = paginated_galleries.page
    total_page = paginated_galleries.pages

    page_start = 1
    page_end = 5
    if total_page < 5:
        page_end = total_page
    else:
        page_start = current_page - 2
        page_end = current_page + 2
        if page_start < 1:
            page_start = 1
            page_end = 5
        if page_end > total_page:
            page_start = total_page - 4
            page_end = total_page

    url_list = []
    for p in range(1, total_page + 1):  # so page number start from 1
        url = url_for('main.galleries', page=p)
        url_list.append(url)
    return render_template('main/galleries.html',
                           galleries=paginated_galleries.items,
                           current_page=current_page,
                           total_page=total_page,
                           page_start=page_start,
                           page_end=page_end,
                           url_list=url_list)
Пример #5
0
def populate_db():
    # Provider
    password = encrypt_password('password')
    p = Provider(
        user=ud.create_user(email="*****@*****.**", password=password))
    p.business_name = 'Sparky\'s'
    p.user.confirmed_at = datetime.date.today()

    p.address.street_1 = '403 South Second'
    p.address.city = 'Clarksville'
    p.address.state = 'TN'
    p.address.zip_code = 37040

    p.menus[0].menu_items.append(
        MenuItem(name="Haircut", price="25", description="We cut your hair"))
    p.menus[0].menu_items.append(
        MenuItem(name="Line out", price="10", description="A quick line out"))
    p.menus[0].menu_items.append(
        MenuItem(name="Shave", price="14", description="Shave your face"))

    p.menus[1].menu_items.append(
        MenuItem(name="Color",
                 price="40",
                 description="A lovely color for your hair"))
    p.menus[1].menu_items.append(
        MenuItem(name="Blowout",
                 price="150",
                 description="Blast it to the moon"))
    p.menus[1].menu_items.append(
        MenuItem(name="Trim",
                 price="25",
                 description="Keep things neat and tidy"))
    p.menus[1].menu_items.append(
        MenuItem(name="Cut and Style",
                 price="60",
                 description="The full package"))

    p.gallery = Gallery()

    for num in range(1, 40):
        p.gallery.photos.append(
            Photo(url="http://placehold.it/600x900&text={}".format(num),
                  sm_thumb="http://placehold.it/450x450&text={}".format(num),
                  lg_thumb="http://placehold.it/550x550&text={}".format(num)))
    p.save()
    p.index()

    # Consumer
    c = Consumer(user=ud.create_user(email='*****@*****.**',
                                     password=password,
                                     first_name='Bob',
                                     last_name='Johnson'))
    c.user.confirmed_at = datetime.date.today()
    c.consumer_url = 'bob.johnson'
    c.favorites.append(Provider.get(10))
    c.favorites.append(Provider.get(15))
    c.favorites.append(Provider.get(94))
    db.session.add(c)
    db.session.commit()
Пример #6
0
def create():
    if request.method == 'POST':
        name = request.form['name']
        type_id = request.form['type_id']
        area_id = request.form['area_id']
        image = request.form['image']
        scale = request.form['scale']
        unit = request.form['unit']
        service = request.form['service']
        description = request.form['description']
        imgs = re.findall('(https.*?)\"',
                          str(request.form.getlist('imgs')))  # 获取前端上传的多张图片
        if not name:
            flash('请输入项目名称')
            return redirect(request.referrer)

        # 往 project 表中插值
        project = Project(name=name,
                          image=image,
                          type_id=type_id,
                          area_id=area_id,
                          scale=scale,
                          unit=unit,
                          service=service,
                          description=description)
        db.session.add(project)
        db.session.commit()
        p = project.to_dict()

        # 循环往 gallery 表中插值
        for img in imgs:
            gallery = Gallery(imgs=img, project_id=p['id'])
            db.session.add(gallery)
            db.session.commit()

        flash('新增成功')
        return redirect('/admin/project')

    return render_template('admin/project/create.html')
Пример #7
0
def get_gallery():
    page = request.args.get('page', 1, type=int)
    per_page = min(request.args.get('per_page', 4, type=int), 100)
    data = Gallery.to_collection_dict(
        Gallery.query.filter_by(deleted=False).order_by(
            Gallery.timestamp.desc()), page, per_page, 'api.get_gallery')
    if request.args.get('html'):
        divs = []
        for item in data['items']:
            divs.append('''
                <div class="col-md-3 col-sm-6 col-12 mb-4">
                    <a class="" href="{image}" data-lightbox="indoor" data-title="{title}">
                        <div class="view overlay zoom w-100 h-100">
                            <img class="img-fluid img-thumbnail z-depth-1" src="{image}" alt="{id}"/>
                            <div class="mask flex-center rgba-red-strong waves-effect waves-light">
                                <p class="white-text">Click for fullscreen</p>
                            </div>
                        </div>
                    </a>
                </div>
            '''.format(image=url_for('content.main', image_url=item['url']),
                       title=item['title'],
                       id=item['id']))
        payload = {
            "_links": {
                "next": data['_links']['next'],
                "prev": data['_links']['prev'],
                "self": data['_links']['self']
            },
            "_meta": {
                "page": data['_meta']['page'],
                "per_page": data['_meta']['per_page'],
                "total_items": data['_meta']['total_items'],
                "total_pages": data['_meta']['total_pages']
            },
            'divs': divs
        }
        return jsonify(payload)
    return jsonify(data)
Пример #8
0
def create_gallery(token_payload):

    generate_token_user(token_payload)
    logged_user = User.query.filter_by(auth_id=token_payload['sub']).first()

    if logged_user is None:
        abort(403, 'Request Blocked. User Token not Valid.')

    galleryname = request.json['galleryname']

    if not galleryname:
        abort(400, 'Gallery Name field is empty.')

    # The user cannot have two galleries with the same name.
    if Gallery.query.filter_by(galleryname=galleryname,
                               author=logged_user).first():
        return jsonify({'message': 'Gallery name already exists.'}), 200

    new_gallery = Gallery(galleryname=galleryname, author=logged_user)
    db.session.add(new_gallery)
    db.session.commit()

    return jsonify({'message': 'Gallery Created.'}), 201