예제 #1
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def post_edit(self, id):
        """ Post Route for Edit an item
            TODO: Ability to edit title/url for posts
        """
        commentForm = self._commentForm(request)
        item = Item.query.get_or_404(id)
        if commentForm.validate_on_submit():
            item.text = self._clean_text(commentForm.text.data)
            item.last_changed = datetime.utcnow()
            item = db.session.merge(item)
            db.session.commit()

            #TODO: Delete only necessary items
            cache.delete_memoized(Item.get_children)
            cache.delete_memoized(Item.get_item_and_children)
            key = make_template_fragment_key("item_text", vary_on=[item.__str__(), item.changed])
            cache.delete(key)

            flash('Edit saved', 'info')
            response = make_response(render_template('item/item.html',
                        item = item, form = commentForm, title=item.title, edit=True))
            next_url = url_for('frontend.item_edit', id=item.id)

            response.headers['X-PJAX-URL'] = next_url
            return response
        else:
            return render_template('item/item.html',
                        item = item, form = commentForm, title=item.title, edit=True)
예제 #2
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def post_item_comment(self, id=None, title=None):
        """ Post route for creating new comment on any item or page
        """
        commentForm = self._commentForm(request)
        if not id:
            id = Item.find_by_title(title).id
        if commentForm.validate_on_submit():
            comment = self._submit_item(text=commentForm.text.data,
                                        parent_id=id)

            if comment.id and comment.parent_id:
                flash('Thanks for adding to the discussion!',
                      category='success')
            else:
                flash(
                    'Something went wrong adding your comment. Please try again',
                    category='error')
                return render_template('item/submit.html',
                                       form=form,
                                       title='Submit')

            # Figure out what url we submitted from so we can keep them there
            next_url = request.headers.get('source_url', request.url)
            path = urlparse(next_url)[2]
            next_id = path[path.rfind('/') + 1:]
            next_url = path + '#item-' + str(comment.id)

            #Redefine the items to pass to template for PJAX
            if 'item' in path:
                item = Item.query.get_or_404(next_id)
            else:
                #TODO: Delete only necessary item
                cache.delete_memoized(Item.find_by_title)
                next_id = urllib.unquote(next_id)
                item = Item.find_by_title(next_id)

            commentForm = self._commentForm(request)
            commentForm.text.data = ''  #form data isn't clearing, so do it manually

            # Make new response and set url header for PJAX
            response = make_response(
                render_template('item/item.html',
                                item=item,
                                form=commentForm,
                                title=item.title))
            response.headers['X-PJAX-URL'] = next_url
            return response
        else:
            return render_template('item/submit.html',
                                   form=form,
                                   title='Submit')
예제 #3
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def _submit_item(self, url=None, title=None, text=None, 
                            kind='comment', parent_id=None):
        """ Submits an item (post or comment) to db
            TODO: Some kind of bad language filter?
        """ 
        text = self._clean_text(text)

        if kind=='comment' and not parent_id:
            return None

        item = Item(url = url,
                    title = title,
                    text = text,
                    kind = kind,
                    parent_id = parent_id,
                    timestamp = datetime.utcnow(),
                    last_changed = datetime.utcnow(),
                    user_id = current_user.id)

        db.session.add(item)
        db.session.commit()

        # TODO : Need to delete cache of just related items, not all
        cache.delete_memoized(Item.ranked_posts)
        cache.delete_memoized(Item.get_item_and_children)
        cache.delete_memoized(Item.get_children)

        return item
예제 #4
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def post_item_comment(self, id=None, title=None):
        """ Post route for creating new comment on any item or page
        """
        commentForm = self._commentForm(request)
        if not id:
            id = Item.find_by_title(title).id
        if commentForm.validate_on_submit():
            comment = self._submit_item(text=commentForm.text.data, parent_id=id)

            if comment.id and comment.parent_id:
                flash('Thanks for adding to the discussion!', category = 'success')
            else:
                flash('Something went wrong adding your comment. Please try again', category='error')
                return render_template('item/submit.html', form=form, title='Submit')

            # Figure out what url we submitted from so we can keep them there
            next_url = request.headers.get('source_url', request.url)
            path = urlparse(next_url)[2]
            next_id = path[path.rfind('/') + 1:]
            next_url = path + '#item-' + str(comment.id)

            #Redefine the items to pass to template for PJAX
            if 'item' in path:
                item = Item.query.get_or_404(next_id)
            else:
                #TODO: Delete only necessary item
                cache.delete_memoized(Item.find_by_title)
                next_id = urllib.unquote(next_id)
                item = Item.find_by_title(next_id)

            commentForm = self._commentForm(request)
            commentForm.text.data = '' #form data isn't clearing, so do it manually

            # Make new response and set url header for PJAX
            response = make_response(render_template('item/item.html',
                                    item = item, form = commentForm, title=item.title))
            response.headers['X-PJAX-URL'] = next_url
            return response
        else:
            return render_template('item/submit.html', form=form, title='Submit')
예제 #5
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def _submit_item(self,
                     url=None,
                     title=None,
                     text=None,
                     kind='comment',
                     parent_id=None):
        """ Submits an item (post or comment) to db
            TODO: Some kind of bad language filter?
        """
        text = self._clean_text(text)

        if kind == 'comment' and not parent_id:
            return None

        item = Item(url=url,
                    title=title,
                    text=text,
                    kind=kind,
                    parent_id=parent_id,
                    timestamp=datetime.utcnow(),
                    last_changed=datetime.utcnow(),
                    user_id=current_user.id)

        db.session.add(item)
        db.session.commit()

        # TODO : Need to delete cache of just related items, not all
        cache.delete_memoized(Item.ranked_posts)
        cache.delete_memoized(Item.get_item_and_children)
        cache.delete_memoized(Item.get_children)

        return item
예제 #6
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def get_item(self, id):
        """ View for a singular item (post or comment)
            Form is used to comment on that post or comment
        """
        item = Item.get_item_and_children(id)
        commentForm = self._commentForm(request)

        #TODO: Having trouble with cache. Delete if we can't find votes/user info and get again
        try:
            print item.votes
            print item.user.name
        except:
            print 'Could not get votes or user info'
            cache.delete_memoized(Item.ranked_posts)
            cache.delete_memoized(Item.get_item_and_children)
            item = Item.get_item_and_children(id)

        title = item.title
        if item.kind == 'comment':
            title = item.user.name + ' comment'
        return render_template('item/item.html',
            item = item, form = commentForm, title=title)
예제 #7
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def post_edit(self, id):
        """ Post Route for Edit an item
            TODO: Ability to edit title/url for posts
        """
        commentForm = self._commentForm(request)
        item = Item.query.get_or_404(id)
        if commentForm.validate_on_submit():
            item.text = self._clean_text(commentForm.text.data)
            item.last_changed = datetime.utcnow()
            item = db.session.merge(item)
            db.session.commit()

            #TODO: Delete only necessary items
            cache.delete_memoized(Item.get_children)
            cache.delete_memoized(Item.get_item_and_children)
            key = make_template_fragment_key(
                "item_text", vary_on=[item.__str__(), item.changed])
            cache.delete(key)

            flash('Edit saved', 'info')
            response = make_response(
                render_template('item/item.html',
                                item=item,
                                form=commentForm,
                                title=item.title,
                                edit=True))
            next_url = url_for('frontend.item_edit', id=item.id)

            response.headers['X-PJAX-URL'] = next_url
            return response
        else:
            return render_template('item/item.html',
                                   item=item,
                                   form=commentForm,
                                   title=item.title,
                                   edit=True)
예제 #8
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def get_item(self, id):
        """ View for a singular item (post or comment)
            Form is used to comment on that post or comment
        """
        item = Item.get_item_and_children(id)
        commentForm = self._commentForm(request)

        #TODO: Having trouble with cache. Delete if we can't find votes/user info and get again
        try:
            print item.votes
            print item.user.name
        except:
            print 'Could not get votes or user info'
            cache.delete_memoized(Item.ranked_posts)
            cache.delete_memoized(Item.get_item_and_children)
            item = Item.get_item_and_children(id)

        title = item.title
        if item.kind == 'comment':
            title = item.user.name + ' comment'
        return render_template('item/item.html',
                               item=item,
                               form=commentForm,
                               title=title)
예제 #9
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def post_vote(self, id):
        """ Post Route for adding a vote
            Submitted via AJAX
        """
        new_vote = json.loads(request.data)
        vote = Vote(timestamp=datetime.utcnow(),
                    user_from_id=current_user.id,
                    user_to_id=new_vote['user_to_id'],
                    item_id=new_vote['item_id'])

        db.session.add(vote)
        db.session.commit()

        #TODO: Delete only necessary items
        cache.delete_memoized(Item.voted_for)
        cache.delete_memoized(Item.get_children)
        cache.delete_memoized(Item.get_item_and_children)
        cache.delete_memoized(Item.ranked_posts)

        return jsonify(vote.serialize)
예제 #10
0
파일: views.py 프로젝트: TonyGu423/DataNews
    def post_vote(self, id):
        """ Post Route for adding a vote
            Submitted via AJAX
        """
        new_vote = json.loads(request.data)
        vote = Vote(timestamp = datetime.utcnow(),
                    user_from_id = current_user.id,
                    user_to_id =  new_vote['user_to_id'],
                    item_id =  new_vote['item_id']
                )

        db.session.add(vote)
        db.session.commit()

        #TODO: Delete only necessary items
        cache.delete_memoized(Item.voted_for)
        cache.delete_memoized(Item.get_children)
        cache.delete_memoized(Item.get_item_and_children)
        cache.delete_memoized(Item.ranked_posts)

        return jsonify(vote.serialize)