def produce_submit(): ok = False form = SaveApi(request.values) if not form.validate_on_submit(): return jsonify(code=3004, message=str(form.errors)) data = request.values.to_dict() if not data: return jsonify(code=3003, message='至少传入一个参数!') try: if not (data['title'] and data['channel']): return jsonify(code=3003, message='标题和渠道为必填') if not data['img_urls']: return jsonify(code=3003, message='img_urls为空') produce = Produce.objects(title=data['title'], channel=data['channel']).first() if not produce: produce_data = data.copy() produce_data.pop('img_urls') produce = Produce(**produce_data) produce.save() else: # if produce.editor_id != 0: if produce.editor_id == 0: produce_data = data.copy() produce_data.pop('img_urls') ok = produce.update(**produce_data) if not ok: return jsonify(code=3003, message='产品更新失败') img_urls = data['img_urls'].split(',') for i, img_url in enumerate(img_urls): img_data = {} img_data['title'] = data['title'] img_data['img_url'] = img_url img_data['channel'] = data['channel'] img_data['url'] = data['url'] img_data['target_id'] = str(produce._id) img = Image.objects(img_url=img_url).first() if not img: img = Image(**img_data) ok = img.save() else: ok = img.update(target_id=str(produce._id)) if i == 0: if not produce.cover_id: ok = produce.update(cover_id=str(img._id)) if ok: return jsonify(code=0, message='success!', data=produce) else: return jsonify(code=3010, message='操作失败!') except (Exception) as e: return jsonify(code=3011, message=str(e))
def category_submit(name): form = SubmitForm(request.form) category = db.session.query(Category).filter_by(name=name).one() if request.method == 'POST' and form.validate(): file = request.files['image'] if not file: filename = form.link.data else: filename = file.filename image = Image(session['user'], category, form.name.data, form.text.data, form.link.data, filename) db.session.add(image) db.session.flush() if file and image.allowed: image.save(file) elif image.allowed: image.save() else: db.session.rollback() flash(local.category_submit['INVALID_IMAGE'], 'error') return render('category_submit.html', title=local.category['TITLE_SUBMIT'], category=category, form=form) image.save_thumbnail() image.save_exif() # Self upvote image.vote(1, session['user']) db.session.commit() flash(local.category_submit['IMAGE_POSTED'], 'success') return redirect(url_for('category_one', name=name)) flash_errors(form) return render('category_submit.html', title=local.category['TITLE_SUBMIT'], category=category, form=form)
def image_submit(): form = SaveApi(request.values) if not form.validate_on_submit(): return jsonify(code=3004, message=str(form.errors)) data = request.values.to_dict() if not data: return jsonify(code=3003, message='至少传入一个参数!') try: item = Image(**data) ok = item.save() if ok: return jsonify(code=0, message='success!', data=item) else: return jsonify(code=3010, message='操作失败!') except (Exception) as e: return jsonify(code=3011, message=str(e))
if uploadimages: users = User.query.all() categories = Category.query.all() for user in users: chance = random.randint(1, 100) if chance > 95: try: # Add image name = ''.join(random.choice(string.ascii_lowercase + ' -_') for x in range(1, random.randint(4, 30))) text = ''.join(random.choice(string.ascii_lowercase + ' -_') for x in range(1, random.randint(20, 200))) filename = 'http://i.imgur.com/7ZVtcSs.jpg' image = Image(user.id, categories[random.randint(0, len(categories)-1)], name, text, filename, filename) db.session.add(image) db.session.flush() image.save() image.save_thumbnail() image.save_exif() image.vote(1, user.id) db.session.commit() except: pass if imageactions: users = User.query.all() images = Image.query.all() i = 0 for user in users: i += 1