Пример #1
0
    def POST(self):
        r = Redis(connection_pool=redis_pool)

        user_id = check_token(r)

        form = url_form()

        if not form.validates():
            return render_template('add.html',
                form=form,
                user_id=user_id,
                is_add=True,
            )

        url = form['url'].value
        token = hashlib.sha1()
        token.update(url.replace('http(s)?://', '').strip())
        key = token.hexdigest()[:6]
        print key + url
        if not r.hget(URL_HASH_NAME, key):
            r.hset(URL_HASH_NAME, key, url)
            r.hset(COUNT_HASH_NAME, key, 0)
            if user_id[1]:
                r.hset(LOG_HASH_NAME, key, r.hget(TOKEN_HASH_NAME, web.input().token))

        if user_id[1] == '':
            raise web.seeother('/%s/+' % key)
        else:
            raise web.seeother('/%s/+?token=%s' % (key, user_id[1]))
Пример #2
0
    def GET(self):
        r = Redis(connection_pool=redis_pool)
        user_id = check_token(r)

        form = url_form()
        return render_template('add.html',
            form=form,
            user_id=user_id,
            is_add=True,
        )
Пример #3
0
def user_info(request, user_id):
    if check_token(request.META['HTTP_AUTH']):
        if request.method == 'GET':
            return get_model_id(request=request,
                                id=user_id,
                                Model=User,
                                Serializer=UserSerializer)
        elif request.method == 'PUT':
            return put_model_id(request=request,
                                id=user_id,
                                Model=User,
                                Serializer=UserSerializer)
    else:
        response = {'message': "Token has expired", 'status_code': 403}
        return Response(ResponseSerializer(response).data)
Пример #4
0
    def GET(self):
        r = Redis(connection_pool=redis_pool)
        user_id = check_token(r)

        all_keys = r.hgetall(URL_HASH_NAME)
        url_list = []

        for key in all_keys:
            url_list.append(
                (
                    all_keys[key].replace('http://', '').replace('https://', ''),
                    key, r.hget(COUNT_HASH_NAME, key) or 0,
                    r.hget(LOG_HASH_NAME, key) or ''
                ),
            )

        return render_template('list.html', user_id=user_id, list=url_list, is_all=True)
Пример #5
0
    def GET(self, key):
        r = Redis(connection_pool=redis_pool)

        user_id = check_token(r)

        url = r.hget(URL_HASH_NAME, key)
        if url:
            count = r.hget(COUNT_HASH_NAME, key)

            return render_template('details.html',
                user_id=user_id,
                key=key,
                url=url,
                count=count,
                section_class='index'
            )
        else:
            raise notfound()
Пример #6
0
            yield from serializers.user(user)
            self.response.set_content(user)
            yield from self.response.close()

    @asyncio.coroutine
    def put(self):
        user = yield from self.get_user()

        if user:
            data = self.request.data
            fields_to_remove = ['email', '_id', 'salt']

            for key in fields_to_remove:
                if key in data:
                    del data[key]

            user['modified'] = datetime.datetime.now()
            user.update(data)
            yield from db.users.save(user)
            yield from serializers.user(user)
            self.response.set_content(user)
            yield from self.response.close()

loop = asyncio.get_event_loop()

# start check_token
loop.call_soon(asyncio.async, utils.check_token())

# start the application
app.start(loop, host, port)
Пример #7
0
            self.response.set_content(user)
            yield from self.response.close()

    @asyncio.coroutine
    def put(self):
        user = yield from self.get_user()

        if user:
            data = self.request.data
            fields_to_remove = ['email', '_id', 'salt']

            for key in fields_to_remove:
                if key in data:
                    del data[key]

            user['modified'] = datetime.datetime.now()
            user.update(data)
            yield from db.users.save(user)
            yield from serializers.user(user)
            self.response.set_content(user)
            yield from self.response.close()


loop = asyncio.get_event_loop()

# start check_token
loop.call_soon(asyncio. async, utils.check_token())

# start the application
app.start(loop, host, port)
Пример #8
0
def process_submission(request):
    # Process the previous submission
    try:
        options = signed.loads(request.POST.get('options', ''))
    except ValueError:
        return {}
    if (int(time.time()) - options['time']) > (5 * 60):
        return {} # Form is too old
    if not utils.check_token(options['token']):
        return {} # Token invalid
    
    species_pk = options['species']
    contestants = options['contestants']
    
    winner = int(request.POST.get('winner', ''))
    if not winner:
        return {}
    
    loser = (set(contestants) - set([winner])).pop()
    
    # Record a win!
    context = utils.record_win(species_pk, winner, loser)
    if not request.user.is_anonymous():
        utils.record_contribution_from(request.user.username)
    
    photos = Photo.objects.select_related(
        'created_by'
    ).in_bulk([winner, loser])
    
    last_species = Species.objects.get(pk = species_pk)
    
    description = '''
        %s: <a href="%s">%s</a>: <a href="%s"><img src="%s"></a> beat 
        <a href="%s"><img src="%s"></a>
        ''' % (
            str(datetime.datetime.now()),
            last_species.get_absolute_url(),
            last_species.common_name,
            photos[winner].get_absolute_url(),
            photos[winner].thumb_75_url(),
            photos[loser].get_absolute_url(),
            photos[loser].thumb_75_url(),
        )
    if not request.user.is_anonymous():
        description += ' (rated by <a href="%s">%s</a>)' % (
            request.user.username, request.user.username
        )
        # And record the species so we don't show it to them multiple times
        set_key = utils.USER_SEEN_SET % request.user.username
        list_key = utils.USER_SEEN_LIST % request.user.username
        r.push(list_key, species_pk, head=True)
        r.sadd(set_key, species_pk)
        if r.scard(set_key) >= SEEN_SPECIES_COUNT:
            r.srem(set_key, r.pop(list_key, tail=True))
    
    r.push('bestpic-activity', description, head=True)
    r.ltrim('bestpic-activity', 0, 200)
    
    context.update({
        'last_species': last_species,
        'last_winner': photos[winner],
        'last_loser': photos[loser],
        'show_link_to_best': utils.species_has_top_10(last_species),
    })
    return context