Example #1
0
    def post():
        article = Post.query.filter(Post.slug == slug).first()
        title = request.form.get("title")
        # if the title changed, check if there is already an article with the
        # new title
        if article.title != title:
            new_slug = title.replace(' ', '-').lower()
            existing_article = Post.query.filter(Post.slug == new_slug)
            if existing_article:
                flash(
                    "Sorry there is already an article with that title, try a different one",
                    "danger")
                return

        article.subtitle = request.form.get("subtitle")
        article.slug = title.replace(' ', '-').lower()
        # turn the title into a slug
        article.featured_image_url = request.form.get(
            "featured-image")  # get image url
        article.content = request.form.get("content")
        article.tags = request.form.get("tags")

        try:
            # Save changes
            db.commit()

            flash("Edits successfully saved", "success")
        except Exception:
            db.rollback()
            flash("There was an error processing your request", "danger")
Example #2
0
    def post():
        title = request.form.get("title")
        subtitle = request.form.get("subtitle")
        # turn the title into a slug by removing all non alphanumeric or numeric
        # characters
        slug = re.sub(r'[^a-zA-Z0-9]', '-', title)
        # make it all lowercase
        slug = slug.lower()
        print(slug)
        featured_image_url = request.form.get(
            "featured-image")  # get image url
        content = request.form.get("content")
        tags = request.form.get("tags")

        if Post.query.filter(Post.title == title).first():
            flash("Sorry there is already an article with that title",
                  "danger")
        else:
            try:
                post = Post(user_id=current_user.id,
                            title=title,
                            subtitle=subtitle,
                            content=content,
                            featured_image_url=featured_image_url,
                            slug=slug,
                            tags=tags)
                # add article to database
                db.add(post)
                db.commit()

                flash("New article has been successfully created", "success")
            except Exception:
                db.rollback()
                flash("There was an error processing your request", "danger")
Example #3
0
def create_remote_user(google_id, facebook_id):
    # hence current_user can be accessed afterwards
    if google_id:
        remote_user = RemoteSourceUser(user_id=current_user.id,
                                       google_id=google_id)
    else:
        remote_user = RemoteSourceUser(user_id=current_user.id,
                                       fb_id=facebook_id)
    try:
        db.add(remote_user)
        db.commit()
    except Exception:
        db.rollback()
Example #4
0
 def post():
     name = request.form.get("name")
     password = generate_password_hash(request.form.get('password'))
     user = User.query.filter(User.id == current_user.id).first()
     user.name = name
     user.password = password
     try:
         # save changes to the user
         db.commit()
         flash("Changes saved successfully", "success")
     except Exception:
         db.rollback()
         flash("There was an error processing your request", "danger")
Example #5
0
def delete_article(slug):

    article = Post.query.filter(Post.slug == slug).delete()
    if article:
        try:
            db.commit()
            flash("Article deleted", "info")
        except Exception:
            db.rollback()
            flash("There was an error processing your request", "danger")
    else:
        abort(404)

    return redirect(url_for("dashboard.show_pending_approvals"))
Example #6
0
def approve(slug):

    article = Post.query.filter(Post.slug == slug).first()
    if article:
        article.published = True
        try:
            db.commit()
            flash("Article approved", "success")
        except Exception:
            db.rollback()
            flash("There was an error processing your request", "danger")
    else:
        abort(404)

    return redirect(url_for("dashboard.show_pending_approvals"))
Example #7
0
    def post():
        user = User.query.filter(User.id == current_user.id).first()

        if (int(request.form.get("form-type")) == 1):
            print("Form type 1")
            user.name = request.form.get("name")
            user.description = request.form.get("description")
        else:
            print("Form type 2")
            password = generate_password_hash(request.form.get('password'))
            user.password = password
        try:
            # save changes to the user
            db.commit()
            flash("Changes saved successfully", "success")
        except Exception:
            db.rollback()
            flash("There was an error processing your request", "danger")
Example #8
0
 def post():
     try:
         password = generate_password_hash(request.form.get('password'))
         email = request.form.get("email")
         name = request.form.get("name")
         # Check if an account with the given credentials already exists
         if (User.query.filter(User.email == email).first()):
             flash('Sorry, that email is already in use', "danger")
         else:
             next = create_user(name,
                                email,
                                profile_image_url=None,
                                password=password)
             return redirect(next)
     except Exception as e:
         print(e)
         db.rollback()
         flash("There was an error processing your request", "danger")
     return render_template("register/register.html")
Example #9
0
 def post():
     name = request.form.get("name")
     email = request.form.get("email")
     image_url = request.form.get("social-image")
     google_id = request.form.get("google-id")
     facebook_id = request.form.get("fb-id")
     # user is logged in with this function
     next = create_user(name, email, image_url)
     # hence current_user can be accessed afterwards
     if google_id:
         remote_user = RemoteSourceUser(user_id=current_user.id,
                                        google_id=google_id)
     else:
         remote_user = RemoteSourceUser(user_id=current_user.id,
                                        fb_id=facebook_id)
     try:
         db.add(remote_user)
         db.commit()
     except Exception:
         db.rollback()
     return redirect(next)
Example #10
0
def activate_user():
    try:
        token = request.args.get("token")
        # check if the token matches the database
        verify_obj = VerifyEmailRequest.query.filter(
            VerifyEmailRequest.token == hashlib.sha256(
                token).hexdigest()).first()
        if (verify_obj is not None):
            # get the related user
            user = User.query.filter(User.id == verify_obj.user_id).first()
            if (user.verified):
                abort(404)
            user.verified = True
            verify_obj.completed = True
            db.commit()
            return render_template("register/successfully_verified.html")
        # else the user is not authorized
        else:
            db.rollback()
            abort(404)
    except Exception:
        abort(404)
Example #11
0
    def post():
        try:
            password = generate_password_hash(request.form.get('password'))
            email = request.form.get("email")
            name = request.form.get("name")
            username = request.form.get("username")
            # Check if an account with the given credentials already exists
            if (User.query.filter(User.email == email).first()):
                flash(
                    'Sorry, there is already an account associated with that email',
                    "danger")
            elif (User.query.filter(User.display_name == username).first()):
                flash('Sorry, that username has already been taken', "danger")
            else:
                user = User(name=name,
                            display_name=username,
                            email=email,
                            password=password,
                            profile_image_url=url_for(
                                'static', filename='images/default_logo.png'))
                # save the new user
                db.add(user)
                db.commit()

                # check if there is a user logged in, if so log them out
                if (current_user):
                    logout_user()
                # login the current user so that we have a handle on the object
                from app.http.controllers.mail_senders import send_verify_email
                send_verify_email(user)
                flash(
                    "The user was created successfully and a verification email has been sent",
                    "success")
        except Exception as e:
            print(e)
            db.rollback()
            flash("There was an error processing your request", "danger")