def new_thread(request): """ create a new thead """ if request.method == 'POST': thread = Thread( creator=request.user, site=Site.objects.get_current(), ) post = Post(thread=thread, creator=request.user, posted_from=get_client_ip(request)) thread_form = forms.ThreadForm(request.POST, instance=thread) post_form = forms.PostForm(request.POST, instance=post) if thread_form.is_valid() and post_form.is_valid(): thread = thread_form.save() post.thread = thread post_form = forms.PostForm(request.POST, instance=post) post_form.save() request.posting_users.add_to_set(request.user.id) return HttpResponseRedirect(reverse('list-threads')) else: thread_form = forms.ThreadForm() post_form = forms.PostForm() return render_to_response("board/new_thread.html", { 'thread_form': thread_form, 'post_form': post_form, }, context_instance=RequestContext(request))
def view_law(law_id): # ajax request handling form_init_js = g.sijax.register_upload_callback('post_form', submit_post) if g.sijax.is_sijax_request: g.sijax.register_callback('load_more_posts', load_more_posts) g.sijax.register_callback('load_comments', load_comments) g.sijax.register_callback('vote_post', vote_post) g.sijax.register_callback('update_notifications', update_notifications) g.sijax.register_callback('set_all_notifications_seen', set_all_notifications_seen) g.sijax.register_callback('toggle_subscription', toggle_subscription) g.sijax.register_callback('set_law_active', set_law_active) g.sijax.register_callback('set_law_premature', set_law_premature) g.sijax.register_callback('set_law_impossible', set_law_impossible) return g.sijax.process_request() # non-ajax handling: law = Law.query.filter_by(id=law_id).first() if not law: abort(404) options = { 'title': 'Law', 'law': law, 'posts': Post.get_more(group='topic', name=law.topic.name), 'topics_all': Topic.query.all(), 'submit_post_form': forms.PostForm(), 'statuses': LawStatus.query.all(), 'form_init_js': form_init_js } options.update(base_options()) return render_template("law.html", **options)
def view_post(post_id): # ajax request handling form_init_js = g.sijax.register_upload_callback('post_form', submit_post) if g.sijax.is_sijax_request: g.sijax.register_callback('load_comments', load_comments) g.sijax.register_callback('vote_post', vote_post) g.sijax.register_callback('update_notifications', update_notifications) g.sijax.register_callback('set_all_notifications_seen', set_all_notifications_seen) g.sijax.register_callback('toggle_subscription', toggle_subscription) return g.sijax.process_request() # non-ajax handling: main_post = Post.query.filter_by(id=post_id).first() if not main_post: abort(404) # oldest parent: old_parent = None if main_post.parent: old_parent = main_post.parent while old_parent.parent: old_parent = old_parent.parent options = { 'title': 'Post', 'main_post': main_post, 'old_parent': old_parent, 'children': main_post.get_children(), 'some_topics': Topic.query[:10], 'topics_all': Topic.query.all(), 'more_topics_number': Topic.query.count(), 'submit_post_form': forms.PostForm(), 'form_init_js': form_init_js } options.update(base_options()) return render_template("post.html", **options)
def edit_post(post_id): # ajax request handling if g.sijax.is_sijax_request: g.sijax.register_callback('update_notifications', update_notifications) g.sijax.register_callback('set_all_notifications_seen', set_all_notifications_seen) return g.sijax.process_request() # non-ajax handling: options = {'title': 'Edit post', 'form_type': 'post'} form = forms.PostForm() post = Post.query.filter_by(id=post_id).first() if not post: flash('You are trying to edit a post that does not exists', category='danger') elif post.poster != current_user: flash('You are trying to modify a post that is not yours', category='danger') elif form.validate_on_submit(): post.edit(form.content.data) flash('Your post has been successfully modified', category='success') else: form.content.data = post.content options['form'] = form options.update(base_options()) return render_template("edit.html", **options)
def home(): # not authenticated user handling: if not current_user.is_authenticated: options = { 'title': 'Welcome', } options.update(base_options()) return render_template("index.html", **options) # ajax request handling form_init_js = g.sijax.register_upload_callback('post_form', submit_post) if g.sijax.is_sijax_request: g.sijax.register_callback('load_more_posts', load_more_posts) g.sijax.register_callback('load_comments', load_comments) g.sijax.register_callback('vote_post', vote_post) g.sijax.register_callback('update_notifications', update_notifications) g.sijax.register_callback('set_all_notifications_seen', set_all_notifications_seen) g.sijax.register_callback('toggle_subscription', toggle_subscription) return g.sijax.process_request() # non-ajax handling: posts = Post.get_more() options = { 'title': 'Home', 'posts': posts, 'some_topics': Topic.query[:10], 'topics_all': Topic.query.all(), 'more_topics_number': Topic.query.count(), 'submit_post_form': forms.PostForm(), 'form_init_js': form_init_js } options.update(base_options()) return render_template("home.html", **options)
def topic(topic_name): # ajax request handling form_init_js = g.sijax.register_upload_callback('post_form', submit_post) if g.sijax.is_sijax_request: g.sijax.register_callback('load_more_posts', load_more_posts) g.sijax.register_callback('load_comments', load_comments) g.sijax.register_callback('vote_post', vote_post) g.sijax.register_callback('update_notifications', update_notifications) g.sijax.register_callback('set_all_notifications_seen', set_all_notifications_seen) g.sijax.register_callback('toggle_subscription', toggle_subscription) return g.sijax.process_request() # non-ajax handling: current_topic = Topic.query.filter_by(name=topic_name).first() if not current_topic: abort(404) posts = Post.get_more(group='topic', name=topic_name) options = { 'title': '#' + current_topic.name, 'current_topic': current_topic, 'posts': posts, 'some_topics': Topic.query[:10], 'topics_all': Topic.query.all(), 'more_topics_number': Topic.query.count(), 'submit_post_form': forms.PostForm(), 'form_init_js': form_init_js } options.update(base_options()) return render_template("topic.html", **options)
def edit_post(post_id): """Allows user to edit an previous submitted blog post""" try: post = models.Post.get_by_id(post_id) form = forms.PostForm() if form.validate_on_submit(): post.title = form.title.data post.date = form.date.data post.time_spent = form.time_spent.data post.what_i_learned = form.what_i_learned.data post.resources_to_remember = form.resources_to_remember.data post.save() flash('You post has been edited', "success") return redirect(url_for('detail', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.date.data = post.date form.time_spent.data = post.time_spent form.what_i_learned.data = post.what_i_learned form.resources_to_remember.data = post.resources_to_remember return render_template('edit.html', form=form, legend='Edit Post', post=post) except models.DoesNotExist: abort(404)
def login(): form = forms.LoginForm() if form.validate_on_submit(): try: loginUser = data_model.Profile.login_user( email=form.email.data, password=form.password.data) ### Properties for login_user imported from flask ### if loginUser.profileStatus == True: loginUser.is_active = True loginUser.is_authenticated = True loginUser.is_anonymous = False ### end properties for login_user ### login_user(loginUser) # return "You've been logged in!" postForm = forms.PostForm() return render_template('wall.html', form=postForm) except data_model.PasswordIncorrectError: return "Your email or password does not match!" except data_model.UserNotFoundError: return "User does not exist" else: return render_template('login.html', form=form)
def submit_post(obj_response, files, form_values): form = forms.PostForm(**form_values) if form.validate(): parent_id = None if form.parent_id.data: parent_id = form.parent_id.data post = Post.submit(form.content.data, current_user, parent_id) if parent_id: render_comment = get_template_attribute('macros.html', 'render_comment') obj_response.html_prepend( ''.join(['#post-', parent_id, '-comments']), render_comment(post, current_user).unescape()) # update parent comments counter obj_response.script(''.join([ '$("#load_comment_button_', parent_id, '").children(".badge").html(', str(Post.query.filter_by(parent_id=parent_id).count()), ')' ])) else: render_post = get_template_attribute('macros.html', 'render_post') obj_response.html_prepend( '#post-container', render_post(post, current_user).unescape()) obj_response.script("$('#collapsable_post_form').collapse('hide');") form.reset() render_post_form = get_template_attribute('macros.html', 'render_post_form') obj_response.html('#collapsable_post_form', render_post_form(form, current_user).unescape()) # register again the sijax upload plugin obj_response.script( 'sjxUpload.registerForm({"callback": "post_form_upload", "formId": "post_form"});' )
def post(): global vipMember global imageUpload global image form = forms.PostForm() if form.validate_on_submit(): # models.Post.create(user=g.user.id, # content=form.content.data.strip(), # filename='test') if 'file' in request.files: f = request.files['file'] print('static/img/' + current_user.username + '/' + f.filename) image = 'static/img/' + current_user.username + '/' + f.filename models.Post.create(user=g.user.id, content=form.content.data.strip(), filename=f.filename, filepath=image) if not os.path.exists('static/img/' + current_user.username + '/'): os.makedirs('static/img/' + current_user.username + '/') print('post id') f.save(image) imageUpload = 'Y' flash("Message posted! Thanks!", "success") return redirect('/stream/' + str(current_user.username)) else: models.Post.create(user=g.user.id, content=form.content.data.strip()) imageUpload = 'N' flash("Message posted! Thanks!", "success") return redirect('/stream/' + str(current_user.username)) return render_template('post.html', form=form)
def post(): form = forms.PostForm() if form.validate_on_submit(): models.Post.create(user=g.user._get_current_object(), content = form.content.data.strip()) flash("Message posted: Thanks!", "success") return redirect(url_for("index")) return render_template("post.html", form=form)
def post(): form = forms.PostForm() if form.validate_on_submit(): models.Post.create(user=g.user.id, content=form.content.data.strip()) flash("Message posted! Thanks!", "success") return redirect(url_for('index')) return render_template('post.html', form=form)
def barbers(id=None): if request.args.get('neighborhood') == 'choose': neighborhood_code = "ga" else: neighborhood_code = request.args.get('neighborhood') neighborhood = neighborhoods_by_key.get(neighborhood_code) form = forms.PostForm() if id == None: barbers = models.Barber.select().limit(100) return render_template('barbers.html', barbers=barbers, form=form, neighborhood=neighborhood) else: barber_param = int(id) barber = models.Barber.get(models.Barber.id == barber_param) reviews = barber.reviews form = ReviewForm() if form.validate_on_submit(): models.Review.create(barber=barber_param, user_id=g.user._get_current_object(), text=form.text.data.strip(), rating=form.rating.data.strip()) flash("You created a review") return render_template("barber.html", barber=barber, reviews=reviews, form=form, pub_key=pub_key)
def post_new(): form = forms.PostForm() if form.validate_on_submit(): post = models.Post(form.name.data, form.title.data, form.content.data) db.session.add(post) db.session.commit() return redirect(url_for('post.index')) return render_template('post/new.slim', form=form)
def posts_create(): form = forms.PostForm(request.form) if request.method == 'POST' and form.validate(): content = form.content.data post = Posts(content=content) db.session.add(post) db.session.commit() return render_template('posts/show.html', post=post)
def post(): form = forms.PostForm() if form.validate_on_submit(): models.Post.create(user=g.user._get_current_object(), content=form.content.data.strip()) flash("A new post has been created!", "success") return redirect(url_for('index')) return render_template('post.html', form=form)
def edit_clicked_post(post_id): if session['login']: form = forms.PostForm(content=db.post_list_by_post_id(post_id)[0][2]) if form.validate_on_submit(): db.edit_post(post_id=post_id, article=form.content.data.strip()) flash('Your Post has been edited!', 'success') return redirect(url_for('index')) return render_template('post.html', form=form)
def post(): form = forms.PostForm() if form.validate_on_submit(): models.Post.create(user=g.user._get_current_object(), content=form.content.data.strip()) flash('Mensaje Posteado!', 'success') return redirect(url_for('index')) return render_template('post.html', form=form)
def post(): post = forms.PostForm() if post.validate_on_submit(): models.Post.create(user=g.user._get_current_object(), content=post.content.data.strip()) flash("Message posted successfully!", "success") return redirect(url_for('index')) flash("Your post wasn't uploaded.", "error") return render_template('post.html', form=post)
def post(): form = forms.PostForm() if form.validate_on_submit(): Models.Post.create(user=g.user._get_current_object(), content=form.content.data) flash("Message posted - thanks!") return redirect(url_for('index')) return render_template('post.html', form=form)
def post(): form = forms.PostForm() if form.validate_on_submit(): # Access the User table's user and add to the Post table models.Post.create(user=g.user._get_current_object(), content=form.content.data.strip()) flash("Message posted!", "success") return redirect(url_for('index')) return render_template('post.html', form=form)
def post(): if session['login']: form = forms.PostForm() if form.validate_on_submit(): db.add_post(user_id=str(session['user_id']), article=form.content.data.strip()) flash('Your Message has been posted!', 'success') return redirect(url_for('index')) return render_template('post.html', form=form)
def index(): form = forms.PostForm() if form.validate_on_submit(): models.Post.create(user=g.user.id, content=form.content.data.strip()) flash("Message Posted: Thanks!", "success") return redirect(url_for('index')) stream = models.Post.select().order_by(models.Post.timestamp.desc()) user = models.User.select().order_by(models.User.id.desc()) return render_template('posts.html', stream=stream, form=form, user=user)
def post(): form = forms.PostForm() if form.validate_on_submit(): user = current_user.id post = models.Post(user_id=user, content=form.content.data.strip()) flash("Message posted! Thanks!", "success") db_session.add(post) db_session.commit() return redirect(url_for('index')) return render_template('post.html', form=form)
def new_post(): form = forms.PostForm() print(current_user.email) print(form.comment_data.data) if form.validate_on_submit(): print(current_user.email, form.comment_data.data) post_model.Post.create_post(profileID=current_user.email, description=form.comment_data.data) return render_template("wall.html", form=form) return render_template('post.html', form=form)
def post(): form = forms.PostForm() if form.validate_on_submit(): models.Project.create_project(from_user=g.user.id, description=form.content.data.strip(), project_id=np.random.randint(0, 100000), name=form.name.data.strip()) flash("Message Posted: Thanks!", "success") return redirect(url_for("index")) return render_template("post.html", form=form)
def post(): form = forms.PostForm() if form.validate_on_submit(): f = form.image.data filename = secure_filename(f.filename) file_path = os.path.join(app.instance_path, '..', 'static/img', filename) f.save(file_path) file_desc = form.desc.data.strip() post_obj = models.Post.create(user=g.user.id, content=form.content.data.strip(), desc=file_desc, filename=filename) flash("Posted!", "success") # Prepare test path # Ideally requires static files for display # to be moved to a testing folder for model test_directory = os.path.join(TEST_PATH, file_desc) if not os.path.exists(test_directory): os.makedirs(test_directory) test_path = os.path.join(test_directory, filename) # Copy file from static to test folder copy(file_path, test_path) # Predict description of image from model try: prediction = IMAGE_PROCESSOR.test_model() predicted_desc = prediction[file_desc] query = post_obj.update(predicted_desc=predicted_desc).\ where(models.Post.filename == filename) query.execute() except NotFittedError: flash("Model not trained yet. Training in background. " "Please wait and try again.") # Add the newly tested file to training set for future processing. directory = os.path.join(TRAIN_PATH, file_desc) if not os.path.exists(directory): os.makedirs(directory) try: # TODO: Improvement # If file by the same name exists, don't add it again # This could be a bug later, but don't want to break # things with a small set. move(test_path, os.path.join(TRAIN_PATH, file_desc)) except Exception as e: for i in os.listdir(test_directory): os.unlink(os.path.join(test_directory, i)) os.rmdir(test_directory) train_image_processor() return redirect(url_for('index')) return render_template('create_post.html', form=form)
def post(): form = forms.PostForm() if form.validate_on_submit(): models.Post.create(user=g.user.id, title=form.title.data.strip(), timespent=form.timespent.data.strip(), learned=form.learned.data.strip(), resources=form.resources.data.strip()) flash("Message posted! Thanks!", "success") return redirect(url_for('index')) return render_template('new.html', form=form)
def post(self): if not (self.current_user and self.current_user.is_admin): return self.redirect(users.create_login_url(self.request.uri)) form = forms.PostForm(self) if form.validate(): post = Post(title=form.title.data, content=form.content.data, author=self.current_user) post.put() return self.redirect('/') self.render('new_post.html', form=form)
def post_edit(id): post = models.Post.query.get(id) form = forms.PostForm(request.form, post) if form.validate_on_submit(): post.name = form.name.data post.title = form.title.data post.content = form.content.data db.session.add(post) db.session.commit() return redirect(url_for('post.show', id=id)) return render_template('post/edit.slim', form=form, post=post)