# Path: app/achievement/utils.py
# def reverse_with_query_params(view, params=None, *args, **kwargs):
#     """
#     Creates a reverse for a url with added query string
#     parameters.
# 
#     @param view: The view function to create the url for
#     @param params: List of key-value arguments to add in the parameters
#     @param args: List of arguments to use in creating the reversed url
#     @param kwargs: List of keyvalue arguments to use in creating the reversed url
#     """
#     if not params:
#         params = {}
# 
#     reversed_url = reverse(view, args=args, kwargs=kwargs)
#     query_params = QueryDict('', mutable = True)
#     query_params.update(params)
# 
#     return "{0}?{1}".format(reversed_url, query_params.urlencode())


from notifications import notify
from django.db.models.signals import post_save
from app.achievement.utils import reverse_with_query_params as reverse
    """
    if not created and instance.active:
        creator = instance.creator
        if creator:
            # Only notify if there's actually a creator attached to the achievement
            notify.send(instance, recipient=creator.user, verb='was approved.',
                description='Your achievement, {0} was approved.'.format(instance.name),
                url=reverse('view_achievement', achievement_id=instance.achievement.pk))


def notify_achievement_unlocked(sender, instance, created, **kwargs):
    """
    Notifies a user that they have unlocked an achievement.

    @param sender: The model class
    @param instance: Instance of the model class being saved
    @param created: True if this is newly created.
    @param kwargs: Dictionary of key-value arguments.
    @return: None
    """
    if not created:
        return None

    if instance.achievement.badge:
        instance.user.badges.add(instance.achievement.badge)
    instance.user.points += instance.achievement.points
    instance.user.save()

    notify.send(instance, recipient=instance.user.user, verb='was unlocked',
        description='You have unlocked "{0}"'.format(instance.achievement.name),

# Path: app/achievement/utils.py
# def reverse_with_query_params(view, params=None, *args, **kwargs):
#     """
#     Creates a reverse for a url with added query string
#     parameters.
# 
#     @param view: The view function to create the url for
#     @param params: List of key-value arguments to add in the parameters
#     @param args: List of arguments to use in creating the reversed url
#     @param kwargs: List of keyvalue arguments to use in creating the reversed url
#     """
#     if not params:
#         params = {}
# 
#     reversed_url = reverse(view, args=args, kwargs=kwargs)
#     query_params = QueryDict('', mutable = True)
#     query_params.update(params)
# 
#     return "{0}?{1}".format(reversed_url, query_params.urlencode())


# Path: app/achievement/models/signals.py
from notifications import notify
from django.db.models.signals import post_save
from app.achievement.utils import reverse_with_query_params as reverse

    """
    if not created and instance.active:
        creator = instance.creator
        if creator:
            # Only notify if there's actually a creator attached to the achievement
            notify.send(instance, recipient=creator.user, verb='was approved.',
                description='Your achievement, {0} was approved.'.format(instance.name),
                url=reverse('view_achievement', achievement_id=instance.achievement.pk))


def notify_achievement_unlocked(sender, instance, created, **kwargs):
    """
    Notifies a user that they have unlocked an achievement.

    @param sender: The model class
    @param instance: Instance of the model class being saved
    @param created: True if this is newly created.
    @param kwargs: Dictionary of key-value arguments.
    @return: None
    """
    if not created:
        return None

    if instance.achievement.badge:
        instance.user.badges.add(instance.achievement.badge)
    instance.user.points += instance.achievement.points
    instance.user.save()

    notify.send(instance, recipient=instance.user.user, verb='was unlocked',
        description='You have unlocked "{0}"'.format(instance.achievement.name),

        url=reverse('view_achievement', achievement_id=instance.achievement.pk))