def create_message(request): if request.method == 'POST': if request.is_ajax(): new_message_form = MessageForm(request.POST) if new_message_form.is_valid(): new_message = new_message_form.save(commit=False) new_message.author = UserProfile.objects.get( pk=request.user.userprofile.pk) new_message.chat_room = ChatRoom.objects.get( pk=request.POST['chat_id']) if request.user.userprofile not in new_message.chat_room.users.all( ): return redirect("/") new_message.save() return JsonResponse(request.POST) return redirect("/")
def home(): error = None form = MessageForm(request.form) if form.validate_on_submit(): new_message = BlogPost(form.title.data, form.description.data, current_user.id) db.session.add(new_message) db.session.commit() flash("New entry was successfully posted. Thanks.") return redirect(url_for('home.home')) else: posts = db.session.query(BlogPost).all() return render_template('index.html', posts=posts, form=form, error=error)
def messages_add(): if not g.user: flash("Access unauthorized.", "danger") return redirect("/") form = MessageForm() if form.validate_on_submit(): msg = Message(text=form.text.data) g.user.messages.append(msg) db.session.commit() flash('New Warble successfully added!', 'success') return redirect(f"/users/{g.user.id}") return render_template('messages/new.html', form=form)
def edit_blogpost(author_id, blog_id): error = None editpost = db.session.query(BlogPost).filter_by(id=blog_id).one() form = MessageForm(obj=editpost) if form.validate_on_submit(): editpost.title = form.title.data editpost.description = form.description.data db.session.add(editpost) db.session.commit() flash("Post successfully edited", "success") return redirect(url_for('blog')) return render_template('edit-post.html', editpost=editpost, form=form, error=error)
def contact(): """Render the website's contact page.""" theForm = MessageForm() if request.method == 'POST': if theForm.validate_on_submit(): name = theForm["name"] email = theForm["email"] subject = theForm["subject"] message = theForm["message"] send(name, email, subject, message) flash('Successful') return redirect(url_for('home')) return render_template('contact.html', form=theForm)
def messages_add(): """Add a message: Show form if GET. If valid, update message and redirect to user page. """ form = MessageForm() if form.validate_on_submit(): msg = Message(text=form.text.data) g.user.messages.append(msg) db.session.commit() return redirect(f"/users/{g.user.id}") return render_template('messages/new.html', form=form)
def priv_chat(id_=None): form = MessageForm() if request.method == 'POST': content = form.data.content send2 = form.data.send2 send2user = User.get_or_404(id=send2) message = Message(create_by=current_user.id, content=content, send2=send2) message.save() src_msgs = Message.objects(create=current_user.id, send2=id_) dst_msgs = Message.objects(send2=current_user.id, create=id_) return render_template('/priv_chat.html', src_msgs=src_msgs, dst_msgs=dst_msgs, form=form)
def guestbook(request): messages = Message.objects.all().order_by('-date') if request.method == 'POST': name = request.POST.get('name', None) message = request.POST.get('message', None) spam = request.POST.get('spam_catcher', None) if spam or 'href' in message or 'http' in message or '<a' in message: return redirect( 'http://www.law.cornell.edu/wex/inbox/state_anti-spam_laws') form = MessageForm(request.POST) if form.is_valid(): if not Message.objects.filter(name=name, message=message): #prevents duplicate entries message = form.save() return render_to_response("guestbook.html", {'messages': messages}, RequestContext(request))
def new_message(request): pass if request.method == "POST": message_form = MessageForm(request.POST, user=request.user) messages = Message.objects.all().order_by('-create_date') if message_form.is_valid() and request.POST.get('commit', ''): post = message_form.save() return HttpResponseRedirect(reverse("guestbook_index")) return render_to_response("guestbook/index.html", { "settings": settings, "message_form": message_form, "messages": messages, }, RequestContext(request)) else: return HttpResponseRedirect(reverse("guestbook_index"))
def send_message(username): form = MessageForm(request.form) if request.method == 'POST' and form.validate(): Message.register( form.encmessage.data, form.subject.data, form.recipient.data, current_user.id ) flash("Wiadomość wysłana!", 'success') return redirect(url_for('index')) user = User.query.filter(User.username == username).first() if not user or user.username == current_user.username: flash("Użytkownik nie istnieje lub jest niepoprawny.", 'danger') return redirect(url_for('index')) return render_template('send_message.html', recipient=user, form=form)
def direct_message(user_id): """Send a direct message: Show form if GET. If valid, update message and redirect to logged in user's page. """ recipient = User.query.get_or_404(user_id) form = MessageForm() if form.validate_on_submit(): dm = DirectMessage(author_id=g.user.id, recipient_id=user_id, text=form.text.data) db.session.add(dm) db.session.commit() return redirect(url_for('users_show', user_id=g.user.id)) return render_template('messages/new.html', recipient=recipient, form=form)
def message(parking_id): form = MessageForm(request.form) user = current_user # Get the information related to this parking spot parking_spot = Parking_Spot.get_parking_spot_by_id(parking_id) if request.method == 'POST': message = Message(user.uid, parking_spot.ownerid, parking_spot.psid, form.message.data) db.session.add(message) db.session.commit() return redirect(url_for('profile')) return render_template('message.html', parking_spot=parking_spot, form=form)
def new_blogpost(author_id): error = None form = MessageForm(request.form) if form.validate_on_submit(): newpost = BlogPost( title=form.title.data, description=form.description.data, author_id=current_user.id) db.session.add(newpost) db.session.commit() flash('your post was successful', 'success') return redirect(url_for('blog')) return render_template( 'create-post.html', form=form, error=error )
def post(self, request, to=''): form = MessageForm(request.POST) if form.is_valid(): message = form.save(commit=False) chain = Chain.objects.create() chain.members.add(*form.cleaned_data['recipient']) chain.members.add(self.user) chain.have_read.add(self.user) chain.save() message.chain = chain message.sender = self.user message.save() return self.to_inbox() return self.get(request)
def messages_show(user_id, id): found_message = Message.query.get(id) if request.method == b"PATCH": form = MessageForm(request.form) if form.validate(): found_message.content = request.form['content'] db.session.add(found_message) db.session.commit() return redirect(url_for('messages_index', user_id=user_id)) return render_template('messages/edit.html', message=found_message, form=form) if request.method == b"DELETE": delete_form = DeleteForm(request.form) if delete_form.validate(): db.session.delete(found_message) db.session.commit() return redirect(url_for('messages_index', user_id=user_id)) return render_template('messages/show.html', message=found_message)
def messages_add(): """Add a message: Show form if GET. If valid, update message and redirect to user page. """ if not g.user: flash("Access unauthorized.", "danger") return redirect("/") form = MessageForm(**(json.loads(request.json['body']))) if form.validate(): msg = Message(text=form.text.data) g.user.messages.append(msg) db.session.commit() serialized = msg.serialize() return jsonify(msg=serialized, user=g.user.username)
def messages_add(): """Add a message - show form if GET. If valid, update message and redirect to user page.""" if not g.user: flash("Access unauthorized.", "danger") return redirect("/") form = MessageForm() if form.validate_on_submit(): msg = Message(text=form.text.data) g.user.messages.append(msg) db.session.commit() return redirect(f"/users/{g.user.id}") return render_template('messages/new.html', form=form)
def post(self): user_id = force_int(self.get_argument('user_id', 0), 0) sender = self.current_user receiver = User.get(id=user_id) if receiver: form = MessageForm(self.request.arguments) if form.validate(): """ message_box1 = current_user.get_message_box(user=user) message_box2 = user.get_message_box(user=current_user) if not message_box1: message_box1 = MessageBox(sender_id=current_user.id, receiver_id=user.id, status=1).save() if not message_box2: message_box2 = MessageBox(sender_id=user.id, receiver_id=current_user.id).save() """ message = form.save(sender_id=sender.id, receiver_id=receiver.id) result = { "status": "success", "message": "私信发送成功", "content": message.content, "created": message.created, "avatar": sender.get_avatar(size=48), "url": sender.url, "id": message.id } else: result = {"status": "error", "message": "请填写至少 4 字的内容"} if self.is_ajax: self.write(result) else: self.flash_message(result) self.redirect_next_url() self.finish() WebSocketHandler.send_message(message.receiver_id, message) return result = {"status": "error", "message": "没有目标用户,不能发送私信哦"} if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect_next_url()
def contact(): """Render the website's contact page.""" contact_form = MessageForm() if request.method == 'POST': if contact_form.validate_on_submit(): # Get validated data from form name = contact_form["name"] email = contact_form["email"] subject = contact_form["subject"] message = contact_form["message"] send_email(name, email, subject, message) flash('Message Sent') return redirect(url_for('home')) return render_template('contact.html', form=contact_form)
def viewPhoto(): photoID = request.args.get('photoid') form = MessageForm() photojson = "static/json_data/" + photoID + ".json" with open(photojson) as json_: photo_data = json.load(json_) if form.validate_on_submit(): contact = form.number.data mediaurl = form.media.data print(contact) print(mediaurl) message = client.messages.create(to=contact, from_="+13143100209", body="Heres your pic!", media_url=[mediaurl]) return redirect('/index') return render_template('view.html', photo=photo_data, form=form)
def channelPage(channel_title): """ Channel Page. """ """ Route takes GET request with title string. Looks for that string channel name and returns it. Else returns channels page with channel not found error. """ """ Get Channel From DataBase if it exists. """ channel = Channel.query.filter_by(title=channel_title).first_or_404() #channel = Channel.query.filter_by(title=channel_title).first() """ If there is no Channel, redirect to Channels list. """ if channel == None: error = "No Channel" return render_template("channels_list.html", error=error) """ Channel Info. """ """ list of Messages from Channel. Send first 20. """ messages = channel.messages if not messages: messages = "No Messages." """ List of Storys from Channel. Send first 20. """ storys = channel.storys if not storys: storys = "No Storys." form = MessageForm() """ Get values from form. """ if form.validate_on_submit(): """ Make sure . """ message = Message(user_id=current_user.id, channel_id=channel.id, message_text=form.message_text.data, user=current_user, channel=channel) db.session.add(message) db.session.commit() return render_template("channel_page.html", channel=channel, messages=messages, storys=storys, form=form) return render_template("channel_page.html", channel=channel, messages=messages, storys=storys, form=form)
def users_show(user_id): """Show user profile.""" form = MessageForm() user = User.query.get_or_404(user_id) # snagging messages in order from the database; # user.messages won't be in order by default messages = ( Message.query # .filter(Message.user_id == user_id) .order_by(Message.timestamp.desc()).limit(100).all()) #getting all likes to be counted for like-count all_likes = Likes.query.filter(Likes.user_id == user.id).all() return render_template('users/show.html', user=user, messages=messages, count=len(all_likes), form=form)
def messages_add(): """Add a message: Show form if GET. If valid, update message and redirect to user page. """ if not g.user: flash("You must be logged in to access this page.", "danger") return redirect("/") form = MessageForm() if form.validate_on_submit(): msg = Message(text=form.text.data) g.user.messages.append(msg) db.session.commit() return redirect("/") return render_template('messages/new.html', form=form)
def homepage(): """Show homepage: - anon users: no messages - logged in: 100 most recent messages of followed_users """ form = MessageForm() if g.user: likes = g.user.likes following = g.user.following f_id = [ff.id for ff in following] f_id.append(g.user.id) messages = (Message.query.filter(Message.user_id.in_(f_id)).order_by( Message.timestamp.desc()).limit(100).all()) return render_template('home.html', messages=messages, likes=likes, form=form) else: return render_template('home-anon.html')
def homepage(): """Show homepage: - anon users: no messages - logged in: 100 most recent messages of followed_users """ if g.user: form = MessageForm() following = [u.id for u in g.user.following] messages = (Message .query .filter((Message.user_id.in_(following)) | (Message.user_id == g.user.id)) .order_by(Message.timestamp.desc()) .limit(100) .all()) likes = [like.id for like in g.user.likes] return render_template('home.html', messages=messages, likes=likes, form=form) else: return render_template('home-anon.html')
def messages_add(): """Add a message: Show form if GET. If valid, update message and redirect to user page. """ if not g.user: flash("Access unauthorized.", "danger") return redirect("/home") form = MessageForm() if form.validate_on_submit(): msg = Message(text=form.text.data, ticker=form.ticker.data.upper(), title=form.title.data) g.user.posts.append(msg) db.session.commit() return redirect("/home") return render_template('messages/new.html', form=form, user=g.user)
def index(): form = MessageForm() image_sources = os.listdir('static/json_data/') image_sources = ['static/json_data/' + x for x in image_sources] image_data = [] for json_file in image_sources: with open(json_file) as json_: data = json.load(json_) print(data['static_link']) image_data.append(data) random.shuffle(image_data) if form.validate_on_submit(): contact = form.number.data blurb = "Welcome to Doggit! Please send pictures to this number for submission. You may optionally include a caption!" print(contact) print("click") message = client.messages.create(to=contact, from_="+13143100209", body=blurb) return redirect('/index') return render_template('index.html', form=form, image_sources=image_data)
def chatting(user_id): global present_user print(present_user) user = User.query.get_or_404(user_id) form = MessageForm() if form.validate_on_submit(): print('hi') msg = Messages(form.message.data, present_user.name, user.name) db.session.add(msg) db.session.commit() db.session.refresh(msg) # form.submit.data = False return render_template('chatting.html', user=user, users=User.query.all(), messages=Messages.query.all(), form=form) print('data : ' + str(form.submit.data)) return render_template('chatting.html', user=user, users=User.query.all(), messages=Messages.query.all(), form=form)
def home(): error = None form = MessageForm(request.form) if form.validate_on_submit(): new_message = BlogPost(form.title.data, form.description.data, current_user.id) db.session.add(new_message) db.session.commit() flash('New entry was successfully posted. Thanks.') return redirect(url_for('home.home')) else: posts = "" try: posts = db.session.query(BlogPost).all() except OperationalError: flash( "You have no database! Please, contact your system administrator to set up the database!" ) return render_template('index.html', posts=posts, form=form, error=error)
def mess_send(*args,**kwargs): form = MessageForm(request.form) if request.method == 'POST' and 'proj_id' in kwargs and form.validate(): mess = Messages(content = form.message.data, kind = 'project', author = g.user, project = Projects.query.get(kwargs['proj_id']), reciever = Projects.query.get(kwargs['proj_id']).user ) db.session.add(mess) db.session.commit() flash('Message send') return redirect('/projects/%s#/2' % kwargs['proj_id']) if request.method == 'POST' and 'author' in kwargs and form.validate(): mess = Messages(content = form.message.data, kind = 'private', author = g.user, reciever = User.query.filter_by(name = kwargs['author']).first() ) db.session.add(mess) db.session.commit() flash('Message send') return redirect('/user/%s#/2' % g.user.name) return redirect('.')