def post(self, request): form = NewsForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') desc = form.cleaned_data.get('desc') category_id = form.cleaned_data.get('category_id') thumbnail = form.cleaned_data.get('thumbnail') content = form.cleaned_data.get('content') author = request.user category = CaetgNews.objects.get(pk=category_id) pk = request.POST.get('pk') news = None if pk.isdigit(): pk = int(pk) if News.objects.filter(pk=pk).exists(): news = News.objects.get(pk=pk) news.title = title news.desc = desc news.category = category news.thumbnail = thumbnail news.content = content else: news = News.objects.create(title=title, desc=desc, category=category, thumbnail=thumbnail, content=content, author=author) news.save() url = reverse('cms:news_manage') data = {'url': url} return result.success(data=data) else: error = form.get_errors() return result.auth_pwd_error(data=error)
def admin_edit(request, uid): news = News.get_by_id(uid) if not news: return redirect(url_for('news/admin')) form = NewsForm(initial={ 'title':news.title, 'is_display':news.is_display, 'short_text':news.short_text, 'text':news.text }) if request.method == 'POST': if form.validate(request.form): news.title = form['title'] news.is_display = form['is_display'] news.short_text = form['short_text'] news.text = form['text'] news.put() return redirect(url_for('news/admin')) else: return render_to_response( 'news/admin/edit.html', { 'form': form.as_widget(), 'news': news }) return render_to_response( 'news/admin/edit.html', { 'form': form.as_widget(), 'news': news })
def r_info_edit(request, uid): r_info = RetailInfo.get_by_id(uid) if not r_info: return redirect(url_for('page/admins/r_info/index')) form = NewsForm(initial={ 'title':r_info.title, 'is_display':r_info.is_display, 'short_text':r_info.short_text, 'text':r_info.text }) if request.method == 'POST': if form.validate(request.form): r_info.title = form['title'] r_info.is_display = form['is_display'] r_info.short_text = form['short_text'] r_info.text = form['text'] r_info.put() return redirect(url_for('page/admins/r_info/index')) else: return render_to_response( 'page/admins/r_info/edit.html', { 'form': form.as_widget(), 'r_info': r_info }) return render_to_response( 'page/admins/r_info/edit.html', { 'form': form.as_widget(), 'r_info': r_info })
def news_edit(request, key): news = News.get(key) images_form = NewsImageForm(action='/news/admin/news/add_image/%s/' % key) if news is None: return redirect('/news/admin/news/all') form = NewsForm(instance=news) if request.method == 'POST' and form.validate(request.form): form.save() return redirect('/news/admin/news/all/') return render_to_response('news/admin/news_add.html', {'form':form.as_widget(), 'images_form':images_form.as_widget(), 'news':news})
def add_news_image(request, news_key): news = News.get(news_key) if not news: news = News() news.put() form = NewsForm(instance=news) images_form = NewsImageForm(action='/news/admin/news/add_image/%s/' % news.key()) if request.method == 'POST': if images_form.validate(request.form, request.files): img = images_form['image_file'] content_type = 'image/jpeg' if not images_form['title']: title = '' else: title = images_form['title'] title = title.replace('"', '"') thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=img, thumb_size=(100, 100), content_type=content_type, title=title) if not images_form['width'] or not images_form['height']: width = height = 300 else: width = images_form['width'] height = images_form['height'] thumb_img.add_new_thumb(blob_img=img, thumb_size=(width, height), content_type=content_type, title=title) thumb_img.put() news_image = NewsImage() news_image.title = title news_image.news = news news_image.image = thumb_img news_image.size = '%sx%s' % (width, height) news_image.put() return redirect('/news/admin/news/edit/%s/' % news.key()) return render_to_response('news/admin/news_add.html', {'form':form.as_widget(), 'images_form':images_form.as_widget()})
def post(self, request): forms = NewsForm(request.POST) if forms.is_valid(): cleaned_data = forms.cleaned_data title = cleaned_data.get('title') desc = cleaned_data.get('desc') thumbnail = cleaned_data.get('thumbnail') content = cleaned_data.get('content') author = request.user category_id = cleaned_data.get('category') category = NewsCategory.objects.get(id=category_id) try: News.objects.create(title=title, desc=desc, thumbnail=thumbnail, content=content, category=category, author=author) return restful.ok() except: return restful.params_error("服务器gg") error = forms.get_first_message() return restful.params_error(error)
def r_info_add(request): r_info = RetailInfo() r_info.put() form = NewsForm(action=url_for('page/admins/r_info/edit', uid=r_info.key.id())) return render_to_response('page/admins/r_info/edit.html', {'form': form.as_widget(), 'r_info': r_info})
def admin_add(request): news = News() news.put() form = NewsForm(action=url_for('news/admin/edit', uid=news.key.id())) return render_to_response('news/admin/edit.html', {'form': form.as_widget(), 'news': news})