Example #1
0
    def handle(self, *args, **options):
        user = first(User.objects.filter(username='demo')[:1])

        if user:
            user.delete()

        user = User(username='demo', email='demo')
        user.set_password('demo')
        user.save()

        site = first(Site.objects.filter(identifier='demo')[:1])

        if site:
            site.delete()

        site = Site(name='Sample demo for Wham', identifier='demo', secret_key='asdf1234', url='http://example.com')
        site.save()
        site.users.add(user)
        site.save()

        for x in range(1, 10):
            email = 'user' + str(x) + '@example.com'
            authenticated_site_user = AuthenticatedSiteUser(user_id=str(x), email=email, username='User ' + str(x), number_of_visits=9, number_of_sessions=9)
            authenticated_site_user.save()

            site_user = SiteUser(site=site, authenticated_site_user=authenticated_site_user, sf_user_id=Site.generate_identifier(), number_of_visits=9, number_of_sessions=9)
            site_user.save()

            for y in range(1, 10):
                created_datetime = (timezone.now() - timedelta(minutes=y + randint(1, 9)))

                visit = Visit(created=created_datetime, site_user=site_user, ip_address='127.0.0.1', user_id=str(x), email=email, username='User ' + str(x), url='http://example.com/' + str(randint(44, 88)) + '.html', referrer='http://google.com/')
                visit.save()

                if y == 1:
                    site_user.first_visit = visit
                    authenticated_site_user.first_visit = visit

                    session = Session(visit=visit)
                    session.save()
                    site_user.first_session = session
                    authenticated_site_user.first_session = session
                elif y == 9:
                    site_user.last_visit = visit
                    authenticated_site_user.last_visit = visit

                    session = Session(visit=visit)
                    session.save()
                    site_user.last_session = session
                    authenticated_site_user.last_session = session

            site_user.save()
            authenticated_site_user.save()

        self.stdout.write('Demo data is loaded.')
Example #2
0
def log(request):
    wham = json.loads(request.body)
    site_identifier = wham.get("siteIdentifier", None)

    if not site_identifier:
        return {"success": False, "error_code": "missing_site_identifier"}

    site = first(Site.objects.filter(identifier=site_identifier)[:1])

    if not site:
        return {"success": False, "error_code": "invalid_site_identifier"}

    origin = request.META.get("HTTP_ORIGIN", None)

    if not origin:
        return {"success": False, "error_code": "missing_site_origin"}

    origin_domain = urlparse(origin).netloc
    site_domain = urlparse(site.url).netloc

    if not settings.DEBUG and origin_domain != site_domain:
        logger.error(
            "Invalid origin domain %s for site domain %s for site id %s" % (origin_domain, site_domain, site.id)
        )
        return {"success": False, "error_code": "invalid_site_origin"}

    # TODO: Check for encrypted user information
    # TODO: Decrypt information as necessary

    sf_user_id = wham.get("id", None)

    if not sf_user_id:
        return {"success": False, "error_code": "missing_identifier"}

    url = wham.get("url", None)
    referrer = wham.get("referrer", None)

    user_dict = wham.get("user", {})
    user_id = user_dict.get("id", None)
    email = user_dict.get("email", None)
    username = user_dict.get("username", None)

    site_user = None

    if user_id != None:
        # Get the SiteUser by user id.
        site_user = first(
            SiteUser.objects.select_related("authenticated_site_user").filter(
                site=site, authenticated_site_user__user_id=user_id
            )[:1]
        )

        if site_user:
            previous_site_user = first(
                SiteUser.objects.select_related("authenticated_site_user").filter(site=site, sf_user_id=sf_user_id)[:1]
            )

            if (
                previous_site_user
                and previous_site_user.authenticated_site_user is None
                and site_user.sf_user_id != previous_site_user.sf_user_id
            ):
                # User was previously unauthenticated, but then authenticates.
                # Associate the unauthenticated SiteUser with the authenticated user id.
                previous_site_user.authenticated_site_user = site_user.authenticated_site_user
                previous_site_user.save()
        else:
            site_user = first(
                SiteUser.objects.select_related("authenticated_site_user").filter(site=site, sf_user_id=sf_user_id)[:1]
            )

            if site_user and site_user.authenticated_site_user and site_user.authenticated_site_user.user_id != user_id:
                # User is authenticated, but user id does not match what is expected based on sf_user_id.
                # Create new AuthenticatedSiteUser and SiteUser with new sf_user_id.
                authenticated_site_user = AuthenticatedSiteUser(user_id=user_id, email=email, username=username)
                authenticated_site_user.save()

                sf_user_id = str(uuid4())
                site_user = SiteUser(site=site, sf_user_id=sf_user_id, authenticated_site_user=authenticated_site_user)
            else:
                authenticated_site_user = AuthenticatedSiteUser(user_id=user_id, email=email, username=username)
                authenticated_site_user.save()

                if not site_user:
                    site_user = SiteUser(site=site, sf_user_id=sf_user_id)

                site_user.authenticated_site_user = authenticated_site_user
    else:
        # Get the SiteUser by sf_user_id.
        site_user = first(
            SiteUser.objects.select_related("authenticated_site_user").filter(site=site, sf_user_id=sf_user_id)[:1]
        )

        if site_user and site_user.authenticated_site_user and site_user.authenticated_site_user.user_id != user_id:
            # User for the sf_user_id has been authenticated, but the user id has changed.
            # Create a new sf_user_id and SiteUser.
            sf_user_id = str(uuid4())
            site_user = SiteUser(site=site, sf_user_id=sf_user_id)

        if not site_user:
            site_user = SiteUser(site=site, sf_user_id=sf_user_id)

    site_user.save()
    visit = Visit(
        site_user=site_user,
        ip_address=_get_client_ip(request),
        user_id=user_id,
        email=email,
        username=username,
        url=url,
        referrer=referrer,
    )
    visit.save()

    authenticated_site_user = site_user.authenticated_site_user

    # Determine if this is a new session.
    if not site_user.last_visit or site_user.last_visit.created < (timezone.now() - timedelta(minutes=30)):
        session = Session(visit=visit)
        session.save()

        # Set session metadata for non-authenticated site users.
        if not site_user.first_session:
            site_user.first_session = session

        site_user.last_session = session
        site_user.number_of_sessions = site_user.number_of_sessions + 1

        # Set session metadata for authenticated site users.
        if authenticated_site_user:
            if not authenticated_site_user.first_session:
                authenticated_site_user.first_session = session
                authenticated_site_user.number_of_sessions = 1
            else:
                authenticated_site_user.number_of_sessions = authenticated_site_user.number_of_sessions + 1

            authenticated_site_user.last_session = session

    # Set visit metadata for non-authenticated site users.
    if not site_user.first_visit:
        site_user.first_visit = visit

    site_user.last_visit = visit
    site_user.number_of_visits = site_user.number_of_visits + 1
    site_user.save()

    # Set visit metadata for authenticated site users.
    if authenticated_site_user:
        if not authenticated_site_user.first_visit:
            authenticated_site_user.first_visit = visit
            authenticated_site_user.number_of_visits = 1
        else:
            authenticated_site_user.number_of_visits = authenticated_site_user.number_of_visits + 1

        authenticated_site_user.last_visit = visit
        authenticated_site_user.save()

    return {"success": True, "id": site_user.sf_user_id}
Example #3
0
def create_session(request):
    if not request.user.is_authenticated():
        return redirect('/login/?next=%s' % request.path)
    else:
        if request.method =='POST': # If the form has been submitted...
            form1 = SessionForm(request.POST, prefix="form1") # form bound to the POST data
            form2 = PerformanceDistanceForm(request.POST, prefix="form2")
            if form1.is_valid() and form2.is_valid(): # All validation rules pass
                f = Session(squad=form1.cleaned_data['squad'],
                            name=form1.cleaned_data['name'],
                            location=form1.cleaned_data['location'],
                            description=form1.cleaned_data['description'],
                            date=form1.cleaned_data['date'],
                            slot1_time=form1.cleaned_data['slot1_time'],
                            slot2_time=form1.cleaned_data['slot2_time'],
                            slot3_time=form1.cleaned_data['slot3_time'],
                            max_rowers1=form1.cleaned_data['max_rowers1'],
                            max_rowers2=form1.cleaned_data['max_rowers2'],
                            max_rowers3=form1.cleaned_data['max_rowers3'],
                            senior_boolean1=form1.cleaned_data['senior_boolean1'],
                            senior_boolean2=form1.cleaned_data['senior_boolean2'],
                            senior_boolean3=form1.cleaned_data['senior_boolean3']
                            )
                f.save()
                s = Session.objects.get(id=f.id)
                squad = form1.cleaned_data['squad']
                rower_list = squad.rower_set.all()[0:]
                for rower in rower_list:
                    p = Performance(session = s,
                                    rower= rower,
                                    set_distance1 = form2.cleaned_data['set_distance1'],
                                    set_distance2= form2.cleaned_data['set_distance2'],
                                    set_distance3= form2.cleaned_data['set_distance3'],
                                    set_distance4= form2.cleaned_data['set_distance4'],
                                    set_distance5= form2.cleaned_data['set_distance5'],
                                    set_distance6= form2.cleaned_data['set_distance6'],
                                    set_time1= form2.cleaned_data['set_time1'],
                                    set_time2= form2.cleaned_data['set_time2'],
                                    set_time3= form2.cleaned_data['set_time3'],
                                    set_time4= form2.cleaned_data['set_time4'],
                                    set_time5= form2.cleaned_data['set_time5'],
                                    set_time6= form2.cleaned_data['set_time6'],
                                    set_rate_cap1= form2.cleaned_data['set_rate_cap1'],
                                    set_rate_cap2= form2.cleaned_data['set_rate_cap2'],
                                    set_rate_cap3= form2.cleaned_data['set_rate_cap3'],
                                    set_rate_cap4= form2.cleaned_data['set_rate_cap4'],
                                    set_rate_cap5= form2.cleaned_data['set_rate_cap5'],
                                    set_rate_cap6= form2.cleaned_data['set_rate_cap6'],
                                    set_heart_zone1= form2.cleaned_data['set_heart_zone1'],
                                    set_heart_zone2= form2.cleaned_data['set_heart_zone2'],
                                    set_heart_zone3= form2.cleaned_data['set_heart_zone3'],
                                    set_heart_zone4= form2.cleaned_data['set_heart_zone4'],
                                    set_heart_zone5= form2.cleaned_data['set_heart_zone5'],
                                    set_heart_zone6= form2.cleaned_data['set_heart_zone6'],
                                    set_rest_time1= form2.cleaned_data['set_rest_time1'],
                                    set_rest_time2= form2.cleaned_data['set_rest_time2'],
                                    set_rest_time3= form2.cleaned_data['set_rest_time3'],
                                    set_rest_time4= form2.cleaned_data['set_rest_time4'],
                                    set_rest_time5= form2.cleaned_data['set_rest_time5'],
                                    set_rest_time6= form2.cleaned_data['set_rest_time6']
                                    )
                    p.save()

                # Now we create a carbon copy of the set performances to add to future rowers that may be added to the squad list

                pc = PerformanceCopy(session=s,
                                    set_distance1 = form2.cleaned_data['set_distance1'],
                                    set_distance2= form2.cleaned_data['set_distance2'],
                                    set_distance3= form2.cleaned_data['set_distance3'],
                                    set_distance4= form2.cleaned_data['set_distance4'],
                                    set_distance5= form2.cleaned_data['set_distance5'],
                                    set_distance6= form2.cleaned_data['set_distance6'],
                                    set_time1= form2.cleaned_data['set_time1'],
                                    set_time2= form2.cleaned_data['set_time2'],
                                    set_time3= form2.cleaned_data['set_time3'],
                                    set_time4= form2.cleaned_data['set_time4'],
                                    set_time5= form2.cleaned_data['set_time5'],
                                    set_time6= form2.cleaned_data['set_time6'],
                                    set_rate_cap1= form2.cleaned_data['set_rate_cap1'],
                                    set_rate_cap2= form2.cleaned_data['set_rate_cap2'],
                                    set_rate_cap3= form2.cleaned_data['set_rate_cap3'],
                                    set_rate_cap4= form2.cleaned_data['set_rate_cap4'],
                                    set_rate_cap5= form2.cleaned_data['set_rate_cap5'],
                                    set_rate_cap6= form2.cleaned_data['set_rate_cap6'],
                                    set_heart_zone1= form2.cleaned_data['set_heart_zone1'],
                                    set_heart_zone2= form2.cleaned_data['set_heart_zone2'],
                                    set_heart_zone3= form2.cleaned_data['set_heart_zone3'],
                                    set_heart_zone4= form2.cleaned_data['set_heart_zone4'],
                                    set_heart_zone5= form2.cleaned_data['set_heart_zone5'],
                                    set_heart_zone6= form2.cleaned_data['set_heart_zone6'],
                                    set_rest_time1= form2.cleaned_data['set_rest_time1'],
                                    set_rest_time2= form2.cleaned_data['set_rest_time2'],
                                    set_rest_time3= form2.cleaned_data['set_rest_time3'],
                                    set_rest_time4= form2.cleaned_data['set_rest_time4'],
                                    set_rest_time5= form2.cleaned_data['set_rest_time5'],
                                    set_rest_time6= form2.cleaned_data['set_rest_time6']
                                    )
                pc.save()

                return HttpResponseRedirect('/tracker/created/') # Redirect after POST
        else:
            form1 = SessionForm(prefix="form1")  # An unbound form
            form2 = PerformanceDistanceForm(prefix="form2")

        return render(request, 'tracker/create_session.html', {"form1": form1, 'form2': form2})