Esempio n. 1
0
class PrivateDialogsController(BaseController):
    @login_required
    @render
    def index(self):
        self.dialogs = [d for d in self.current_user.dialogs if d.messages.count()]

    @login_required
    @render
    def show(self):
        self.dialog = PrivateDialog.query.get_or_404(self.params["dialog_id"])
        self.form = PrivateMessageForm()
        self.form.validate_on_submit()

    @login_required
    def create(self):
        if str(self.current_user.id) == str(self.params["user_id"]):
            return abort(403)
        another_user = User.query.get_or_404(self.params["user_id"])
        dialog = PrivateDialog.query.filter(
            PrivateDialog.users.contains(self.current_user), PrivateDialog.users.contains(another_user)
        ).first() or PrivateDialog.create(users=[self.current_user, another_user])
        return redirect(url_for("privatedialogs.show", dialog_id=dialog.id))

    @login_required
    def create_new_message(self):
        self.dialog = PrivateDialog.query.get_or_404(self.params["dialog_id"])
        self.form = PrivateMessageForm()
        if self.form.validate_on_submit():
            PrivateMessage.create(dialog_id=self.dialog.id, text=self.params["text"], user=self.current_user)
            return redirect(url_for("privatedialogs.show", dialog_id=self.dialog.id))
        return self.show()
Esempio n. 2
0
 def create_new_message(self):
     self.dialog = PrivateDialog.query.get_or_404(self.params["dialog_id"])
     self.form = PrivateMessageForm()
     if self.form.validate_on_submit():
         PrivateMessage.create(dialog_id=self.dialog.id, text=self.params["text"], user=self.current_user)
         return redirect(url_for("privatedialogs.show", dialog_id=self.dialog.id))
     return self.show()
Esempio n. 3
0
 def show(self):
     self.dialog = PrivateDialog.query.get_or_404(self.params["dialog_id"])
     self.form = PrivateMessageForm()
     self.form.validate_on_submit()