def new_art(request): ArtDeliverFormSet = formset_factory(ArtDeliverForm,max_num=1,formset=RequiredFormSet) if request.POST: art_form = ArtForm(request.POST) deliver_form = ArtDeliverFormSet(request.POST) if art_form.is_valid() and deliver_form.is_valid(): art_cleaneddata = art_form.cleaned_data art = Art() art.name = art_cleaneddata['name'] art.country = art_cleaneddata['country'] art.save() for frm in deliver_form: deliver_form_cleandata = frm.cleaned_data delivery = Delivery() delivery.country = deliver_form_cleandata['country'] delivery.art = art delivery.price = deliver_form_cleandata['price'] delivery.save() return HttpResponseRedirect('/list') else: return render_to_response('add_art.html',{'form':art_form,'deliver':deliver_form,'error':1}) form = ArtForm(initial={'country': Country.objects.get(id=3)}) deliver_form = ArtDeliverFormSet() return render_to_response('add_art.html',{'form':form,'deliver':deliver_form})
def populate(): agent_add1 = Agent(name='Test 1',phone='0',postcode='AA1AA1') agent_add1.save() agent_add2 = Agent(name='Test 2',phone='1',postcode='AA1AA2') agent_add2.save() agent_add3 = Agent(name='Test 3',phone='2',postcode='AA1AA3') agent_add3.save() count = 0 artist = 0 agents = [agent_add1,agent_add2,agent_add3] while count < 1000: artist = int(artist) if artist % 5 == 0: artist += 1 artist = str(art) ag = agents[count%3] art = Art(name=str(count), sub_category=str(int(artist)%3), category=str(count%10), artist=artist, price=50, quantity=0, agent=ag) art.save() count += 1
def post(self): title = self.request.get("title") art = self.request.get("art") if title and art: a = Art(title=title, art=art) a.put() self.redirect("/") else: error = "We need both a title and artwork!" self.render("art.html", error=error)
def post(self): title = self.request.get("title") category = self.request.get("category") art = self.request.get("art") if (title and category and art): a = Art(title=title, category=category, art=art) a.put() self.redirect("/") else: error = "Invalid inputs!" self.render_front(title, art, error)
def post(self): title = self.request.get("title") art = self.request.get("art") if title and art: a = Art(title = title, art = art, parent= art_key) coords = get_coords(self.request.remote_addr) if coords: a.coords = coords a.put() top_arts(True) self.redirect("/asciichan") else: error = "We need a title and some artwork!" self.render_front(title, art, error)
def art_add(): form = ArtForm() if form.validate_on_submit(): data = form.data # 上传logo file = secure_filename(form.logo.data.filename) logo = change_name(file) if not os.path.exists(app.config['UP']): os.makedirs(app.config["UP"]) # 保存文件 form.logo.data.save(app.config["UP"] + "/" + logo) # 获取用户ID user = User.query.filter_by(name=session["user"]).first() user_id = user.id # 保存数据 art = Art( title=data["title"], cate=data["cate"], user_id=user_id, logo=logo, content=data["content"], addtime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") ) db.session.add(art) db.session.commit() flash("Post Article Successfully!", "ok") return render_template('art_add.html', title="Post Article", form=form)
def art_add(): form = ArtForm() if form.validate_on_submit(): data = form.data # 上传logo # uploads里面存的是图片文件 # 数据库里存的是uploads里面的图片的文件名 file = secure_filename(form.logo.data.filename) logo = change_name(file) # logo是时间加唯一标志服加图片文件后缀 if not os.path.exists(app.config["UP"]): os.makedirs(app.config["UP"]) form.logo.data.save(app.config["UP"] + "/" + logo) # 获取用户ID user = User.query.filter_by(name=session["user"]).first() user_id = user.id # 保存数据 art = Art( title=data["title"], cate=data["cate"], user_id=user_id, logo=logo, content=data["content"], addtime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) db.session.add(art) db.session.commit() flash(u"发布文章成功", "ok") return render_template("art_add.html", title=u"发布文章", form=form)
def add_art(): user_id = session['user_id'] target = os.path.join(APP_ROOT, 'templates/art/') print(target) if not os.path.isdir(target): os.mkdir(target) for art in request.files.getlist("new_art"): print(art) artname = art.filename destination = "/".join([target, artname]) print(destination) art.save(destination) all_children = Child.query.filter_by(parent_id=user_id).all() child_name = request.form['child_name'] for child in all_children: if child.name == child_name: child_id = child.id new_art = Art(title=request.form['title'], art=artname, creation_date=request.form['art_date'], description=request.form['art_description'], child_id=child_id) db.session.add(new_art) db.session.commit() return redirect(url_for('user', user_id=user_id))
def art_add(): form = PublishArtForm() if form.validate_on_submit(): data = form.data print(form.logo.data.filename) file = secure_filename(form.logo.data.filename) print("file == ", file) logo = change_name(file) print("logo == ", logo) if not os.path.exists(app.config["UP"]): os.makedirs(app.config["UP"]) form.logo.data.save(app.config["UP"] + "/" + logo) user = User.query.filter_by(name=session["user"]).first() user_id = user.id # 保存数据 art = Art(title=data["title"], cate=data["category"], user_id=user_id, logo=logo, content=data["content"], addtime=datetime.now().strftime("%Y-%m-%d %H:%M:%S")) db.session.add(art) db.session.commit() return render_template("art_add.html", title="发布文章", form=form) # 渲染模板
def post(self): title = self.request.get('title') art = self.request.get('art') if title and art: a = Art( title=title, art=art ) # lookup coordinates from import # if they have coordinates add them to the mapping # to get the ip self.request.remote_addr geopt = self.get_coords('4.2.2.2') if geopt: a.coords = geopt a.put() self.top_arts(True) self.redirect('/unit3') else: error = "we need both title and art" self.render_ascii(title, art, error )
def post(self): title = self.request.get("title") art = self.request.get("art") if title and art: p = Art(parent=art_key, title = title, art = art) #lookup the user's coordinates from their IP coords = get_coords(self.request.remote_addr) #if we have coordinates, add them to the art if coords: p.coords = coords p.put() self.redirect("/ascii") else: error = "we need both a title and some artwork!" self.render_front(error = error, title = title, art =art)
def top_arts(update = False): key = 'top' arts = memcache.get(key) # logging.error("run top") if arts is None or update: logging.error("db query") arts = Art.query(ancestor = art_key).order(-Art.created).fetch() # arts = list(arts) memcache.set(key, arts) return arts
def get(self): query = self.request.get("q") arts = list(Art.all()) if query: f = lambda art: query in art.title arts = filter(f, arts) self.render("results.html", arts=arts) else: error = "You can't search for nothing!" self.render("art.html", error=error)
def top_arts(update=False): key = "top" arts = memcache.get(key) if arts is None or update: logging.error("DB QUERY") arts = Art.top(10) # prevent the running of multiple queries arts = list(arts) memcache.set(key, arts) return arts
def post(self): title = self.request.get("title") art = self.request.get("art") if title and art: a = Art.new(title, art) coords = get_coords(self.request.remote_addr) if coords: a.coords = coords a.put() # rerun the query and update the cache top_arts(True) self.redirect("/asciichan") else: error = "we need both a title and some artwork!" self.render_front(title, art, error)
def get(self, category): #arts = Art.gql("WHERE category = :category", category=category) arts = Art.all() # Returns a query object arts.filter("category = ", category).order("-created") self.render('category.html', arts=arts, category=category)
def get(self): art_query = Art.all().order("-created") arts = [art for art in art_query.run(limit=10)] self.render("art.html", arts=arts)