def boost2_confirm(request): """ Boost your Spark step 2/2 confirmation. This view saves the parent-child relationship in the user tree. """ profile = request.user.profile ajax = request.is_ajax() if profile.boost2_completed: return HttpResponseRedirect(reverse('mobile.home')) username = request.POST.get('parent') no_parent = request.POST.get('no_parent') if username or no_parent: error = False if not no_parent: try: parent = User.objects.get(username=username) created = create_relationship(parent, request.user) if created: profile.no_parent = False profile.save() # Update 'longest chain' stat of all ancestors if necessary profile.update_ancestors_longest_chain() # Add a share for the parent SharingHistory.add_share(parent.profile) # Add a share between cities of this user and the parent user CitySharingHistory.add_share_from_profiles(parent.profile, profile) # Trigger challenge completion for the parent update_completed_challenges.delay(parent.id) else: error = True except User.DoesNotExist: error = True if not error: profile = request.user.profile profile.parent_username = username profile.boost2_completed = True profile.save() # Don't use a celery task here so that "+{n} new" notification # has the correct value in the mobile menu on the next page. # This requires to award badges synchronously for this particular step. update_completed_challenges(profile.user.id) if ajax: return {'status': 'success', 'url': reverse('desktop.parent_info')} else: return HttpResponseRedirect(reverse('mobile.home')) else: if ajax: return {'status': 'error'} return jingo.render(request, 'spark/handlers/mobile/400.html', status=400)
def test_complete_1_1(self): profile = self.get_profile('bob') eq_(0, profile.total_shares) SharingHistory.add_share(profile) profile = self.get_profile('bob') eq_(1, profile.total_shares) self.assert_completion(profile, '1_1')
def test_complete_2_7(self): profile = self.get_profile('bob') eq_(0, profile.total_shares) for i in range(13): SharingHistory.add_share(profile) eq_(13, profile.total_shares) self.assert_completion(profile, '2_7')
def _add_share_to_user(request, username, via): try: user = User.objects.get(username=username) tz_offset = request.GET.get('tz', None) if via == 't': SharingHistory.add_share_from_twitter(user.profile, tz_offset) elif via == 'fb': SharingHistory.add_share_from_facebook(user.profile, tz_offset) elif via == 'qr': SharingHistory.add_share_from_qr_code(user.profile, tz_offset) elif via == 'p': SharingHistory.add_share_from_poster(user.profile, tz_offset) else: SharingHistory.add_share(user.profile, tz_offset) # Gaining a share triggers detection of completed challenges _trigger_challenge_detection(username) return username except User.DoesNotExist: # Ignore 'shared_by' cookies if they have been tampered with # or if the parent user has deleted their account in the meantime. return None
def shares_over_time(self): """Aggregate data of Spark shares since the start of the campaign. Used by the 'shares over time' diagram in the user dashboard. """ from stats.models import SharingHistory return SharingHistory.get_shares_over_time(self)
def is_completed_by(self, profile): return SharingHistory.has_gained_shares_via(profile, VIA_QR)
def is_completed_by(self, profile): max_share_count = SharingHistory.get_max_share_count() profile_share_count = SharingHistory.get_num_shares(profile) return max_share_count == profile_share_count
def test_complete_2_4(self): profile = self.get_profile('bob') eq_(0, profile.total_shares) SharingHistory.add_share_from_qr_code(profile) self.assert_completion(profile, '2_4')
def test_complete_3_2(self): profile = self.get_profile('bob') eq_(0, profile.total_shares) SharingHistory.add_share_from_poster(profile) self.assert_completion(profile, '3_2')