Exemplo n.º 1
0
def increment_view_count(self, pks, content_type, user_id, view_type, retry: bool = True) -> None:
    """
    :param self:
    :param pk:
    :param content_type:
    :param user_id:
    :param view_type:
    :return:
    """
    individual_storage = False # TODO: Change back to true IFF we figure out how to manage storage of this table
    user = None
    if user_id:
        user = User.objects.get(pk=user_id)
    redis = RedisService().redis
    for pk in pks:
        key = f"{content_type}_{pk}"
        print(key)
        result = redis.incr(key)
        if pk and view_type == 'individual' and individual_storage:
            try:
                ObjectView.objects.create(
                    viewer=user,
                    target_id=pk,
                    target_type=ContentType.objects.filter(model=content_type).first(),
                    view_type=view_type,
                    )
            except:
                pass # fix for https://sentry.io/organizations/gitcoin/issues/1715509732/
Exemplo n.º 2
0
def grants_bulk_add(request, grant_str):
    grants = {}
    redis = RedisService().redis
    key = hashlib.md5(grant_str.encode('utf')).hexdigest()
    views = redis.incr(key)

    grants_data = grant_str.split(':')[0].split(',')

    for ele in grants_data:
        # new format will support amount and token in the URL separated by ;
        grant_data = ele.split(';')
        if len(grant_data) > 0 and grant_data[0].isnumeric():
            grant_id = grant_data[0]
            grants[grant_id] = {'id': int(grant_id)}

            if len(grant_data) == 3:  # backward compatibility
                grants[grant_id]['amount'] = grant_data[1]
                grants[grant_id]['token'] = FTokens.objects.filter(
                    id=int(grant_data[2])).first()

    by_whom = ""
    prefix = ""
    try:
        by_whom = f"by {grant_str.split(':')[1]}"
        prefix = f"{grant_str.split(':')[2]} : "
    except:
        pass

    # search valid grants and associate with its amount and token
    grants_info = grants.values()
    grant_ids = [grant['id'] for grant in grants_info]
    for grant in Grant.objects.filter(pk__in=grant_ids):
        grants[str(grant.id)]['obj'] = grant

    grants = [grant for grant in grants_info if grant.get('obj')]

    grant_titles = ", ".join([grant['obj'].title for grant in grants])
    title = f"{prefix}{len(grants)} Grants in Shared Cart {by_whom} : Viewed {views} times"

    context = {
        'grants':
        grants,
        'avatar_url':
        request.build_absolute_uri(
            static('v2/images/twitter_cards/tw_cards-03.png')),
        'title':
        title,
        'card_desc':
        "Click to Add All to Cart: " + grant_titles
    }
    response = TemplateResponse(request,
                                'grants/bulk_add_to_cart.html',
                                context=context)
    return response