def cover(self): if self.cover_id and len(self.cover_id) == 12: asset = Asset.objects(_id=ObjectId(self.cover_id)).first() if asset and asset.deleted == 0: return asset asset = Asset.objects(target_id=str(self._id), asset_type=5, deleted=0).first() if asset and asset.deleted == 0: return asset return None
def asset_ajx_list(): page = int(request.args.get('page', 1)) per_page = int(request.args.get('per_page', 10)) target_id = request.args.get('target_id', '') asset_type = int(request.args.get('asset_type', 1)) cover_id = request.args.get('cover_id', '') meta = {} query = {} if target_id: query['target_id'] = target_id query['asset_type'] = asset_type query['deleted'] = 0 try: data = Asset.objects(**query).order_by('-created_at').paginate( page=page, per_page=per_page) total_count = Asset.objects(**query).count() # 过滤数据 fields = [] for i, d in enumerate(data.items): thumb_dict = d.get_thumb_path() thumb_url = '' is_cover = False if cover_id == str(d._id): is_cover = True if thumb_dict: thumb_url = thumb_dict['sm'] field = { '_id': str(d._id), 'name': d.name, 'path': d.path, 'asset_type': d.asset_type, 'domain': d.domain, 'thumb_url': thumb_url, 'is_cover': is_cover } fields.append(field) meta['rows'] = fields except (Exception) as e: meta['rows'] = [] total_count = 0 meta['total_count'] = total_count meta['page'] = page meta['per_page'] = per_page return jsonify(code=0, message='success!', data=meta)
def save(self, **param): data = self.data asset_ids = data['asset_ids'] if data['title']: if Column.objects(title=data['title']).first(): raise ValueError('栏目信息已存在!') data['user_id'] = param['user_id'] data.pop('id') data.pop('asset_ids') data.pop('asset_type') item = Column(**data) ok = item.save() if not ok: raise ValueError('保存失败!') # 更新附件 if asset_ids: asset_arr = asset_ids.strip().split(',') for asset_id in asset_arr: try: asset = Asset.objects(_id=ObjectId(asset_id)).first() if asset: asset.update(target_id=str(item._id)) except (Exception) as e: continue return item
def mark_recovery(self): ok = super(Brand, self).update(deleted=0) if ok: # 恢复附件 assets = Asset.objects(target_id=str(self._id), asset_type=5, deleted=1) for item in assets: item.update(deleted=0)
def mark_delete(self): ok = super(Column, self).update(deleted=1) if ok: # 删除附件 assets = Asset.objects(target_id=str(self._id), asset_type=7, deleted=0) for item in assets: item.mark_delete()
def asset_submit(): meta = metaInit.copy() id = request.args.get('id', None) meta['data'] = None if id: item = Asset.objects(_id=ObjectId(id)).first() if not item: return jsonify(success=False, message='内容不存在!') meta['data'] = item form = SaveForm() meta['referer_url'] = request.environ.get('HTTP_REFERER') if request.environ.get('HTTP_REFERER') else '' return render_template('admin/asset/submit.html', meta=meta, form=form)
def asset_ajx_del(): id = request.args.get('id', '') if not id: return jsonify(code=500, message='缺少请求参数!') try: asset = Asset.objects(_id=ObjectId(id)).first() if not asset: return jsonify(code=500, message='内容不存在!') if not g.is_edit: return jsonify(code=500, message='没有权限!') ok = asset.mark_delete() return jsonify(code=0, message='success') except (Exception) as e: return jsonify(code=500, message=str(e))
def asset_recovery(): meta = {} ids = request.values.get('ids', '') type = request.values.get('type', 1) if not ids: return jsonify(success=False, message='缺少请求参数!') try: arr = ids.split(',') for d in arr: item = Asset.objects(_id=ObjectId(d)).first() item.mark_recovery() if item else None except(Exception) as e: return jsonify(success=False, message=str(e)) return jsonify(success=True, message='操作成功!', data={'ids': ids, 'type':type}, redirect_to=url_for('admin.asset_list'))
def asset_list(): meta = metaInit.copy() query = {} page = force_int(request.args.get('page', 1)) per_page = force_int(request.args.get('per_page', 100)) assetType = force_int(request.args.get('asset_type', 0)) status = force_int(request.args.get('status', 0)) deleted = force_int(request.args.get('deleted', 0)) kind = force_int(request.args.get('kind', 0)) domain = request.args.get('domain', '') t = force_int(request.args.get('t', 1), 1) q = request.args.get('q', '') if q: if t==1: query['_id'] = ObjectId(q) if t==2: query['name'] = {"$regex": q.strip()} if assetType: query['asset_type'] = assetType if domain: query['domain'] = domain if status == -1: meta['css_disable'] = 'active' query['status'] = 0 if status == 1: query['status'] = 1 meta['css_enable'] = 'active' else: pass if deleted == 1: query['deleted'] = 1 meta['css_deleted'] = 'active' else: query['deleted'] = 0 if not status and not deleted: meta['css_all'] = 'active' else: meta['css_all'] = '' page_url = url_for('admin.asset_list', page="#p#", q=q, t=t, kind=kind, status=status) data = Asset.objects(**query).order_by('-created_at').paginate(page=page, per_page=per_page) total_count = Asset.objects(**query).count() # 过滤数据 for i, d in enumerate(data.items): data.items[i]._id = str(d._id) data.items[i].thumb = d.get_thumb_path() meta['data'] = data.items meta['total_count'] = total_count pager = Pager(page, per_page, total_count, page_url) meta['pager'] = pager.render_view() return render_template('admin/asset/list.html', meta=meta)