def isMatch(Person1, Person2): '''Return True if Person2 has already submitted a crush on Person1. Call this function when we receive a crush from Person1 on Person2 (crush in the opposite direction), to see if this is a match. ''' digest = crush_digest(Person2, Person1) crush_hashes = CrushHash.objects.filter(active=True, digest=digest) return len(crush_hashes) > 0
def submit(request): form = RegisterForm(request.POST) if form.is_valid(): print('form is valid') if not 'email' in request.session: return redirect('%s/need_certificate' % HOSTNAME) person = Person.objects.get( email = request.session['email'] ) num_allowed = person.num_allowed_crushes if num_allowed < 0: num_allowed = Crush.num_allowed_crushes num_submitted = 0 for i in range(Crush.num_allowed_crushes): crush_email = form.cleaned_data['Crush_email_%d' % (i+1)] if crush_email != '': num_submitted += 1 try: crush = Person.objects.get(email=crush_email) except: variables = RequestContext(request, { 'invalid': crush_email }) return render_to_response('crush_connector/invalid.html', variables) num_left = num_allowed - person.num_crushes_used next_refresh = RefreshDates.objects.filter(date__gte = datetime.today()).order_by('date')[0] variables = RequestContext(request, { 'num_left': num_left, 'num_allowed': num_allowed, 'num_used': person.num_crushes_used, 'refresh_date': next_refresh }) crushes = CrushHash.objects.filter(crusher=person) if num_submitted > num_left and len(crushes) > 0: last_submission = crushes[0].timestamp last_refresh = RefreshDates.objects.filter(date__lte = datetime.today()).order_by('-date')[0] if last_refresh.date > last_submission.date(): for crush in crushes: crush.active = False crush.save() person.num_crushes_used = 0 else: # too many, not allowed to submit this many crushes # throw error page, tell them to go back and submit fewer and wait til refresh date to submit more return render_to_response('crush_connector/over_limit.html', variables) matches = [] for i in range(Crush.num_allowed_crushes): crush_email = form.cleaned_data['Crush_email_%d' % (i+1)] if crush_email == '': continue crush_person, created = Person.objects.get_or_create( email = crush_email ) if created: print('creating new person for the crush') crush_person.name = '__no_name__ %s' % crush_email crush_person.save() digest = crush_digest(person, crush_person) now = datetime.now() crush_hash = CrushHash(crusher=person, digest=digest, timestamp=now) crush_hash.save() crush = Crush(crusher=person, crushee=crush_person, timestamp=now) crush.save() person.num_crushes_used += 1 person.save() if confirmCrushAndEmail(person, crush_person): print('match! check your email') matches.append(crush_person) mutual = MutualCrushHash(crush_hash=crush_hash) mutual.save() num_left = num_left - num_submitted variables = RequestContext(request, { 'num_left': num_left, 'num_allowed': num_allowed, 'num_used': person.num_crushes_used, 'refresh_date': next_refresh }) return render_to_response('crush_connector/validate.html', variables) else: variables = RequestContext(request, {'form': form, 'hostname': HOSTNAME}) return render_to_response('crush_connector/connect.html', variables)