Esempio n. 1
0
STK = "source_tracking_key"


class SourceTrackingMiddleware(object):
    def process_request(self, request):
        if request.user.is_authenticated():
            return None
        codes = request.session.get(STK, {})
        source = request.GET.get("source", None)
        subsource = request.GET.get("subsource", None)
        if source or subsource:
            codes["source"] = source or ""
            codes["subsource"] = subsource or ""
        referrer = request.META.get("HTTP_REFERER", None)
        if referrer and not re.search(DOMAIN, referrer):
            codes["referrer"] = referrer
        if codes.items():
            request.session[STK] = codes


def add_source_tracking(sender, request, user, is_new_user, **kwargs):
    if is_new_user:
        codes = request.session.pop(STK, {"source": "direct"})
        UserSource.objects.create(user=user,
                                  source=codes.get("source", ""),
                                  subsource=codes.get("subsource", ""),
                                  referrer=codes.get("referrer", ""))


logged_in.connect(add_source_tracking)
Esempio n. 2
0
from rah.signals import logged_in

from models import UserSource

DOMAIN = re.compile("https?://%s" % Site.objects.get_current().domain)
STK = "source_tracking_key"

class SourceTrackingMiddleware(object):
    def process_request(self, request):
        if request.user.is_authenticated():
            return None
        codes = request.session.get(STK, {})
        source = request.GET.get("source", None)
        subsource = request.GET.get("subsource", None)
        if source or subsource:
            codes["source"] = source or ""
            codes["subsource"] = subsource or ""
        referrer = request.META.get("HTTP_REFERER", None)
        if referrer and not re.search(DOMAIN, referrer):
            codes["referrer"] = referrer
        if codes.items():
            request.session[STK] = codes

def add_source_tracking(sender, request, user, is_new_user, **kwargs):
    if is_new_user:
        codes = request.session.pop(STK, {"source": "direct"})
        UserSource.objects.create(user=user, source=codes.get("source", ""),
            subsource=codes.get("subsource", ""), referrer=codes.get("referrer", ""))
logged_in.connect(add_source_tracking)
Esempio n. 3
0
    created = models.DateTimeField(_('created'), auto_now_add=True)
    updated = models.DateTimeField(_('updated'), auto_now=True)

    class Meta:
        unique_together = ("action_form", "user",)
        verbose_name = ("action form data")
        verbose_name_plural = ("action form data")

    def __unicode__(self):
        return _("%(user)s is working on %(action_form)s") % {
            'user': self.user, 'action_form': self.action_form}

"""
SIGNALS!
"""
def update_action_aggregates(sender, instance, **kwargs):
    instance.action.users_completed = UserActionProgress.objects.filter(action=instance.action, is_completed=True).count()
    instance.action.users_committed = UserActionProgress.objects.filter(action=instance.action, date_committed__isnull=False).count()
    instance.action.save()
models.signals.post_save.connect(update_action_aggregates, sender=UserActionProgress)

def apply_changes_from_commitment_cards(sender, request, user, is_new_user, **kwargs):
    changes = Action.objects.process_commitment_card(user, new_user=is_new_user)
    if not changes:
        return
    messages.success(request,
                     _("%(num_actions)s actions were applied to "
                       "your account from a commitment card") % {'num_actions': len(changes)},
                     extra_tags="sticky")
logged_in.connect(apply_changes_from_commitment_cards)
Esempio n. 4
0
    dict and initialized with the corresponding ActionForm.
    """
    action_form = models.ForeignKey(ActionForm)
    user = models.ForeignKey(User)
    data = models.TextField()
    # TODO: make ActionFormData.data a serialized field
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = ("action_form", "user",)

    def __unicode__(self):
        return u"%s is working on %s" % (self.user, self.action_form)

"""
SIGNALS!
"""
def update_action_aggregates(sender, instance, **kwargs):
    instance.action.users_completed = UserActionProgress.objects.filter(action=instance.action, is_completed=True).count()
    instance.action.users_committed = UserActionProgress.objects.filter(action=instance.action, date_committed__isnull=False).count()
    instance.action.save()
models.signals.post_save.connect(update_action_aggregates, sender=UserActionProgress)

def apply_changes_from_commitment_cards(sender, request, user, is_new_user, **kwargs):
    changes = Action.objects.process_commitment_card(user, new_user=is_new_user)
    if changes:
        messages.success(request, "%s actions were applied to your account from a commitment card" % len(changes),
            extra_tags="sticky")
logged_in.connect(apply_changes_from_commitment_cards)