def edit_collection(collection_id):
    categories = EnumValues.type_filter(const.PHOTO_CATEGORY_KEY).all()
    styles = EnumValues.type_filter(const.PHOTO_STYLE_KEY).all()
    photo_collection = PhotoCollection.query.get(collection_id)
    if photo_collection.photographer != current_user:
        flash("您没有权限编辑本作品集,将返回网站主页")
        return redirect(url_for("index"))
    form = PhotoCollectionForm(categories, styles)
    if request.method == "POST":
        if (
            request.form.get("photo-collection-to-delete") is not None
            and len(request.form.get("photo-collection-to-delete")) != 0
        ):
            id_to_delete = int(request.form.get("photo-collection-to-delete"))
            delete_by_id(PhotoCollection, id_to_delete)
            flash("作品集删除成功")
            return redirect(url_for("my_photos"))
        elif request.form.get("photo-works-to-delete") is not None:
            works_to_delete = request.form.get("photo-works-to-delete").split(",")
            delete_photo_works(works_to_delete)
            files = request.files.getlist("photos[]")
            if not (len(files) == 1 and files[0].filename == u""):
                add_photo_works(files, photo_collection)
        else:
            save_photo_collection(form, photo_collection)
        AppInfo.get_db().session.commit()
    return rt(
        "edit_collection.html", photo_collection=photo_collection, categories=categories, form=form, styles=styles
    )
Exemple #2
0
def delete_by_id(obj_type, id_to_del):
    """
    :type obj_type: db.Model
    :type id_to_del: int
    """
    obj = obj_type.query.get(id_to_del)
    AppInfo.get_db().session.delete(obj)
    AppInfo.get_db().session.commit()
Exemple #3
0
def photograph():
    categories = EnumValues.type_filter(const.PHOTO_CATEGORY_KEY).all()
    styles = EnumValues.type_filter(const.PHOTO_STYLE_KEY).all()
    category, style, include_none_date, include_none_price, min_price, max_price, min_date, max_date = \
        [None, None, None, None, None, None, None, None]
    if request.method == 'POST':
        category_id, include_none_date, include_none_price, max_date, max_price, min_date, min_price, style_id = extra_fields_from_form()
        collections, category, style = query_for_photo_collection(category_id, True, True, None, None, None, None, style_id)

        query = AppInfo.get_db().session.query(User).join(PhotoCollection, User.id == PhotoCollection.photographer_id)
        category, style = None, None
        if category_id is not None:
            query = query.filter(PhotoCollection.category_id == category_id)
            category = EnumValues.query.get(category_id)
        if style_id is not None:
            query = query.filter(PhotoCollection.style_id == style_id)
            style = EnumValues.query.get(style_id)
        if min_price is not None:
            if include_none_price:
                query = query.filter(or_(PhotoCollection.price >= min_price, PhotoCollection.price.is_(None)))
            else:
                query = query.filter(PhotoCollection.price >= min_price)
        if max_price is not None:
            if include_none_price:
                query = query.filter(or_(PhotoCollection.price <= max_price, PhotoCollection.price.is_(None)))
            else:
                query = query.filter(PhotoCollection.price <= max_price)
        if min_date is not None:
            if include_none_date:
                query = query.filter(or_(PhotoCollection.date >= min_date, PhotoCollection.date.is_(None)))
            else:
                query = query.filter(PhotoCollection.date >= min_date)
        if max_date is not None:
            if include_none_date:
                query = query.filter(or_(PhotoCollection.date <= max_date, PhotoCollection.date.is_(None)))
            else:
                query = query.filter(PhotoCollection.date <= max_date)
        collections = query.filter(PhotoCollection.photos.any()).all()

        result_list = []
        for c in collections:
            if c.photographer not in result_list:
                result_list.append(c.photographer)
    else:
        pg_type = EnumValues.find_one_by_code(const.PHOTOGRAPHER_USER_TYPE)
        result_list = User.query.filter_by(type_id=pg_type.id).all()
    return rt("photograph.html", result_list=result_list, categories=categories,
              styles=styles, category=category, style=style, route='/photograph',
              min_price=min_price, max_price=max_price, include_none_price=include_none_price,
              min_date=min_date, max_date=max_date, include_none_date=include_none_date)
def query_for_photo_collection(category_id, include_none_date, include_none_price,
                               max_date, max_price, min_date, min_price, style_id):
    """
    通过一组条件查询作品集
    :param category_id: 作品的分类id
    :param style_id: 作品的风格id
    :param include_none_date:是否包括日期为空的记录
    :param include_none_price:是否包括价格为空的记录
    :param max_date:最大的作品日期
    :param max_price: 最大的价格
    :param min_date: 最小的作品日期
    :param min_price: 最小的日期
    :return: 查询到的作品集的列表,查询条件的作品分类,查询条件的作品风格
    """
    query = AppInfo.get_db().session.query(PhotoCollection)
    category, style = None, None
    if category_id is not None:
        query = query.filter(PhotoCollection.category_id == category_id)
        category = EnumValues.query.get(category_id)
    if style_id is not None:
        query = query.filter(PhotoCollection.style_id == style_id)
        style = EnumValues.query.get(style_id)
    if min_price is not None:
        if include_none_price:
            query = query.filter(or_(PhotoCollection.price >= min_price, PhotoCollection.price.is_(None)))
        else:
            query = query.filter(PhotoCollection.price >= min_price)
    if max_price is not None:
        if include_none_price:
            query = query.filter(or_(PhotoCollection.price <= max_price, PhotoCollection.price.is_(None)))
        else:
            query = query.filter(PhotoCollection.price <= max_price)
    if min_date is not None:
        if include_none_date:
            query = query.filter(or_(PhotoCollection.date >= min_date, PhotoCollection.date.is_(None)))
        else:
            query = query.filter(PhotoCollection.date >= min_date)
    if max_date is not None:
        if include_none_date:
            query = query.filter(or_(PhotoCollection.date <= max_date, PhotoCollection.date.is_(None)))
        else:
            query = query.filter(PhotoCollection.date <= max_date)
    collections = query.filter(PhotoCollection.photos.any()).all()
    return collections, category, style
Exemple #5
0
def date_status_json(photographer_id):
    year = int(request.args.get('ano'))
    month = int(request.args.get('mes'))
    month_begin = date(year, month, 1)
    month_end = date(year, month, calendar.monthrange(year, month)[1])
    query = AppInfo.get_db().session.query(DateStatus)
    date_statuses = query.filter(DateStatus.user_id == photographer_id) \
        .filter(or_(and_(DateStatus.start_date >= month_begin, DateStatus.start_date <= month_end),
                    and_(DateStatus.end_date >= month_begin, DateStatus.end_date <= month_end))).all()
    events = []
    for ds in date_statuses:
        day_count = (ds.end_date - ds.start_date).days + 1
        for single_date in [d for d in (ds.start_date + timedelta(n) for n in range(day_count)) if d <= ds.end_date]:
            event = dict()
            event['date'] = '{dt.day}/{dt.month}/{dt.year}'.format(dt=single_date)
            event['title'] = u'不可用'
            event['color'] = u'rgba(190, 190, 190, 0.6)'
            event['class'] = u'unavailable'
            events.append(event)
    return jsonify(events=events)
Exemple #6
0
# encoding=utf-8
import uuid
from app import app_provider, AppInfo
from app.const import MESSAGE_STATUS_UNREAD
from app.forms import UserRegisterForm
from app.models import Image, Message, EnumValues
from flask import render_template
from flask.ext.login import current_user
from flask.ext.security import LoginForm

gallery_service = app_provider.AppInfo.get_galleries_store_service()
image_service = app_provider.AppInfo.get_image_store_service()
app = app_provider.AppInfo.get_app()
db = AppInfo.get_db()


def rt(template_html, **args):
    if current_user.get_id() is not None:
        msgs = db.session.query(Message).outerjoin(EnumValues, Message.status) \
            .filter(EnumValues.code == MESSAGE_STATUS_UNREAD).filter(
            Message.receive_user_id == current_user.id).all()
        unread_msg_size = len(msgs)
    else:
        unread_msg_size = 0
    return render_template(template_html, login_user_form=LoginForm(),
                           unread_msg_size=unread_msg_size, register_user_form=UserRegisterForm(), **args)


def default_date_formatter(view, context, model, name):
    """
    默认的日期格式化器,格式化为 四位年/两位月/两位日
def add_photo_works(files, photo_collection):
    for img in files:
        work = PhotoWork()
        work.photo_collection_id = photo_collection.id
        view_util.save_photo_work_image(work, img)
        AppInfo.get_db().session.add(work)