def tweet(request): if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): twit = Tweet() status = twit.tweet = form.cleaned_data['tweet'] twit.name = request.user.username twit.save() variables = RequestContext(request,{ 'status':status }) return render_to_response('tweet.html',RequestContext(request),variables) else: return render_to_response('tweet.html',{'request':RequestContext(request)}) else: tweets = [] queryset=Tweet.objects.all() for query in queryset: if request.user.username == query.name: tweets.append(query) status = '' for tweet in tweets: status = tweet.tweet form = TweetForm() form.name = request.user variables = RequestContext(request, { 'form': form,'status': status }) return render_to_response('tweet.html',RequestContext(request),variables)
def submit(request): if request.method == 'GET': form = TweetForm({'owner': request.user}, auto_id=True) else: form = TweetForm(request.POST.copy(), auto_id=True) if form.is_valid(): if form.is_shameful(): form.instance.blocked = True else: api = twitter.Api(consumer_key=settings.TW_C_KEY, consumer_secret=settings.TW_C_SECRET, access_token_key=settings.TW_AT_KEY, access_token_secret=settings.TW_AT_SECRET) api.PostUpdate(form.instance.content) form.instance.published = True form.instance.pub_date = datetime.now() form.instance.save() return HttpResponseRedirect(reverse('home') + '?shame=' + str(form.is_shameful()).lower()) tpl_data = { 'form': form } return render_to_response('tweet_brander/submit.html', tpl_data, context_instance=RequestContext(request))
def tweets(search=None): form = TweetForm(request.form) # The use either posted a tweet or interacted with a tweet. if request.method == 'POST': if dispatch_tweet_actions(request.form) == NO_DISPATCH: if form.validate(): try: post_tweet(form.post.data) except (DatabaseException, NotLoggedInException) as error: flash(str(error), 'danger') if search: tweet_posts = search_for_tweets(search) # Only notify first time we're getting the page. if request.method == 'GET': if len(tweet_posts) == 0: flash('No tweets found!', 'danger') else: flash('{number} tweets found!'.format(number=len(tweet_posts)), 'success') else: tweet_posts = get_newest_tweets(18) if session.get('logged_in'): user = User(*session['user']) followers = get_user_followers(user.user_id) return render_template('tweets.html', form=form, tweets=chunks(tweet_posts, 3), followers=followers) else: return render_template('tweets.html', form=form, tweets=chunks(tweet_posts, 3), followers=[])
def add_tweet(): form = TweetForm(request.form) if request.method == "POST" and form.validate(): new_tweet = Message(session['uid'], form.text.data) db.session.add(new_tweet) db.session.commit() return redirect(url_for('home')) else: return redirect(url_for('home'))
def post_tweet(): form = TweetForm() if form.validate(): tweet = Tweet(user_id=current_user.id, text=form.text.data, date_created=datetime.now()) db.session.add(tweet) db.session.commit() return redirect(url_for('timeline')) return 'Something went wrong.'
def tweet(): tweet_form = TweetForm() if tweet_form.validate_on_submit(): with models.database.transaction(): models.Tweet.create(user=current_user._get_current_object(), content=tweet_form.content.data.strip()) flash('You just tweeted. :)', 'success') return redirect(url_for('index')) return render_template('tweet.html', tweet_form=tweet_form)
def post_tweet(): form = TweetForm() if form.validate(): tweet = Tweet(user_id=current_user.id, text=form.text.data, date_created=datetime.now()) db.session.add(tweet) db.session.commit() return redirect(url_for('timeline')) return 'Что-то пошло не так' #todo добавь позже нормальную страницу с ошибкой
def crea_tweet(request): if (request.method == 'GET'): context_form = TweetForm() return render(request, "crea.html", {"form": context_form.as_p()}) if (request.method == 'POST'): tweet = TweetForm(request.POST) tweet.is_valid() data = tweet.cleaned_data db_istance = Tweet(text=data['text'], title=data['title'], user=request.user) db_istance.save() return redirect("home", utente=request.user)
def post(self, request, *args, **kwargs): tf = TweetForm(data=request.POST) if tf.is_valid(): tf_obj = tf.save(commit=False) tf_obj.tweeted_by = request.user tf_obj = parse_category_and_tweet(tf_obj) tf_obj.save() return HttpResponseRedirect("/") else: return render_to_response("tweet.html", {"tweet_form": tf}, context_instance=RequestContext(request))
def user(user_id): user = User.query.get_or_404(user_id) tweet_form = TweetForm() if tweet_form.validate_on_submit() and g.logged_user: tweet = Tweet(g.logged_user, tweet_form.content.data) db.session.add(tweet) db.session.commit() tweet_form.content.data = '' # TODO: keksi siistimpi keino flash(u'Uusi töötti lisätty!') return render_template('user.html', user=user, tweet_form=tweet_form)
def post_tweet(): form = TweetForm() # if the method is only post then use only validate because it is obvious if form.validate(): tweet = Tweet(user_id=current_user.id, text=form.text.data, date_created=datetime.now()) db.session.add(tweet) db.session.commit() return redirect(url_for('timeline')) return 'Something went wrong'
def post_tweet(): form = TweetForm() if form.validate(): #inseram in baza de date tweet = Tweet(user_id=current_user.id, text=form.text.data, date_created=datetime.now()) db.session.add(tweet) db.session.commit() # ma intorc pe aceasi pagina return redirect(url_for('timeline')) return 'Something went wrong'
def post(self, request, username): form = TweetForm(self.request.POST) if form.is_valid(): user = User.objects.get(username = username) tweet = Tweet(text = form.cleaned_data['text'], user = user, country = "Vietname" )#form.cleaned_data['country']) tweet.save() words = form.cleaned_data['text'].split(" ") for word in words: hashtag, created = HashTag.objects.get_or_create(name = word[1:]) hashtag.tweet.add(tweet) return HttpResponseRedirect('/tweets/user/%s' % username) return redirect(Profile.as_view())
def homepage(request): """Renders homepage, with public timeline""" if request.method == 'POST': # If the form has been submitted... form = TweetForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data t=Tweet(text=form.cleaned_data['text'], owner=request.user) t.save() return HttpResponseRedirect('/') # Redirect after POST else: form = TweetForm() # An unbound form return render_to_response(request, 'homepage.html', { 'form': form, })
def post(self, request, username): form = TweetForm(self.request.POST) if form.is_valid(): user = User.objects.get(username=username) tweet = Tweet(text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country']) tweet.save() words = form.cleaned_data['text'].split(" ") for word in words: if word[0] == "#": hashtag, created = HashTag.objects.get_or_create(name=word[1:]) hashtag.tweet.add(tweet) return HttpResponseRedirect('/user/'+username)
def index(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): uid = request.session['uid'] Tweet.create_post(text=form.cleaned_data["text"], uid=uid) return HttpResponseRedirect(reverse("twitter_home")) else: form = TweetForm() context = { 'username': request.session['username'], 'tweet_form': form, 'timeline': Tweet.get_timeline(request.session['uid']), } return render(request, "twitter/index.html", context)
def show_tweets(): if "user_id" not in session: flash("Please login first!", "danger") return redirect('/') form = TweetForm() all_tweets = Tweet.query.all() if form.validate_on_submit(): text = form.text.data new_tweet = Tweet(text=text, user_id=session['user_id']) db.session.add(new_tweet) db.session.commit() flash('Tweet Created!', 'success') return redirect('/tweets') return render_template("tweets.html", form=form, tweets=all_tweets)
def tweet_submit(request): if request.method == "POST": tweets = Tweet.objects.filter(author=request.user.followed_by.all).order_by('-id')[:10] tweet_form = TweetForm(data=request.POST) if tweet_form.is_valid(): tweet = tweet_form.save(commit=False) tweet.author = request.user tweet.save() return redirect('/') else: return render(request, 'public.html', {'tweet_form': tweet_form, 'tweets': tweets}) return redirect('/')
def timeline(username): form = TweetForm() if username: user = User.query.filter_by(username=username).first() if not user: abort(404) tweets = Tweet.query.filter_by(user=user).order_by( Tweet.date_created.desc()).all() total_tweets = len(tweets) else: user = current_user tweets = Tweet.query.join( followers, (followers.c.followee_id == Tweet.user_id)).filter( followers.c.follower_id == current_user.id).order_by( Tweet.date_created.desc()).all() total_tweets = Tweet.query.filter_by(user=user).order_by( Tweet.date_created.desc()).count() current_time = datetime.now() who_to_watch = list_who_to_watch(current_user) return render_template('timeline.html', form=form, tweets=tweets, current_time=current_time, user=user, total_tweets=total_tweets, who_to_watch=who_to_watch, current_user=current_user)
def profile(username): form = UpdatePic() form_2 = TweetForm() if username: user = User.query.filter_by(username=username).first() if not user: flash('User does not exist') abort(404) else: user = current_user tweets = Tweet.query.filter_by(user=user).order_by(Tweet.date_created.desc()).all() display_follow = True change_pic = False if current_user == user: display_follow = False change_pic = True elif current_user in followed_by: display_follow = False change_pic = False whot_to_watch = who_to_watch_list(user) return render_template('profile.html', current_user=user, tweets=tweets, current_time= get_current_time(), followed_by=user.followed_by.all(), display_follow=display_follow, who_to_watch=whot_to_watch, logged_in_user=current_user, form=form, form_2=form_2, change_pic=change_pic)
def post_tweet(request): c = {} if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): cd = form.cleaned_data first_name = cd['first_name'] last_name = cd['last_name'] tweet = cd['tweet'] current_authors = Author.objects.filter( first_name=first_name).filter(last_name=last_name) if (len(current_authors) == 1): author = current_authors[0] else: author = Author.create(first_name=first_name, last_name=last_name) author.save() tweet = Tweet.create(tweet=tweet, author=author) tweet.save() form = TweetForm() else: form = TweetForm() c['form'] = form tweets = Tweet.objects.all() c['tweets'] = tweets return render_to_response('tweets.html', c)
def get_form(self): if self.request.user.is_authenticated(): form = TweetForm(auto_id=False, **self.get_form_kwargs()) form.fields['author'].queryset = get_user_model().objects.filter( Q(pk=self.request.user.id) | Q(serving__master=self.request.user)).exclude( social_auth__iexact=None) return form return None
def show_tweets(): """Show all tweets""" if "user_id" not in session: flash('Please log in first.', 'danger') return redirect('/login') form = TweetForm() all_tweets = Tweet.query.all() if form.validate_on_submit(): text = form.text.data new_tweet = Tweet(text=text, user_id=session['user_id']) db.session.add(new_tweet) db.session.commit() flash('Successfully created tweet!', 'success') return redirect('/tweets') else: return render_template('tweets.html', form=form, tweets=all_tweets)
def tweet(): if "user_id" not in session: return redirect("/") form = TweetForm() if form.validate_on_submit(): tweet = form.tweet.data new_tweet = Tweet(tweet=tweet, user_id=session["user_id"]) db.session.add(new_tweet) db.session.commit() flash("Tweet created", "success") return redirect("/") return render_template("tweets.html", form=form)
def index(): form = LoginForm() form_2 = TweetForm() page = request.args.get('page', 1, type=int) all_tweets = Tweet.query.order_by(Tweet.date_created.desc()).paginate(page=page, per_page=10) current_time = get_current_time() return render_template('index.html', form=form, form_2=form_2, logged_in_user=current_user, all_tweets=all_tweets, current_time=current_time)
def show_tweets(): if "user_id" not in session: flash('Please login first.', 'danger') return redirect('/') form = TweetForm() all_tweets = Tweet.query.all() if form.validate_on_submit(): text = form.text.data user = User.query.get(session['user_id']) new_tweet = Tweet(text=text, user_id=user.id) db.session.add(new_tweet) db.session.commit() flash(f'{user.username} Successfully Created a Tweet.', 'success') return redirect('/tweets') return render_template('tweets.html', form=form, tweets=all_tweets)
def followers(username): form_2 = TweetForm() if username: user = User.query.filter_by(username=username).first() if not user: abort(404) else: user = current_user all_followers = user.followed_by return render_template('followers.html', followers=all_followers, form_2=form_2)
def tweet(request): if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): twit = Tweet() status = twit.tweet = form.cleaned_data['tweet'] twit.name = request.user.username twit.save() variables = RequestContext(request, {'status': status}) return render_to_response('tweet.html', RequestContext(request), variables) else: return render_to_response('tweet.html', {'request': RequestContext(request)}) else: tweets = [] queryset = Tweet.objects.all() for query in queryset: if request.user.username == query.name: tweets.append(query) status = '' for tweet in tweets: status = tweet.tweet form = TweetForm() form.name = request.user variables = RequestContext(request, {'form': form, 'status': status}) return render_to_response('tweet.html', RequestContext(request), variables)
def index(): form = TweetForm(request.form) #tweetmaster = Tweet(key_name='tm') #t = Tweet(parent=tweetmaster) if request.method=='POST' and form.validate(): tweet_id = "post:" + str(getTweetId()) content = request.form['tweet'] date = datetime.datetime.now() tweet = {'content': content, 'date': str(date)} redis_server.hmset(tweet_id, tweet) redis_server.save() return redirect(url_for('index')) retrieve = [] listOfPostKeys = redis_server.keys('post:*') for keys in listOfPostKeys: contentOfTweet=redis_server.hgetall(keys) retrieve.append(contentOfTweet) photo_url = {} display = [] return render_template("index.html", form=form, retrieve=retrieve)
def show_tweets(): # if not session['user_id']: if 'user_id' not in session: flash("Please sign in first to see tweets", "danger") return redirect('/') form = TweetForm() all_tweets = Tweet.query.all() if form.validate_on_submit(): text = form.text.data new_tweet = Tweet(text=text, user_id=session['user_id']) db.session.add(new_tweet) db.session.commit() flash(f"{new_tweet.user.username} you just created a new tweet!", "success") return redirect("/tweets") return render_template('tweets.html', form=form, tweets=all_tweets)
def show_tweets(): """""" if "user_id" not in session: flash("Please login first!", "danger") return redirect('/') # The code below will only run if there is a logged in user. # We could put this form instance above the if stmt, and it wouldn't affect what the user sees, but there's no sense in creating an instance of the form if we are just going to redirect anyway. form = TweetForm() all_tweets = Tweet.query.all() if form.validate_on_submit(): text = form.text.data new_tweet = Tweet(text=text, user_id=session["user_id"]) db.session.add(new_tweet) db.session.commit() flash("Tweet Created!", "success") return redirect('/tweets') return render_template("tweets.html", form=form, tweets=all_tweets)
def post_tweet(request): c = {} if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): cd = form.cleaned_data first_name = cd['first_name'] last_name = cd['last_name'] tweet = cd['tweet'] current_authors = Author.objects.filter(first_name=first_name).filter(last_name=last_name) if (len(current_authors) == 1): author = current_authors[0] else: author = Author.create(first=first_name, last=last_name) author.save() tweet = Tweet.create(tweet=tweet, author=author) tweet.save() form = TweetForm() else: form = TweetForm() c['form'] = form tweets = Tweet.objects.all() c['tweets'] = tweets return render_to_response('twitter.html', c)
def add_tweet(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): tweet = form.save(commit=False) tweet.posted_by = request.user tweet.save() url = reverse("tweets-user_tweets", args=[request.user]) return redirect(url) else: form = TweetForm() template_data = { "form": form, } template = "tweets/add_tweet.html" ctx = RequestContext(request) return render_to_response( template, template_data, context_instance=ctx )
def index(username=None): login_form = LoginForm() if username or current_user.is_authenticated: tweet_form = TweetForm() if username: user = models.User.get(models.User.username**username) else: user = current_user._get_current_object() return render_template('dashboard.html', user=user, login_form=login_form, tweet_form=tweet_form) signup_form = SignupForm() return render_template('index.html', signup_form=signup_form, login_form=login_form)
def timeline(username): form = TweetForm() if username: #промотр чужой страницы user = User.query.filter_by(username=username).first() if not user: abort(404) # собственные посты пользователя на его странице tweets = Tweet.query.filter_by(user=user).order_by( Tweet.date_created.desc()).all() total_tweets = len(tweets) else: #просмотр своей страница user = current_user #посты всех пользователей, на которые подписан текущий пользователь tweets = Tweet.query.join( followers, (followers.c.followee_id == Tweet.user_id)).filter( followers.c.follower_id == current_user.id).order_by( Tweet.date_created.desc()).all() # количество собественных постов текущего пользователя total_tweets = Tweet.query.filter_by(user=user).order_by( Tweet.date_created.desc()).count() current_time = datetime.now() followed_by_count = user.followed_by.count() who_to_watch = who_to_watch_list(user) return render_template('timeline.html', form=form, tweets=tweets, current_time=current_time, current_user=user, total_tweets=total_tweets, who_to_watch=who_to_watch, logged_in_user=current_user, followed_by_count=followed_by_count)
def timeline(username): form = TweetForm() if username: user = User.query.filter_by(username=username).first() if not user: abort(404) tweets = Tweet.query.filter_by(user=user).order_by(Tweet.date_created.desc()).all() total_tweets = len(tweets) else: user = current_user tweets = Tweet.query.join(followers, (followers.c.followee_id == Tweet.user_id)).\ filter(followers.c.follower_id == current_user.id).order_by(Tweet.date_created.desc()).all() total_tweets = Tweet.query.filter_by(user=user).order_by(Tweet.date_created.desc()).count() current_time = datetime.now() who_to_watch = User.query.filter(User.id != user.id).order_by(db.func.random()).limit(4).all() followed_by_count = user.followed_by.count() return render_template('timeline.html', form=form, tweets=tweets, current_time=current_time, current_user=user, total_tweets=total_tweets, who_to_watch=who_to_watch, logged_in_user=current_user, followed_by_count=followed_by_count)
def timeline(username): form = TweetForm() if username: user = User.query.filter_by(username=username).first() if not user: abort(404) else: user = current_user tweets = Tweet.query.filter_by(user=user).order_by( Tweet.date_created.desc()).all() total_tweets = len(tweets) current_time = datetime.now() return render_template('timeline.html', form=form, tweets=tweets, current_time=current_time, current_user=user, total_tweets=total_tweets)
def add_tweet(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): tweet = form.save(commit=False) tweet.posted_by = request.user tweet.save() url = reverse("tweets-user_tweets", args=[request.user]) return redirect(url) else: form = TweetForm() template_data = { "form": form, } template = "tweets/add_tweet.html" ctx = RequestContext(request) return render_to_response(template, template_data, context_instance=ctx)