Beispiel #1
0
def membership(id):
    json = {}

    community = session.query(Community).filter_by(id=id).first()

    # See if they're already in the community
    community_user = session.query(CommunityUser).filter_by(
        user=current_user, community=community).first()

    # If they're not in the community, add them. Otherwise, remove them
    if community_user is None:
        community_user = CommunityUser(user=current_user, community=community)

        session.add(community_user)
        session.commit()

        json['action'] = 'joined'

    else:
        session.delete(community_user)
        session.commit()

        json['action'] = 'left'

    json['success'] = True

    return jsonify(**json)
Beispiel #2
0
def report_post():
    form = PostReportForm(request.form)
    post = None

    if form.validate():

        # Lets get the post in question
        post = session.query(Post).filter_by(id=int(form.post.data)).first()

        # Has the user already reported this post?
        postReportCount = session.query(PostReport).filter_by(
            user=current_user, parent=post).count()

        if postReportCount == 0:
            postReport = PostReport(title=form.title.data,
                                    content=form.content.data,
                                    user=current_user,
                                    post=post,
                                    category=form.category.data)

            session.add(postReport)
            session.commit()
        else:
            flash("You have already reported this post")

    else:
        flash_errors(form)

    return redirect(
        url_for('communities.show', permalink=post.community.permalink))
Beispiel #3
0
 def vote_for_community(self, community):
     # First we want to make sure that this user
     # hasn't yet voted for this community
     upvoteCount = session.query(CommunityUpvote).filter_by(
         user_id=self.id, community_id=community.id).count()
     if upvoteCount == 0:  # Has not voted
         newUpvote = CommunityUpvote(self, community)
         session.add(newUpvote)
         session.commit()
     else:
         flash("You have already voted for this community")
Beispiel #4
0
def create():
    comment = None
    parent_post = None

    form = CommentCreateForm(request.form)

    if form.validate():  # Valid data

        # We need to get the post that the com
        # session.query(Post)

        if form.parent.data is not "":

            # We need to get the post that the com
            parent_comment = session.query(Comment).filter_by(
                id=int(form.parent.data)).first()

            parent_post = parent_comment.post

            comment = Comment(content=form.content.data,
                              user=current_user,
                              post=parent_post,
                              parent=parent_comment)

        elif form.post.data is not "":
            # Lets get the post
            parent_post = session.query(Post).filter_by(
                id=int(form.post.data)).first()

            comment = Comment(content=form.content.data,
                              user=current_user,
                              post=parent_post)

        try:
            if comment is not None:
                session.add(comment)
                session.commit()
        except:
            print("Unexpected Error : %s" % sys.exc_info()[0])

        print(request.form.get('redirect_to'))

        if request.form.get('redirect_to') is not None:
            return redirect(request.form.get('redirect_to'))

        return redirect(
            url_for('communities.show',
                    permalink=parent_post.community.permalink))

    else:
        flash_errors(form)

        return redirect(url_for('base.index'))
Beispiel #5
0
def follow(id):
    returnDict = {}

    user_to_follow = session.query(User).filter_by(id=id).first()

    # First make sure a Follower doesn't exist

    if user_to_follow is not None:
        returnDict['success'] = True
        follower = Follower(source_user=current_user,
                            target_user=user_to_follow)
        session.add(follower)
        session.commit()
    else:
        returnDict['success'] = False

    return jsonify(**returnDict)
Beispiel #6
0
def upvote_post(id):
    returnDict = {}
    # First lets get the post were upvoting
    post = session.query(Post).filter_by(id=id).first()

    # Check to see user hasn't already upvoted
    if not current_user.has_upvoted_post(post):
        returnDict['success'] = True
        pu = PostUpvote(post, current_user)

        # Add the new post upvote
        session.add(pu)
        session.commit()
    else:
        returnDict['success'] = False
        # flash("User has already upvoted post")

    return jsonify(**returnDict)
Beispiel #7
0
    def vote(self, user):
        # First lets make sure the user hasn't voted
        # for this community yet
        voteCount = session.query(CommunityUpvote).filter_by(
            user_id=user.id, community_id=self.id).count()

        if voteCount == 0:
            newUpvote = CommunityUpvote(user, self)
            session.add(newUpvote)
            session.commit()

            if self.queryNumUpvotes() >= 10:
                self.active = True

            session.commit()
            return True
        else:
            return False
Beispiel #8
0
def create():
    community = None  # This will be just a filler object for our community

    # Get the form data
    form = PostCreateForm(request.form)

    # Set the valid community choices
    form.community_select.choices = [(c.id, c.name)
                                     for c in current_user.get_communities()]

    if form.validate():

        # We need to get the community to pass through to post
        # constructor

        # First we check if there was the hidden field, or if
        # it was a select box
        if form.community_id.data is not "":  # Hidden Field
            community = session.query(Community).filter_by(
                id=int(form.community_id.data)).first()
        elif form.community_select is not None:  # Select Box
            community = session.query(Community).filter_by(
                id=int(form.community_select.data)).first()

        # Create the post object
        post = Post(text_brief=form.text_brief.data,
                    text_long=None,
                    image_url=None,
                    user=current_user,
                    community=community)

        # Add to the session and commit to the database
        session.add(post)
        session.commit()

        flash(u"Post Successfully Created", "success")
    else:
        pprint(form.errors)
        flash(u"Post failed", "error")

        # lets also flash the errors
        flash_errors(form)

    return redirect(url_for('communities.show', permalink=community.permalink))
Beispiel #9
0
def create():
    form = UserCreateForm(request.form)

    if form.validate():
        user = User(
            username=form.username.data,
            email=form.email.data,
            name=form.name.data,
            password=form.password.data
        )

        session.add(user)
        session.commit()

        login_user(user)

        return redirect(url_for('base.index'))

    return render_template('users/new.html', form=form)
Beispiel #10
0
    def __init__(self,
                 text_brief,
                 text_long,
                 image_url,
                 user,
                 community,
                 answered=False):
        self.text_brief = text_brief
        self.text_long = text_long
        self.answered = answered
        self.image_url = image_url
        self.user = user
        self.community = community

        # Default Values
        now = dt.now().isoformat()  # Current Time to Insert into Datamodels
        self.date_created = now
        self.date_modified = now

        # Each post starts with 1 upvote (whomever created the post)
        # We have to insert a record into the post_upvote table
        p_upvote = PostUpvote(self, self.user)
        session.add(p_upvote)
        session.commit()
Beispiel #11
0
def create():
    # banner_url = "http://dsedutech.org/images/demo/placement_banner1.jpg"
    # thumbnail_url = "http://i.imgur.com/7mo7QHW.gif"
    form = CommunityCreateForm(request.form)

    if form.validate():

        # First thing we are going to do is create a
        # Community object with the data from our form.
        community = Community(name=form.name.data,
                              description=form.description.data,
                              nsfw=form.nsfw.data)

        session.add(community)
        session.commit()  # we need to create a record so we have the id

        # Now we have a community with an ID. Lets create
        # a directory to upload image files to.
        community_upload_relative = 'communities/' + str(community.id)
        community_upload_url = './amable/uploads/' + community_upload_relative

        if not os.path.exists(community_upload_url):
            os.makedirs(community_upload_url)

        # Lets do checks for Banner
        if 'banner' in request.files:
            banner_file = request.files['banner']

            if banner_file.filename != '':
                if allowed_file(banner_file.filename):
                    banner_filename = 'banner_' + \
                        secure_filename(banner_file.filename)

                    # Save | Upload Banner file
                    fullPath = os.path.join(community_upload_url,
                                            banner_filename)

                    banner_file.save(fullPath)

                    community.banner_url = community_upload_relative + "/" + banner_filename
                else:
                    flash('Banner file type is not allowed, could not upload',
                          'error')

        # Now lets do checks for Thumbnail
        if 'thumbnail' in request.files:
            thumbnail_file = request.files['thumbnail']

            if thumbnail_file.filename != '':
                if allowed_file(thumbnail_file.filename):
                    thumbnail_filename = 'thumbnail_' + \
                        secure_filename(thumbnail_file.filename)

                    # Save | Upload Thumbnail File
                    fullPath = os.path.join(community_upload_url,
                                            thumbnail_filename)

                    thumbnail_file.save(fullPath)

                    community.thumbnail_url = community_upload_relative + "/" + thumbnail_filename
                else:
                    flash(
                        'Thumbnail file type is not allowed, could not upload',
                        'error')

        # community.banner_url = banner_url
        # community.thumbnail_url = thumbnail_url
        community.vote(current_user)
        session.commit()

        return redirect(
            url_for('communities.show', permalink=community.permalink))

    return redirect(url_for('communities.new'))
Beispiel #12
0
session.commit()

# user1 replies to comment0
comment5 = CommentFactory(user=user1, post=post0, parent=comment0)

# --- POST REPORTS --- #

# user 2 and 3 reports post 0
pr0 = PostReportFactory(post=post0, user=user2)
pr1 = PostReportFactory(post=post0, user=user3)

session.commit()

# Lets give post1 9 reports (to test)
for x in range(0, 9):
    session.add(PostReportFactory(post=post1))
    session.commit()

# --- POST UPVOTES --- #

# user 1 upvotes post 0
pu0 = PostUpvoteFactory(post=post0, user=user1)

session.commit()

# topComment = CommentFactory()

# session.commit()

# subComment1 = CommentFactory(post=topComment.post, parent=topComment.id)
# subComment2 = CommentFactory(post=topComment.post, parent=topComment.id)
Beispiel #13
0
from amable import session
from amable.models.post_upvote import PostUpvote, update_date_modified
from amable.models.post import Post
from amable.models.community import Community
from amable.models.user import User

from spec.factories.post_upvote_factory import PostUpvoteFactory
from spec.factories.post_factory import PostFactory
from spec.factories.user_factory import UserFactory

s = session()

with context('amable.models'):
    with before.each:
        self.post_upvote = PostUpvoteFactory.create()
        session.add(self.post_upvote)
        session.commit()

    with after.all:
        session.rollback()
        session.query(PostUpvote).delete()
        session.query(Post).delete()
        session.query(Community).delete()
        session.query(User).delete()
        session.commit()

    with context('post_upvote'):
        with context('PostUpvote'):
            with context('__init__'):
                with it('create'):
                    post = PostFactory.create()
Beispiel #14
0
from expects import *

from amable import session
from amable.models.hashtag import Hashtag, update_date_modified

from spec.factories.hashtag_factory import HashtagFactory


s = session()

with context('amable.models'):
    with before.each:
        self.hashtag = HashtagFactory.create()
        session.add(self.hashtag)
        session.commit()

    with after.all:
        session.rollback()
        session.query(Hashtag).delete()
        session.commit()

    with context('hashtag'):
        with context('Hashtag'):
            with context('__init__'):
                with it('create'):
                    hashtag = Hashtag(
                        tag='reevus'
                    )

                    expect(hashtag.tag).to(equal('reevus'))