def accounts(): u"""用户列表""" page = request.args.get("page", 1, type=int) per_page = request.args.get("per_page", Account.PER_PAGE, type=int) q = request.args.get("q") with db_session_cm() as session: search = False users_query = session.query(Account).outerjoin(Account.avatar) if q: search = True q_string = "%%%s%%" % q users_query = users_query.filter( or_(Account.cellphone.like(q_string), Account.nickname.like(q_string)) ) common_logger.debug(users_query) pagination = Pagination(found=users_query.count(), total=users_query.count(), page=page, search=search, record_name=_(u"用户"), bs_version=3) users = users_query.order_by(Account.id.desc()).offset((page - 1) * per_page).limit(per_page) context = { "users": users.all(), "pagination": pagination, "q": q } return render_template("admin/account/index.html", **context)
def import_friends(cls, session, account_id, contacts): u"""导入联系人 :param session: A DB session instance :param account_id: 需要添加朋友的账户id :param contacts: A list with dict content. format: contacts = [ { "phone": "15333333331", "name": "小芳", "email": "*****@*****.**" }, { "phone": "15333333332", "name": "小明", "email": "*****@*****.**" } ] the "phone" and "name" is required, "email" is optional. 导入原则: 1. "phone": 已经存在的,检查是否是朋友关系,如不是,添加成朋友; 2. "phone": 不存在直接写入; 3. "name" 重名的,采用重新修改"name"为 "name" + "phone"; """ account = session.query(Account).get(account_id) for contact in contacts: phone = contact.get("phone") name = contact.get("name") email = contact.get("email") friend = session.query(Account).filter(Account.cellphone == phone).first() if not friend: # 朋友不存在, 创建 new_friend = Account(phone, phone[5:11]) # 设置成未注册 new_friend.status = Account.STATUS_UNREGISTERED if email: new_friend.email = email common_logger.debug(account.to_friends) af = AccountFriend() af.from_account = account af.to_account = new_friend af.nickname = name session.add(new_friend) session.add(af) else: # 存在, 检测是否已经是朋友关系 common_logger.debug(friend) af_query = session.query(AccountFriend).\ filter(AccountFriend.from_account == account).\ filter(AccountFriend.to_account == friend) common_logger.debug(af_query) exists_af = af_query.first() common_logger.debug(exists_af) if not exists_af: # 不是朋友关系则添加成朋友关系 af = AccountFriend() af.from_account = account af.to_account = friend af.nickname = name session.add(af)
def account_show(account_id): u"""查看用户""" with db_session_cm() as session: account = session.query(Account).get(account_id) a_af_query = session.query(Account, AccountFriend).\ join(AccountFriend, AccountFriend.to_account_id == Account.id).\ filter(AccountFriend.from_account_id == account_id) common_logger.debug(a_af_query) results = a_af_query.all() common_logger.debug(results) return render_template("admin/account/show.html", account=account, results=results)
def send(self, content=None): username = setting.SMS["username"] password = setting.SMS["password"] api_key = setting.SMS["apikey"] params = { 'username': username, 'password': password, 'apikey': api_key, 'mobile': self.phone, 'content': content or self.content, 'encode': 1 # 不转码 } headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} result = requests.post(SmsSender.URL, params=params, timeout=30, headers=headers) common_logger.debug(result.text) status, message = result.text.split(":") self.status = status self.message = message
def plan_update(plan_id): u"""更新方案""" try: plan_form = PlanForm(request.form) plan_form.keywords.choices = PlanKeyword.choices() if request.method == 'POST' and plan_form.validate_on_submit(): with db_session_cm() as session: title = plan_form.title.data.strip() content = plan_form.content.data.strip() keywords = plan_form.keywords.data request_file = request.files['image'] plan = session.query(Plan).options(subqueryload(Plan.content)).\ join(Plan.keywords).filter(Plan.id == plan_id).first() plan.content.content = content plan.title = title if request_file: name, suffix = os.path.splitext(request_file.filename) filename = image_resources.save(request_file, folder=str(current_user.id)) common_logger.debug(plan.cover_image) common_logger.debug(type(plan.cover_image)) cover_image = plan.cover_image or ImageResource(filename, current_user.id) common_logger.debug(cover_image) cover_image.path = filename cover_image.format = suffix plan.cover_image = cover_image if keywords: keywords = session.query(PlanKeyword).filter(PlanKeyword.id.in_(keywords)).all() plan.keywords = keywords session.add(plan) session.commit() flash(_(u"方案编辑成功!"), category="success") return redirect(url_for('admin_frontend.plan_show', plan_id=plan.id)) except Exception, e: common_logger.error(traceback.format_exc(e)) flash(_(u"方案编辑失败!"), category="danger") return redirect(url_for('admin_frontend.plan_show', plan_id=plan_id))