Beispiel #1
0
    def get(self, topic_id):
        topic = yield gen.maybe_future(Topic.get(topic_id))
        if not topic:
            raise HTTPError(404)
        user = yield gen.maybe_future(User.get(topic.admin_id))
        up_votes = yield gen.maybe_future(TopicUpVote.count_by_topic(topic_id))
        down_votes = yield gen.maybe_future(
            TopicDownVote.count_by_topic(topic_id))

        up_voted, down_voted = False, False
        username = self.current_user
        if username is not None:
            up_voted = bool((yield gen.maybe_future(
                TopicUpVote.get_by_user_topic(username, topic_id))))
            down_voted = bool((yield gen.maybe_future(
                TopicDownVote.get_by_user_topic(username, topic_id))))
        self.render(
            'proposal.html',
            title=topic.name,
            keywords=topic.name + ', 2L',
            description=topic.description,
            id=topic.id,
            avatar=topic.avatar,
            rules=topic.rules,
            why=topic.why,
            state=topic.state,
            date=topic.date,
            user=user.username,
            #  user_avatar=user.profile.avatar,
            up_votes=up_votes,
            down_votes=down_votes,
            up_voted=int(up_voted),
            down_voted=int(down_voted),
        )
Beispiel #2
0
 def test_post(self):
     r = self.post(self._path.format(''), body=TOPIC)
     data = TOPIC
     data.update({
         'id': 1,
         'admin': self._me['username'],
     })
     self.assertEqual(r.code, 200)
     self.assertDictEqual(json.loads(r.body), {'status': 1})
     topic = Topic.get(1)
     self.assertDictEqual(topic.to_dict(), data)
     self._mox.VerifyAll()
Beispiel #3
0
    def patch(self, topic_id):
        fields = dict()
        for key in ('description', 'rules', 'avatar', 'state'):
            value = self.get_argument(key, None)
            if value is not None:
                fields[key] = value

        if not fields:
            raise exceptions.EmptyFields()
        else:
            topic = yield gen.maybe_future(Topic.get(topic_id))
            yield gen.maybe_future(topic.update(**fields))
Beispiel #4
0
    def delete(self, id_):
        if self.category == 'topic':
            t = yield gen.maybe_future(Topic.get(id_))
            if t and t.state in (-1, 1):
                raise exceptions.TopicVoteTimeHasPassed()

        username = self.current_user
        v = yield gen.maybe_future(self._get_by_user_category(username, id_))
        if v:
            yield gen.maybe_future(v.delete())
            count = yield gen.maybe_future(self._count_by_category(id_))
            # Update gold.
            update_gold.apply_async(
                ('cancel_vote', username, id_, self.category, self.vote_type))
            raise gen.Return({'count': count})
        else:
            raise exceptions.NoVoteCanBeCancel()
Beispiel #5
0
 def get(self, topic_id):
     topic = yield gen.maybe_future(Topic.get(topic_id))
     if not topic:
         raise HTTPError(404)
     admin = yield gen.maybe_future(User.get(topic.admin_id))
     is_subscribed = False
     if self.current_user:
         is_subscribed = yield gen.maybe_future(
             Subscription.get_by_user_topic(self.current_user, topic.id))
     self.render(
         'topic.html',
         title=topic.name,
         keywords=topic.name + ', 2L',
         description=topic.description,
         topic_id=topic_id,
         admin=admin.username,
         avatar=topic.avatar,
         rules=topic.rules,
         is_subscribed=int(bool(is_subscribed)),
     )
Beispiel #6
0
    def post(self, id_):
        if self.category == 'topic':
            t = yield gen.maybe_future(Topic.get(id_))
            if t and t.state in (-1, 1):
                raise exceptions.TopicVoteTimeHasPassed()

        username = self.current_user
        v = yield gen.maybe_future(self._get_by_user_category(username, id_))
        if v:
            vote_type = self.vote_type.capitalize()
            exception = getattr(exceptions,
                                'CanNotVote{0}Again'.format(vote_type))
            raise exception()
        else:
            yield gen.maybe_future(self.table.create(username, id_))
            count = yield gen.maybe_future(self._count_by_category(id_))

            # Update gold.
            update_gold.apply_async(
                ('vote', username, id_, self.category, self.vote_type))
            raise gen.Return({'count': count})
Beispiel #7
0
def check_proposal(topic_id):
    from app.libs.db import db_session
    from app.models import Topic, User, TopicUpVote, TopicDownVote
    from app.settings import Gold

    user_count = User.count()
    topic = Topic.get(topic_id)
    user = User.get(topic.admin_id)
    up_votes = TopicUpVote.count_by_topic(topic_id)
    down_votes = TopicDownVote.count_by_topic(topic_id)

    # Check if the proposal can be accepted or rejected.
    if (up_votes - down_votes) > (user_count / 2):
        topic.state = 1
        user.profile.gold += Gold['proposal_accepted']
    else:
        topic.state = -1
        user.profile.gold += Gold['proposal_rejected']

    db_session.add(user)
    db_session.add(topic)
    db_session.commit()
Beispiel #8
0
    def get(self, topic_id):
        username = self.current_user

        if topic_id:
            topic = yield gen.maybe_future(Topic.get(topic_id))
            info = yield gen.maybe_future(_topic_info(username, topic))
            raise gen.Return(info)
        else:
            page = int(self.get_argument('page', 1))
            per_page = int(self.get_argument('per_page', 20))

            pagination = yield gen.maybe_future(
                Topic.page_list_all(page, per_page))
            result = {
                'page': page,
                'per_page': per_page,
                'has_prev': pagination.has_prev,
                'has_next': pagination.has_next,
                'pages': pagination.pages,
                'total': pagination.total,
                'topics': [(yield gen.maybe_future(item.to_dict()))
                           for item in pagination.items],
            }
            raise gen.Return(result)
Beispiel #9
0
 def delete(self, topic_id):
     topic = yield gen.maybe_future(Topic.get(topic_id))
     if topic:
         yield gen.maybe_future(topic.delete())