Exemplo n.º 1
0
 def PUT(self, request, post_id):
     story = models.story.Story.active_story()
     if not story:
         return ("", 400)
     elif not story.accepting_snippets():
         return ("", 429)
         
     post = models.post.Post.get(post_id)
     if not post:
         return ("", 404)
         
     voted_users = [user['objectId'] for user in post.voted_users]
     if User.current_user().objectId not in voted_users:
         post.voted_users.append(User.current_user())
     
     post.save()
     
     refresh = False
     if len(post.voted_users) >= models.post.Post.max_vote_count():
         story.accept_post(post)
         story.save()
         
         refresh = True
     
     return json.dumps({
         'votes': len(post.voted_users),
         'refresh': refresh
     })
Exemplo n.º 2
0
    def PUT(self, request, post_id):
        story = models.story.Story.active_story()
        if not story:
            return ("", 400)
        elif not story.accepting_snippets():
            return ("", 429)

        post = models.post.Post.get(post_id)
        if not post:
            return ("", 404)

        voted_users = [user['objectId'] for user in post.voted_users]
        if User.current_user().objectId not in voted_users:
            post.voted_users.append(User.current_user())

        post.save()

        refresh = False
        if len(post.voted_users) >= models.post.Post.max_vote_count():
            story.accept_post(post)
            story.save()

            refresh = True

        return json.dumps({'votes': len(post.voted_users), 'refresh': refresh})
Exemplo n.º 3
0
 def GET(self, request, story_id):        
     story = models.story.Story.get(story_id)
     if not story:
         story = models.story.Story.active_story()
         
     accepted_posts = sorted([
         models.post.Post.get(post['objectId'])
         for post in story.accepted_posts
     ], cmp=lambda x, y: 1 if x.createdAt > y.createdAt else -1)
     
     snippets = sorted([
         models.post.Post.get(post['objectId'])
         for post in story.snippets
     ], cmp=models.post.comparePosts)
     
     initials = ":)"
     if self.session(request):
         with SessionToken(self.session(request)):
             user = User.current_user()
             initials = user.first_name[0] + user.last_name[0]
     
     return flask.render_template(
         "story.html",
         story=story,
         accepted_posts=accepted_posts,
         snippets=snippets,
         
         initials = initials,
         session = self.session(request),
         
         max_vote_count=models.post.Post.max_vote_count(),
         user_voted_story=story.user_voted(self.session(request)),
         
         text_length=models.post.Post.text_length
     )
Exemplo n.º 4
0
    def GET(self, request, story_id):
        story = models.story.Story.get(story_id)
        if not story:
            story = models.story.Story.active_story()

        accepted_posts = sorted([
            models.post.Post.get(post['objectId'])
            for post in story.accepted_posts
        ],
                                cmp=lambda x, y: 1
                                if x.createdAt > y.createdAt else -1)

        snippets = sorted([
            models.post.Post.get(post['objectId']) for post in story.snippets
        ],
                          cmp=models.post.comparePosts)

        initials = ":)"
        if self.session(request):
            with SessionToken(self.session(request)):
                user = User.current_user()
                initials = user.first_name[0] + user.last_name[0]

        return flask.render_template(
            "story.html",
            story=story,
            accepted_posts=accepted_posts,
            snippets=snippets,
            initials=initials,
            session=self.session(request),
            max_vote_count=models.post.Post.max_vote_count(),
            user_voted_story=story.user_voted(self.session(request)),
            text_length=models.post.Post.text_length)
Exemplo n.º 5
0
def user_calleft(token):
    if request.method == 'GET':
        session_token  = token
        with SessionToken(session_token):
            me = User.current_user()
            me.calleft = user_calories(token) - me.feed['calories']
            me.save()
            return me.calleft
Exemplo n.º 6
0
def user_calories(token):
    if request.method == 'GET':
        session_token  = token
        with SessionToken(session_token):
            me = User.current_user()
            me.calories = str(float(me.weight) * 33 )
            me.save()
            return me.calories
Exemplo n.º 7
0
def user_calleft(token):
    if request.method == 'GET':
        session_token = token
        with SessionToken(session_token):
            me = User.current_user()
            me.calleft = user_calories(token) - me.feed['calories']
            me.save()
            return me.calleft
Exemplo n.º 8
0
def user_calories(token):
    if request.method == 'GET':
        session_token = token
        with SessionToken(session_token):
            me = User.current_user()
            me.calories = str(float(me.weight) * 33)
            me.save()
            return me.calories
Exemplo n.º 9
0
    def DELETE(self, request, post_id):
        story = models.story.Story.active_story()
        if not story:
            return ("", 400)
        elif not story.accepting_snippets():
            return ("", 429)

        post = models.post.Post.get(post_id)
        if not post:
            return ("", 404)

        voted_users = [user['objectId'] for user in post.voted_users]
        if User.current_user().objectId in voted_users:
            post.voted_users = [
                user for user in post.voted_users
                if user['objectId'] != User.current_user().objectId
            ]

        post.save()
        return json.dumps({'votes': len(post.voted_users)})
Exemplo n.º 10
0
    def testCanGetCurrentUser(self):
        user = User.signup(self.username, self.password)
        self.assertIsNotNone(user.sessionToken)

        register(getattr(settings_local, 'APPLICATION_ID'),
                 getattr(settings_local, 'REST_API_KEY'),
                 session_token=user.sessionToken)

        current_user = User.current_user()
        self.assertIsNotNone(current_user)
        self.assertEqual(current_user.sessionToken, user.sessionToken)
        self.assertEqual(current_user.username, user.username)
Exemplo n.º 11
0
    def user_voted(self, session):
        with SessionToken(session):
            for snippet in self.snippets:
                post = Post.get(snippet['objectId'])
                user_voted = bool([
                    user for user in post.voted_users
                    if user['objectId'] == User.current_user().objectId
                ])

                if user_voted:
                    return True
            return False
Exemplo n.º 12
0
 def user_voted(self, session):
     with SessionToken(session):
         for snippet in self.snippets:
             post = Post.get(snippet['objectId'])
             user_voted = bool([
                 user for user in post.voted_users
                 if user['objectId'] == User.current_user().objectId
             ])
             
             if user_voted:
                 return True
         return False
Exemplo n.º 13
0
 def DELETE(self, request, post_id):
     story = models.story.Story.active_story()
     if not story:
         return ("", 400)
     elif not story.accepting_snippets():
         return ("", 429)
         
     post = models.post.Post.get(post_id)
     if not post:
         return ("", 404)
         
     voted_users = [user['objectId'] for user in post.voted_users]
     if User.current_user().objectId in voted_users:
         post.voted_users = [
             user for user in post.voted_users
             if user['objectId'] != User.current_user().objectId
         ]
     
     post.save()
     return json.dumps({
         'votes': len(post.voted_users)
     })
Exemplo n.º 14
0
    def testCanGetCurrentUser(self):
        user = User.signup(self.username, self.password)
        self.assertIsNotNone(user.sessionToken)

        register(
            getattr(settings_local, 'APPLICATION_ID'),
            getattr(settings_local, 'REST_API_KEY'),
            session_token=user.sessionToken
        )

        current_user = User.current_user()
        self.assertIsNotNone(current_user)
        self.assertEqual(current_user.sessionToken, user.sessionToken)
        self.assertEqual(current_user.username, user.username)
Exemplo n.º 15
0
def user_feed(token):
    if request.method == 'GET':
        session_token = token
        with SessionToken(session_token):
            me = User.current_user()
            me.calconsumed = 0
            print(me.feed)

            for i in me.feed:
                j = json.loads(i)
                #print(j)
                me.calconsumed = str(int(j['calories']) + int(me.calconsumed))
            me.save()
            return me.calconsumed
Exemplo n.º 16
0
def user_feed(token):
    if request.method == 'GET':
        session_token  = token
        with SessionToken(session_token):
            me = User.current_user()
            me.calconsumed = 0
            print(me.feed)

            for i in me.feed:
                j = json.loads(i)
                #print(j)
                me.calconsumed = str(int(j['calories']) + int(me.calconsumed))
            me.save()
            return me.calconsumed
Exemplo n.º 17
0
    def POST(self, request):
        story = models.story.Story.active_story()
        if not story:
            return ("Cannot find active story", 400)

        post = models.post.Post(text=request.values.get("text"),
                                author=User.current_user(),
                                voted_users=[],
                                archived=False)

        if not post or not post.valid():
            return ("Post invalid " + str(post), 400)

        post.save()

        story.snippets.append(post)
        story.save()

        return flask.redirect('/')
Exemplo n.º 18
0
 def POST(self, request):
     story = models.story.Story.active_story()
     if not story:
         return ("Cannot find active story", 400)
         
     post = models.post.Post(
         text=request.values.get("text"),
         author=User.current_user(),
         voted_users=[],
         archived=False
     )
     
     if not post or not post.valid():
         return ("Post invalid " + str(post), 400)
         
     post.save()
     
     story.snippets.append(post)
     story.save()
     
     return flask.redirect('/')
Exemplo n.º 19
0
 def user_voted(self, session):
     with SessionToken(session):
         return bool([
             user for user in self.voted_users
             if user['objectId'] == User.current_user().objectId
         ])
Exemplo n.º 20
0
 def user_voted(self, session):
     with SessionToken(session):        
         return bool([
             user for user in self.voted_users
             if user['objectId'] == User.current_user().objectId
         ])
Exemplo n.º 21
0
def GetCurrentUser():
	token = session.get('session_token')
	settings_local.initParse(token)
	me = User.current_user()
	return me