Ejemplo n.º 1
0
def user_list():
    user_dbs, more_cursor = util.retrieve_dbs(
        model.User.query(),
        limit=util.param("limit", int),
        cursor=util.param("cursor"),
        order=util.param("order") or "-created",
        admin=util.param("admin", bool),
        active=util.param("active", bool),
        permissions=util.param("permissions", list),
    )

    if flask.request.path.startswith("/_s/"):
        return util.jsonify_model_dbs(user_dbs, more_cursor)

    permissions = list(UserUpdateForm._permission_choices)
    permissions += util.param("permissions", list) or []
    return flask.render_template(
        "user/user_list.html",
        html_class="user-list",
        title="User List",
        user_dbs=user_dbs,
        more_url=util.generate_more_url(more_cursor),
        has_json=True,
        permissions=sorted(set(permissions)),
    )
Ejemplo n.º 2
0
def user_list():
    user_dbs, more_cursor = util.retrieve_dbs(
        model.User.query(),
        limit=util.param('limit', int),
        cursor=util.param('cursor'),
        order=util.param('order') or '-created',
        admin=util.param('admin', bool),
        active=util.param('active', bool),
        permissions=util.param('permissions', list),
    )

    if flask.request.path.startswith('/_s/'):
        return util.jsonify_model_dbs(user_dbs, more_cursor)

    permissions = list(UserUpdateForm._permission_choices)
    permissions += util.param('permissions', list) or []
    return flask.render_template(
        'user/user_list.html',
        html_class='user-list',
        title='User List',
        user_dbs=user_dbs,
        more_url=util.generate_more_url(more_cursor),
        has_json=True,
        permissions=sorted(set(permissions)),
    )
Ejemplo n.º 3
0
def user_list():
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order') or '-created',
      name=util.param('name'),
      admin=util.param('admin', bool),
      active=util.param('active', bool),
      permissions=util.param('permissions', list),
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs, more_cursor)

  permissions = list(UserUpdateForm._permission_choices)
  permissions += util.param('permissions', list) or []
  return flask.render_template(
      'user/user_list.html',
      html_class='user-list',
      title='User List',
      user_dbs=user_dbs,
      more_url=util.generate_more_url(more_cursor),
      has_json=True,
      permissions=sorted(set(permissions)),
    )
Ejemplo n.º 4
0
def ads(id=0,act=''):
  if act=='delete' and id:
    #delete
    k=ndb.Key(cms.Ads,id)
    k.delete()
    flask.flash('delete advertisement success', category='success')
    memcache.delete(memkey.ads_key)
    return flask.redirect(flask.url_for('ads'))
  query=cms.Ads.query()
  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(query.fetch())
  btn = 'Create'
  form = AdsForm()
  if id:
    obj=cms.Ads.get_by_id(id)
    if not obj:
      flask.flash('invalid obj id', category='danger')
      return flask.render_template('admin/ads.html',has_json=True,form=form,data=query,btn=btn)
    else:
      btn = 'Save'
      form=AdsForm(obj=obj)
  elif flask.request.method=='GET':
    # init some ad itmes
    ads_for_add = []
    for ad in ADS_POSITION:
      if not query.filter(cms.Ads.name == ad).get():
        ads_for_add.append(cms.Ads(name=ad))
    if ads_for_add:
      ndb.put_multi(ads_for_add)
  if form.validate_on_submit():
    name=form.name.data.lower()
    if id:
      # update
      # check existence
      if obj.name.lower() != name:
        if query.filter(cms.Ads.name==name,
          cms.Ads.key!=ndb.Key(cms.Ads,id)).get():
          flask.flash('name exist',category='danger')
          return flask.render_template('admin/ads.html',has_json=True,form=form,data=query,btn=btn)
      form.name.data=name
      form.populate_obj(obj)
      obj.put()
      flask.flash('update success',category='success')
    else:
      # create
      # check existence
      if query.filter(cms.Ads.name==name).get():
        flask.flash('name exist',category="danger")
        return flask.render_template('admin/ads.html',has_json=True,form=form,data=query,btn=btn)
      item = cms.Ads(
        name=name,
        value=form.value.data,
        description=form.description.data)
      item.put()
      flask.flash('ads add success',category='success')
    memcache.delete(memkey.ads_key)
    return flask.redirect(flask.url_for('ads'))
  #get
  else:
    return flask.render_template('admin/ads.html',has_json=True,form=form,data=query,btn=btn)
Ejemplo n.º 5
0
def user_merge():
    user_keys = util.param('user_keys', list)
    if not user_keys:
        flask.abort(400)

    user_db_keys = [ndb.Key(urlsafe=k) for k in user_keys]
    user_dbs = ndb.get_multi(user_db_keys)
    if len(user_dbs) < 2:
        flask.abort(400)

    if flask.request.path.startswith('/_s/'):
        return util.jsonify_model_dbs(user_dbs)

    user_dbs.sort(key=lambda user_db: user_db.created)
    merged_user_db = user_dbs[0]
    auth_ids = []
    permissions = []
    is_admin = False
    is_active = False
    for user_db in user_dbs:
        auth_ids.extend(user_db.auth_ids)
        permissions.extend(user_db.permissions)
        is_admin = is_admin or user_db.admin
        is_active = is_active or user_db.active
        if user_db.key.urlsafe() == util.param('user_key'):
            merged_user_db = user_db

    auth_ids = sorted(list(set(auth_ids)))
    permissions = sorted(list(set(permissions)))
    merged_user_db.permissions = permissions
    merged_user_db.admin = is_admin
    merged_user_db.active = is_active

    form_obj = copy.deepcopy(merged_user_db)
    form_obj.user_key = merged_user_db.key.urlsafe()
    form_obj.user_keys = ','.join(user_keys)

    form = UserMergeForm(obj=form_obj)
    if form.validate_on_submit():
        form.populate_obj(merged_user_db)
        merged_user_db.auth_ids = auth_ids
        merged_user_db.put()

        deprecated_keys = [
            key for key in user_db_keys if key != merged_user_db.key
        ]
        merge_user_dbs(merged_user_db, deprecated_keys)
        return flask.redirect(
            flask.url_for('user_update', user_id=merged_user_db.key.id()), )

    return flask.render_template(
        'user/user_merge.html',
        title='Merge Users',
        html_class='user-merge',
        user_dbs=user_dbs,
        merged_user_db=merged_user_db,
        form=form,
        auth_ids=auth_ids,
    )
Ejemplo n.º 6
0
def user_merge():
  user_keys = util.param('user_keys', list)
  if not user_keys:
    flask.abort(400)

  user_db_keys = [ndb.Key(urlsafe=k) for k in user_keys]
  user_dbs = ndb.get_multi(user_db_keys)
  if len(user_dbs) < 2:
    flask.abort(400)

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs)

  user_dbs.sort(key=lambda user_db: user_db.created)
  merged_user_db = user_dbs[0]
  auth_ids = []
  permissions = []
  is_admin = False
  is_active = False
  for user_db in user_dbs:
    auth_ids.extend(user_db.auth_ids)
    permissions.extend(user_db.permissions)
    is_admin = is_admin or user_db.admin
    is_active = is_active or user_db.active
    if user_db.key.urlsafe() == util.param('user_key'):
      merged_user_db = user_db

  auth_ids = sorted(list(set(auth_ids)))
  permissions = sorted(list(set(permissions)))
  merged_user_db.permissions = permissions
  merged_user_db.admin = is_admin
  merged_user_db.active = is_active
  merged_user_db.verified = False

  form_obj = copy.deepcopy(merged_user_db)
  form_obj.user_key = merged_user_db.key.urlsafe()
  form_obj.user_keys = ','.join(user_keys)

  form = UserMergeForm(obj=form_obj)
  if form.validate_on_submit():
    form.populate_obj(merged_user_db)
    merged_user_db.auth_ids = auth_ids
    merged_user_db.put()

    deprecated_keys = [k for k in user_db_keys if k != merged_user_db.key]
    merge_user_dbs(merged_user_db, deprecated_keys)
    return flask.redirect(
        flask.url_for('user_update', user_id=merged_user_db.key.id()),
      )

  return flask.render_template(
      'user/user_merge.html',
      title='Merge Users',
      html_class='user-merge',
      user_dbs=user_dbs,
      merged_user_db=merged_user_db,
      form=form,
      auth_ids=auth_ids,
    )
Ejemplo n.º 7
0
def admin_pay_upgrade_service():
  pay_dbs, pay_cursor = util.get_dbs(
      model.Pay.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order'),
    )
  ndb.put_multi(pay_dbs)
  return util.jsonify_model_dbs(pay_dbs, pay_cursor)
Ejemplo n.º 8
0
def category(cateid=0,act=''):
  if act=='delete' and cateid:
    #delete
    k=ndb.Key(cms.Category,cateid)
    k.delete()
    flask.flash('delete category success', category='success')
    memcache.delete(memkey.cate_key)
    return flask.redirect(flask.url_for('category'))
  query=cms.Category.query()
  cate_dbs=cms.Category.allcates()
  if flask.request.path.startswith('/_s/'):
    # if cateid:
    #   return util.jsonify_model_db(cms.Category.get_by_id(cateid))
    # else:
    return util.jsonify_model_dbs(cate_dbs)
  btn = 'Create'
  form = CategoryForm()
  if cateid:
    obj=cms.Category.get_by_id(cateid)
    if not obj:
      flask.flash('invalid obj id', category='danger')
      return flask.render_template('admin/category.html',has_json=True,form=form,data=cate_dbs,btn=btn)
    else:
      btn = 'Save'
      form=CategoryForm(obj=obj)
  if form.validate_on_submit():
    name=form.name.data.capitalize()
    if cateid:
      # update
      # check existence
      if obj.name.capitalize() != name:
        if query.filter(cms.Category.name==name,
          cms.Category.key!=ndb.Key(cms.Category,cateid)).get():
          flask.flash('name exist',category='danger')
          return flask.render_template('admin/category.html',has_json=True,form=form,data=query,btn=btn)
      form.name.data=name
      form.populate_obj(obj)
      obj.put()
      flask.flash('update success',category='success')
    else:
      # create
      # check existence
      if query.filter(cms.Category.name==name).get():
        flask.flash('name exist',category="danger")
        return flask.render_template('admin/category.html',form=form,data=query,btn=btn)
      cate = cms.Category(
        name=name,
        slug=form.slug.data,
        sort=form.sort.data)
      cate.put()
      flask.flash('category add success',category='success')
    memcache.delete(memkey.cate_key)
    return flask.redirect(flask.url_for('category'))
  #get
  else:
    return flask.render_template('admin/category.html',has_json=True,form=form,data=query,btn=btn)
Ejemplo n.º 9
0
def admin_tournament_update():
  tournament_dbs, tournament_cursor = util.get_dbs(
      model.Tournament.query(),
      limit=util.param('limit', int) or config.DEFAULT_DB_LIMIT,
      order=util.param('order'),
      cursor=util.param('cursor'),
    )

  ndb.put_multi(tournament_dbs)
  return util.jsonify_model_dbs(tournament_dbs, tournament_cursor)
Ejemplo n.º 10
0
def my_tournament_list():
  tournament_dbs, next_cursor = model.Tournament.get_dbs(user_key=auth.current_user_key())

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(tournament_dbs, next_cursor)

  return flask.render_template(
      'tournament/my_tournament_list.html',
      html_class='my-tournament-list',
      title='My Tournaments',
      tournament_dbs=tournament_dbs,
      next_url=util.generate_next_url(next_cursor),
  )
Ejemplo n.º 11
0
def tournament_list():
  tournament_dbs, next_cursor = model.Tournament.get_dbs(is_public=True)

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(tournament_dbs, next_cursor)

  return flask.render_template(
      'tournament/tournament_list.html',
      html_class='tournament-list',
      title='Tournaments',
      tournament_dbs=tournament_dbs,
      next_url=util.generate_next_url(next_cursor),
  )
Ejemplo n.º 12
0
def update_user_tutor():
    event_dbs, event_cursor = util.get_dbs(
        model.Event.query(),
        limit=util.param("limit", int) or config.MAX_DB_LIMIT,
        order=util.param("order"),
        cursor=util.param("cursor"),
    )

    for event_db in event_dbs:
        event_db.accuracy = "day"

    ndb.put_multi(event_dbs)
    return util.jsonify_model_dbs(event_dbs, event_cursor)
Ejemplo n.º 13
0
def links(id=0,act=''):
  if act=='delete' and id:
    #delete
    k=ndb.Key(cms.Links,id)
    k.delete()
    flask.flash('delete link success', category='success')
    memcache.delete(memkey.links_key)
    return flask.redirect(flask.url_for('links'))
  query=cms.Links.query()
  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(query.fetch())
  btn = 'Create'
  form = LinksForm()
  if id:
    obj=cms.Links.get_by_id(id)
    if not obj:
      flask.flash('invalid obj id', category='danger')
      return flask.render_template('admin/links.html',has_json=True,form=form,data=query,btn=btn)
    else:
      btn = 'Save'
      form=LinksForm(obj=obj)
  if form.validate_on_submit():
    name=form.name.data
    if id:
      # update
      # check existence
      if obj.name != name:
        if query.filter(cms.Links.name==name,
          cms.Links.key!=ndb.Key(cms.Links,id)).get():
          flask.flash('name exist',category='danger')
          return flask.render_template('admin/links.html',has_json=True,form=form,data=query,btn=btn)
      form.populate_obj(obj)
      obj.put()
      flask.flash('update success',category='success')
    else:
      # create
      # check existence
      if query.filter(cms.Links.name==name).get():
        flask.flash('name exist',category="danger")
        return flask.render_template('admin/links.html',has_json=True,form=form,data=query,btn=btn)
      item = cms.Links(
        name=name,
        url=form.url.data,
        sort=form.sort.data)
      item.put()
      flask.flash('link add success',category='success')
    memcache.delete(memkey.links_key)
    return flask.redirect(flask.url_for('links'))
  #get
  else:
    return flask.render_template('admin/links.html',has_json=True,form=form,data=query,btn=btn)
Ejemplo n.º 14
0
def resource_list():
  resource_dbs, resource_cursor = auth.current_user_db().get_resource_dbs()

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(resource_dbs, resource_cursor)

  return flask.render_template(
      'resource/resource_list.html',
      html_class='resource-list',
      title='Resource List',
      resource_dbs=resource_dbs,
      next_url=util.generate_next_url(resource_cursor),
      has_json=True,
    )
Ejemplo n.º 15
0
def user_list():
    user_dbs, user_cursor = model.User.get_dbs()

    if flask.request.path.startswith('/_s/'):
        return util.jsonify_model_dbs(user_dbs, user_cursor)

    permissions = list(UserUpdateForm._permission_choices)
    permissions += util.param('permissions', list) or []
    return flask.render_template(
        'user/user_list.html',
        html_class='user-list',
        title='User List',
        user_dbs=user_dbs,
        next_url=util.generate_next_url(user_cursor),
        has_json=True,
        permissions=sorted(set(permissions)),
    )
Ejemplo n.º 16
0
def user_list():
  user_dbs, user_cursor = model.User.get_dbs(email=util.param('email'))

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs, user_cursor)

  permissions = list(UserUpdateForm._permission_choices)
  permissions += util.param('permissions', list) or []
  return flask.render_template(
      'user/user_list.html',
      html_class='user-list',
      title='User List',
      user_dbs=user_dbs,
      next_url=util.generate_next_url(user_cursor),
      has_json=True,
      permissions=sorted(set(permissions)),
    )
Ejemplo n.º 17
0
def admin_event_list():
    event_dbs, next_cursor = util.get_dbs(
        model.Event.query(),
        limit=util.param("limit", int),
        cursor=util.param("cursor"),
        order=util.param("order") or "user_key,-timestamp,accuracy,-created",
    )

    if flask.request.path.startswith("/_s/"):
        return util.jsonify_model_dbs(event_dbs, next_cursor)

    return flask.render_template(
        "admin/event_list.html",
        html_class="admin-event-list",
        title="Event List",
        event_dbs=event_dbs,
        next_url=util.generate_next_url(next_cursor),
    )
Ejemplo n.º 18
0
def admin_user_update():
  user_dbs, user_cursor = util.get_dbs(
      model.User.query(),
      limit=util.param('limit', int) or config.DEFAULT_DB_LIMIT,
      order=util.param('order'),
      cursor=util.param('cursor'),
    )

  updated_dbs = []
  for user_db in user_dbs:
    if not user_db.birthdate:
      user_db.birthdate = None
      updated_dbs.append(user_db)

  if updated_dbs:
    ndb.put_multi(updated_dbs)

  return util.jsonify_model_dbs(updated_dbs, user_cursor)
Ejemplo n.º 19
0
def event_list_service(username=None):
  if auth.is_logged_in():
    user_db = auth.current_user_db()
  else:
    user_dbs, user_cursor = model.User.get_dbs(is_public=True, limit=10)
    user_db = random.choice(user_dbs) if user_dbs else None

  if username and user_db.username != username:
    if not user_db.admin:
      return flask.abort(404)
    user_db = model.User.get_by('username', username)

  if not user_db:
    return flask.abort(404)

  event_dbs, next_cursor = user_db.get_event_dbs()

  return util.jsonify_model_dbs(event_dbs, next_cursor)
Ejemplo n.º 20
0
def admin_event_list():
  event_dbs, next_cursor = util.get_dbs(
      model.Event.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order') or 'user_key,-timestamp,accuracy,-created',
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(event_dbs, next_cursor)

  return flask.render_template(
      'admin/event_list.html',
      html_class='admin-event-list',
      title='Event List',
      event_dbs=event_dbs,
      next_url=util.generate_next_url(next_cursor),
    )
Ejemplo n.º 21
0
def index():
    user_dbs, more_cursor = util.retrieve_dbs(
        User,
        User.query(),
        limit=util.param('limit', int),
        cursor=util.param('cursor'),
        order=util.param('order'),
        name=util.param('name'),
      )

    if request.path.startswith('/_json/'):
        return util.jsonify_model_dbs(user_dbs, more_cursor)
    return render_template(
        'admin/user/index.html',
        title=u'Пользователи',
        user_dbs=user_dbs,
        more_url=util.generate_more_url(more_cursor)
    )
Ejemplo n.º 22
0
def resource_list():
    resource_dbs, more_cursor = util.retrieve_dbs(
        model.Resource.query(),
        user_key=auth.current_user_key(),
        limit=util.param('limit', int),
        cursor=util.param('cursor'),
        order=util.param('order') or '-created',
    )

    if flask.request.path.startswith('/_s/'):
        return util.jsonify_model_dbs(resource_dbs, more_cursor)

    return flask.render_template(
        'resource/resource_list.html',
        html_class='resource-list',
        title='Resource List',
        resource_dbs=resource_dbs,
        more_url=util.generate_more_url(more_cursor),
    )
Ejemplo n.º 23
0
def resource_list():
  resource_dbs, more_cursor = util.retrieve_dbs(
      model.Resource.query(),
      user_key=auth.current_user_key(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order') or '-created',
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(resource_dbs, more_cursor)

  return flask.render_template(
      'resource/resource_list.html',
      html_class='resource-list',
      title='Resource List',
      resource_dbs=resource_dbs,
      more_url=util.generate_more_url(more_cursor),
    )
Ejemplo n.º 24
0
def user_list():
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order') or '-created',
      name=util.param('name'),
      admin=util.param('admin', bool),
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs, more_cursor)

  return flask.render_template(
      'user_list.html',
      html_class='user',
      title=_('User List'),
      user_dbs=user_dbs,
      more_url=util.generate_more_url(more_cursor),
    )
Ejemplo n.º 25
0
def user_list():
  user_dbs, more_cursor = util.retrieve_dbs(
      model.User.query(),
      limit=util.param('limit', int),
      cursor=util.param('cursor'),
      order=util.param('order') or '-created',
      name=util.param('name'),
      admin=util.param('admin', bool),
    )

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(user_dbs, more_cursor)

  return flask.render_template(
      'user_list.html',
      html_class='user',
      title='User List',
      user_dbs=user_dbs,
      more_url=util.generate_more_url(more_cursor),
    )
Ejemplo n.º 26
0
def user_list():
    user_dbs, more_cursor = util.retrieve_dbs(
        model.User.query(),
        limit=util.param("limit", int),
        cursor=util.param("cursor"),
        order=util.param("order") or "-created",
        name=util.param("name"),
        admin=util.param("admin", bool),
    )

    if flask.request.path.startswith("/_s/"):
        return util.jsonify_model_dbs(user_dbs, more_cursor)

    return flask.render_template(
        "user_list.html",
        html_class="user",
        title="User List",
        user_dbs=user_dbs,
        more_url=util.generate_more_url(more_cursor),
    )
Ejemplo n.º 27
0
def event_list(username=None):
  user_db = auth.current_user_db()
  if username and user_db.username != username:
    if not user_db.admin:
      return flask.abort(404)
    user_db = model.User.get_by('username', username)
    if not user_db:
      return flask.abort(404)

  event_dbs, next_cursor = user_db.get_event_dbs()

  if flask.request.path.startswith('/_s/'):
    return util.jsonify_model_dbs(event_dbs, next_cursor)

  return flask.render_template(
      'event/event_list.html',
      html_class='event-list',
      title=_('My Places'),
      event_dbs=event_dbs,
      next_url=util.generate_next_url(next_cursor),
      has_json=True,
      user_db=user_db,
    )
Ejemplo n.º 28
0
def posts_json():
    year = util.param('year', int)
    month = util.param('month', int)
    q = create_posts_query()
    if year:
        if year and month:
            current_month = datetime.datetime(year, month, 1)
            next_month = util.add_months(datetime.datetime(year, month, 1), 1)
        else:
            current_month = datetime.datetime(year, 1, 1)
            next_month = datetime.datetime(year+1, 1, 1)

        q = q.filter(Post.created >= current_month, Post.created < next_month)
        q = util.run_query(q)
    else:
        tag = util.param('tag')
        if tag:
            q = q.filter(Post.tags.IN([tag]))
            q = util.run_query(q)
        else:
            offset = util.param('offset', int) or 0
            limit = util.param('limit', int) or config.ATOM_FEED_LIMIT
            q = util.run_query(q, limit, offset=offset)
    return util.jsonify_model_dbs(q)
Ejemplo n.º 29
0
def products():
    products_obj = Product.query()
    return jsonify_model_dbs(products_obj)
Ejemplo n.º 30
0
def products():
    products_obj = Product.query()
    return jsonify_model_dbs(products_obj)