def add_post(request): if request.method == 'GET': if 'user' in request.session: return render_to_response('add_post.html', RequestContext(request)) else: return redirect(request, 'You need to login to make a post', '') elif request.method == 'POST': d = request.POST title = d['title'] content = d['content'] date = datetime.datetime.now() viewable = d['viewable'] author = None for user in User.objects(username=request.session['user']): author = user form = UploadImgForm(request.POST, request.FILES) if form.is_valid(): newImg = Img(img_width=50, img_height=50) newImg.img_src.put(request.FILES['img'], content_type='image/jpeg') newImg.save() newPost = Post(title=title, content=content, date_added=date, image_id=newImg, author=author, viewable=viewable) newPost.save() return redirect(request, 'Added post successfully', '') return redirect(request, 'All inputs need to be filled', 'add_post')
def delete_post(): try: post_id = request.args.get('post_id',type=int) except: flash('delete post failed') Post.delete(id = post_id) flash('delete post success') return redirect(url_for('main.index'))
def newpost(request): now = datetime.datetime.now() if request.method == 'POST': # If the form has been submitted... form = PostForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data try: form_name = User.objects.get(username = request.user) except User.DoesNotExist: return HttpResponseRedirect('/login/') form_title = form.cleaned_data['title'] form_article = form.cleaned_data['article'] p = Post(uname = form_name, title = form_title, article = form_article, pub_date = now) p.save() return HttpResponseRedirect('/posts/%s/' %(form_name.username)) # Redirect after POST
def chatting_detail(): session['current_path'] = request.path form = PostForm() username = session.get("USERNAME") if not session.get("USERNAME") is None: employee_in_db = Employee.query.filter( Employee.username == session.get("USERNAME")).first() user = {'city': employee_in_db.key} user_id = request.args.get("id") user_in_db = Customer.query.filter(Customer.id == user_id).first() if form.validate_on_submit(): body = form.postbody.data if user_in_db: name = 'Employee_' + str(employee_in_db.id) post = Post(body=body, author=user_in_db, name=name) db.session.add(post) db.session.commit() return redirect(url_for('employee_chatting')) else: return render_template('chatting_detail.html', user=user, username=username, title='Message', customer=user_in_db, form=form, language=language[render_languages()]) else: flash("User needs to either login or signup first") return redirect(url_for('login'))
def customer_chatting(): session['current_path'] = request.path form = PostForm() username = session.get("USERNAME") if not session.get("USERNAME") is None: if form.validate_on_submit(): body = form.postbody.data user_in_db = Customer.query.filter( Customer.username == session.get("USERNAME")).first() post = Post(body=body, author=user_in_db, name=session.get("USERNAME")) db.session.add(post) db.session.commit() return redirect(url_for('customer_chatting')) else: user_in_db = Customer.query.filter( Customer.username == session.get("USERNAME")).first() prev_posts = Post.query.filter(Post.user_id == user_in_db.id).all() # print("Checking for user: {} with id: {}".format(user_in_db.username, user_in_db.id)) return render_template('customer_chatting.html', title='Message', username=username, user_in_db=user_in_db, prev_posts=prev_posts, form=form, language=language[render_languages()]) else: flash("User needs to either login or signup first") return redirect(url_for('login'))
def post(self, post_id=None): if post_id: abort(400) else: args = post_post_parser.parse_args(strict=True) user = User.verify_auth_token(args['token']) if not user: abort(401) new_post = Post(title=args['title'], publish_date=datetime.datetime.now(), text=args['text'], user=user) if args['tags']: for item in args['tags']: tag = Tag.query.filter_by(title=item).first() # Add the tag if it exists. # If not, make a new tag if tag: new_post.tags.append(tag) else: new_tag = Tag(title=item) new_post.tags.append(new_tag) db.session.add(new_post) db.session.commit() return new_post.id, 201
def employee_chatting(): form = PostForm2() if not session.get("USERNAME") is None: employee_in_db = Employee.query.filter( Employee.username == session.get("USERNAME")).first() user = {'city': employee_in_db.key} if form.validate_on_submit(): body = form.postbody.data who = form.who.data user_in_db = Customer.query.filter( Customer.username == who).first() if user_in_db: post = Post(body=body, customer=user_in_db, name=session.get("USERNAME")) db.session.add(post) db.session.commit() return redirect(url_for('employee_chatting')) else: # user_in_db = Customer.query.first() prev_posts = Post.query.all() # print("Checking for user: {} with id: {}".format(user_in_db.username, user_in_db.id)) return render_template('employee_chatting.html', user=user, title='Message', prev_posts=prev_posts, form=form) else: flash("User needs to either login or signup first") return redirect(url_for('login'))
def search(): if not g.search_form.validate(): return redirect(url_for('main.explore')) page = request.args.get('page',1,type=int) posts,total = Post.search(g.search_form.q.data,page,current_app.config['POST_PER_PAGE']) next_url = url_for('main.search',q=g.search_form.q.data,page=page + 1) if total > page * current_app.config['POST_PER_PAGE'] else None prev_url = url_for('main.user',q=g.search_form.q.data,page=page - 1) if page > 1 else None return render_template('search.html',title='Search',posts=posts,next_url=next_url,prev_url=prev_url)
def create(request): post = Post() post.title = request.POST['title'] post.body = request.POST['body'] post.pic = request.FILES['pic'] post.pub_date = timezone.datetime.now() post.save() return redirect('/post/' + str(post.id))
def addpost(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data,body=form.body.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live') return redirect(url_for('main.index')) return render_template('addpost.html',form=form,title='Addpost')
def newpost(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data,content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Post has been submited', 'success') return redirect(url_for('posts')) return render_template('newpost.html', title='New post', form=form)
def message(): list1 = request.form.get('s') list1 = list1.split(',') customer = Customer.query.filter(Customer.username == list1[1]).first() post = Post(body=list1[0], author=customer, name=session.get("USERNAME")) db.session.add(post) db.session.commit() s = list1[1] return jsonify(s)
def database_operation(request): if 'user' in request.session: if request.session['security_level'] == 10: post1 = Post(title='Public article 2', content='test1', viewable='P').save() #post2 = Post(title='Private1', content='1eggrgwgwrhwrh', viewable='N').save() #post3 = Post(title='Private2', content='5635653768779', viewable='N').save() return redirect(request, 'Data inserted successfully', '') return redirect(request, 'Illegal operation!', '')
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('The post was created successfully', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form)
def setup_posts(): """Add posts.""" user = User.query.get(1) tag_one = Tag(title='Python') tag_two = Tag(title='Flask') tag_three = Tag(title='SQLAlchemy') tag_four = Tag(title='Jinja') tag_list = [tag_one, tag_two, tag_three, tag_four] for i in xrange(1, 101): new_post = Post(title="Post " + str(i)) new_post.user = user new_post.publish_date = datetime.datetime.now() new_post.text = "Example test for post #%s" % i new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
def delete(request): if request.method == 'GET': if 'user' in request.session: d = request.GET id = d['id'] username = d['username'] return render_to_response('delete.html', {'id': id, 'username': username}, RequestContext(request)) return redirect(request, 'Illegal operation!', '') elif request.method == 'POST': if 'user' in request.session: d = request.POST if request.session['user'] == d['username'] or request.session['security_level'] == 10: id = d['id'] if Post.objects(id=id).first().image_id is not None: img_id = Post.objects(id=id).first().image_id.id Img.objects(id=img_id).delete() Post.objects(id=id).delete() return redirect(request, 'deleted post successfully', '') return redirect(request, 'Illegal operation!', '')
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash(f'Your post has been successfully created', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='Create Post')
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash("Your post has been created!", "success") return redirect(url_for("main.home")) return render_template("create_post.html", title="New Post", form=form, legend="New Post")
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Twój post został utworzony!', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form, legend='Nowy post')
def test_follow_posts(self): # create four users u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) # create four posts now = datetime.utcnow() p1 = Post(body="post from john", author=u1, timestamp=now + timedelta(seconds=1)) p2 = Post(body="post from susan", author=u2, timestamp=now + timedelta(seconds=4)) p3 = Post(body="post from mary", author=u3, timestamp=now + timedelta(seconds=3)) p4 = Post(body="post from david", author=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() # setup the followers u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u3) # susan follows mary u3.follow(u4) # mary follows david db.session.commit() # check the followed posts of each user f1 = u1.followed_posts().all() f2 = u2.followed_posts().all() f3 = u3.followed_posts().all() f4 = u4.followed_posts().all() self.assertEqual(f1, [p2, p4, p1]) self.assertEqual(f2, [p2, p3]) self.assertEqual(f3, [p3, p4]) self.assertEqual(f4, [p4])
def delete(request): if request.method == 'GET': if 'user' in request.session: d = request.GET id = d['id'] username = d['username'] return render_to_response('delete.html', { 'id': id, 'username': username }, RequestContext(request)) return redirect(request, 'Illegal operation!', '') elif request.method == 'POST': if 'user' in request.session: d = request.POST if request.session['user'] == d['username'] or request.session[ 'security_level'] == 10: id = d['id'] if Post.objects(id=id).first().image_id is not None: img_id = Post.objects(id=id).first().image_id.id Img.objects(id=img_id).delete() Post.objects(id=id).delete() return redirect(request, 'deleted post successfully', '') return redirect(request, 'Illegal operation!', '')
def index(): form = CreatePostForm() posts = Post.query.all() if form.validate_on_submit(): post = Post( title=form.title.data, body=form.body.data, user_id=current_user.id ) db.session.add(post) db.session.commit() flash('New Post has been created!') return redirect(url_for('index')) return render_template('index.html', title="Home", form=form, posts=posts)
def add_post(request): if request.method == 'GET': if 'user' in request.session: return render_to_response('add_post.html', RequestContext(request)) else: return redirect(request, 'You need to login to make a post', '') elif request.method == 'POST': d = request.POST title = d['title'] content = d['content'] date = datetime.datetime.now() viewable = d['viewable'] author = None for user in User.objects(username=request.session['user']): author = user form = UploadImgForm(request.POST, request.FILES) if form.is_valid(): newImg = Img(img_width = 50, img_height=50) newImg.img_src.put(request.FILES['img'], content_type = 'image/jpeg') newImg.save() newPost = Post(title=title, content=content, date_added=date, image_id=newImg, author=author, viewable=viewable) newPost.save() return redirect(request, 'Added post successfully', '') return redirect(request, 'All inputs need to be filled', 'add_post')
def new_post(): """Make new blog post.""" form = PostForm() if form.validate_on_submit(): new_post = Post( title=form.title.data, text=form.text.data, publish_date=datetime.datetime.now(), ) db.session.add(new_post) db.session.commit() return redirect(url_for('.home')) return render_template('new.html', form=form)
def update(request): id = eval("request." + request.method + "['id']") post = Post.objects(id=id)[0] if request.method == 'POST': template_html = "index.html" post.tilte = request.POST['title'] post.last_update = datetime.datetime.now() post.content = request.POST['content'] post.save() params = {'Posts': Post.objects} else: template_html = 'update.html' params = {'post': post} return render_to_response(template_html, params)
def index(request): if request.method == 'POST': # save new post title = request.POST['title'] content = request.POST['content'] post = Post(title=title) post.content = content post.last_update = datetime.datetime.now() post.save() posts = Post.objects return render_to_response("index.html", {'Posts': posts})
def delete(request): ''' To delete the post which id is from user selection :param request: :return: ''' id = eval("request." + request.method + "['id']") if request.method == 'POST': post = Post.objects(id=id)[0] post.delete() template_html = "index.html" params = {'Posts': Post.objects} else: template_html = "delete.html" params = {'id': id} return render_to_response(template_html, params)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash(_('Your post is now live!')) return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template('index.html', title=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def handle(self, *args, **options): # Get all the filenames in POST_PATH post_filenames = os.listdir(POST_PATH) # Delete all the posts in the DB whose filenames don't exist in the posts directory Post.objects.filter(~Q(filename__in=post_filenames)).delete() # Add/update all posts in the posts directory for filename in post_filenames: post = Post.from_file(filename).save() # Now do static images existing_files = [] for root, dirs, files in os.walk(IMG_PATH): for name in files: image = Image.from_file(os.path.join(root, name)) # this can be None if the file isn't an image file if image is not None: image.save() existing_files.append(image.static_path) # Delete all the images in the DB whose filenames don't exist in the img directory Image.objects.filter(~Q(static_path__in=existing_files)).delete()
def test_string_representation(self): expected = "This is a title" p1 = Post(title=expected) actual = str(p1) self.assertEqual(expected, actual)
def new_post(request, bid): if request.POST: post_title = request.POST.get('post_title') post_body = request.POST.get('post_body') tags = request.POST.get('tag') post = Post() post.title = post_title post.body = post_body post.ctime = datetime.now().strftime("%Y-%m-%d %H:%M") post.mtime = datetime.now().strftime("%Y-%m-%d %H:%M") post.author = request.user blog = Blog.objects.get(id = bid) post.blog = blog post.save() if tags is not None: print tags tags_list = tags.split(',') for tag_name in tags_list: tag_name = tag_name.strip() if len(Tag.objects.filter(tag_name = tag_name)) <= 0: tag = Tag() tag.tag_name = tag_name tag.save() post.tags.add(tag) post.save() else: tag = Tag.objects.get(tag_name = tag_name) post.tags.add(tag) post.save() return HttpResponseRedirect(reverse('blog', args=[bid]))
def mutate(root, info, input=None): post_instance = Post(title=input.title, description=input.description, author=input.author) post_instance.save() return CreatePost(post=post_instance)
def new_post(request, bid): if request.POST: post_title = request.POST.get('post_title') post_body = request.POST.get('post_body') tags = request.POST.get('tag') post = Post() post.title = post_title post.body = post_body post.ctime = datetime.now().strftime("%Y-%m-%d %H:%M") post.mtime = datetime.now().strftime("%Y-%m-%d %H:%M") post.author = request.user blog = Blog.objects.get(id=bid) post.blog = blog post.save() if tags is not None: print tags tags_list = tags.split(',') for tag_name in tags_list: tag_name = tag_name.strip() if len(Tag.objects.filter(tag_name=tag_name)) <= 0: tag = Tag() tag.tag_name = tag_name tag.save() post.tags.add(tag) post.save() else: tag = Tag.objects.get(tag_name=tag_name) post.tags.add(tag) post.save() return HttpResponseRedirect(reverse('blog', args=[bid]))