예제 #1
0
    def save(self, commit=True, *args, **kwargs):
        lets_object = super(LetsForm, self).save(
            *args, commit=commit, **kwargs)

        if not commit:
            return lets_object

        lets_object.save()

        # Coins
        user_key = Lets.u_cache_key(self.creator)
        lets_key = cache.get(user_key)
        if lets_key:
            # No 250 +
            cache.incr(user_key)
        else:
            # Yes 250 +
            cache.set(user_key, 1, 86400)
            profile = self.creator.profile
            old_coins = profile.coins
            profile.update(coins=profile.coins +
                           ucs.ACTIONS_COINS['lets']['daily'])
            updated_coins = profile.coins
            changetuple = (self.creator.username, old_coins, updated_coins)
            log.warning('Coins changed for (%s): %s -> %s' % changetuple)

        return lets_object
예제 #2
0
def delete(request, lets_id):
    """Delete a Lets object, only if logged in user is the creator."""

    lets = get_object_or_404(Lets, pk=lets_id)
    if lets.creator != request.user:
        raise PermissionDenied

    lets.delete()

    # Coins
    profile = request.user.profile
    user_key = Lets.u_cache_key(request.user)
    lets_key = cache.get(user_key)
    if lets_key:
        if lets_key > 0:
            # No 250 -
            cache.decr(user_key)

        if lets_key <= 0:
            # Yes 250 -
            profile.update(coins=profile.coins -
                           ucs.ACTIONS_COINS['lets']['daily'])
            cache.delete(user_key)
    else:
        # yes 250 -
        profile.update(coins=profile.coins -
                       ucs.ACTIONS_COINS['lets']['daily'])
    update_progress(profile, action='lets')
    update_rewards(request.user, 'lets')
    process_mojo_action_down(request.user, 'lets')

    # TODO: return progress_data with ajax request. Modify js on lets side to
    #   update profile completion widget
    next_url = reverse('lets.wall', args=[request.user.username])
    if request.is_ajax():
        return JSONResponse({'is_valid': True,
                             'msg': 'Deleted'})
    return HttpResponseRedirect(next_url)