def chinese_valentines_day_participate(request): currentCampaign = Campaign.objects.get(slug=chineseValentinesDayCampaignSlug) data = {} if request.method == 'POST': if 'running' == currentCampaign.status: email = request.POST['subscriber'] if not Participation.objects.filter(activity=currentCampaign, note=email).exists(): user = User.objects.get(pk=1) participation = Participation(activity=currentCampaign, participant=user, note=email, status='attend') participation.save() data['result'] = 'success' else: data['result'] = 'failed' else: data['result'] = 'failed' else: data['result'] = 'failed' response = json.dumps(data) return HttpResponse(response, mimetype='application/x-javascript')
def event_register(request, event_slug): currentEvent = Event.objects.get(slug=event_slug, status__in=('preparing', 'enrolling', 'enrolled')) currentUser = auth.get_user(request) data = {'event': currentEvent} if request.method == 'POST': if not request.user.is_authenticated(): raise PermissionDenied is_update = Person.objects.filter(user=request.user).exists() if is_update: form = PersonForm(request.POST, instance=Person.objects.get(user=request.user)) else: form = PersonForm(request.POST) data['form'] = form if form.is_valid(): person = form.save(commit=False) if is_update: person.save() else: person.user = currentUser person.save() if not Participation.objects.filter(activity=currentEvent, participant=currentUser).exists(): participation = Participation(activity=currentEvent, participant=currentUser, status='attend') participation.save() data['registered'] = True elif request.user.is_authenticated(): if Person.objects.filter(user=currentUser).exists(): data['form'] = PersonForm(instance=currentUser.get_profile()) else: fbAuth = UserSocialAuth.objects.filter(user=currentUser, provider='facebook') initData = {} if fbAuth.exists(): token = fbAuth.get().tokens['access_token'] if token: graph = facebook.GraphAPI(token) me = graph.get_object('me', locale='zh_TW') if 'name' in me: initData['fullname'] = me['name'] if 'gender' in me and me['gender'] in genderMap.keys(): initData['gender'] = genderMap[me['gender']] data['form'] = PersonForm(initial=initData) return render(request, 'event/event_register.html', data)
def campaign_claim_award(request, campaign_slug, nav_template=None, award_name=None): print campaign_slug currentCampaign = Campaign.objects.get(slug=campaign_slug, status='result') awarded = None if request.user.is_active: currentUser = auth.get_user(request) if award_name: currentAwards = Award.objects.filter(~Q(price=None) & ~Q(price__name='sorry'), activity=currentCampaign, winner=currentUser, name=award_name) if currentUser.email: unregCurrentAwards = Award.objects.filter(activity=currentCampaign, winner_extra=currentUser.email, name=award_name) else: unregCurrentAwards = [] else: currentAwards = Award.objects.filter(~Q(price=None) & ~Q(price__name='sorry'), activity=currentCampaign, winner=currentUser) if currentUser.email: unregCurrentAwards = Award.objects.filter(activity=currentCampaign, winner_extra=currentUser.email) else: unregCurrentAwards = [] awarded = currentAwards.exists() or unregCurrentAwards.exists() data = {'campaign': currentCampaign, 'awarded': awarded, 'nav_template': nav_template, 'MOCO_URL': MOCO_URL} if request.method == 'POST': if not request.user.is_authenticated(): raise PermissionDenied is_update = Person.objects.filter(user=request.user).exists() if is_update: form = AwardClaimForm(request.POST, instance=Person.objects.filter(user=request.user).latest('user')) else: form = AwardClaimForm(request.POST) data['form'] = form if form.is_valid(): person = form.save(commit=False) if is_update: person.save() else: person.user = currentUser person.save() for currentAward in currentAwards: currentAward.status = 'claimed' currentAward.save() for unregCurrentAward in unregCurrentAwards: unregCurrentAward.status = 'claimed' unregCurrentAward.winner = currentUser unregCurrentAward.save() if not Participation.objects.filter(Q(activity=currentCampaign) & Q(Q(participant=currentUser) | Q(note=currentUser.email))).exists(): participation = Participation(activity=currentCampaign, participant=currentUser, status='attend') participation.save() data['registered'] = True elif request.user.is_active: profiles = Person.objects.filter(user=currentUser) if profiles.exists(): data['form'] = AwardClaimForm(instance=profiles.latest('user')) else: fbAuth = UserSocialAuth.objects.filter(user=currentUser, provider='facebook') initData = {} if fbAuth.exists(): token = fbAuth.latest('uid').tokens['access_token'] if token: graph = facebook.GraphAPI(token) me = graph.get_object('me', locale='zh_TW') if 'name' in me: initData['fullname'] = me['name'] if 'gender' in me and me['gender'] in genderMap.keys(): initData['gender'] = genderMap[me['gender']] data['form'] = AwardClaimForm(initial=initData) return render(request, 'event/campaign_claim_award.html', data)