def setUp(self): self.user = User(username="******", password="******") self.user.save() self.question = "Is Foo the new bar??" self.pitch = "Foo is better!" self.guid = create_poll_guid(self.question) self.poll = Poll(guid=self.guid, owner=self.user, question=self.question, pitch_a=self.pitch, last_modified=datetime.now()) self.poll.save()
def test_GetPoll(self): testslug = "is-foo-the-new-bar" testguid = create_poll_guid(testslug) poll = Poll.objects.get(guid=testguid) pitch = poll.pitch_set.get(editor=self.user) self.assertEquals(self._question, poll.question) self.assertEquals(self.user, poll.owner) self.assertEquals(self._content, pitch.content)
def testGetPoll(self): testslug = "is-foo-the-new-bar" testguid = create_poll_guid(testslug) getpoll = Poll.objects.get(guid=testguid) self.assertEquals(self.question, getpoll.question) self.assertEquals(self.user, getpoll.owner) self.assertEquals(self.pitch, getpoll.pitch_a) self.assertEquals("", getpoll.pitch_b)
def poll(request, question): #TODO: check if user is logged in, then enable chat, etc. question_guid = create_poll_guid(question) try: poll = Poll.objects.get(guid=question_guid) except Poll.DoesNotExist: raise Http404 #TODO optimize queries: votes_a = Vote.objects.filter(pitch__poll__guid=question_guid, choice="a").count() votes_b = Vote.objects.filter(pitch__poll__guid=question_guid, choice="b").count() pitch_a,_ = Pitch.objects.get_or_create(poll=poll, choice_id="a", defaults={"content":"", 'editor':request.user}) pitch_b,_ = Pitch.objects.get_or_create(poll=poll, choice_id="b", defaults={"content":"", 'editor':request.user}) args = {"poll":poll, "pitch_a":pitch_a, "pitch_b":pitch_b, "votes_a":votes_a, "votes_b":votes_b, "user":request.user, "STOMP_PORT":settings.STOMP_PORT, "CHANNEL_NAME":question_guid, "HOST":settings.INTERFACE, "SESSION_COOKIE_NAME":settings.SESSION_COOKIE_NAME} return render_to_response('polls/poll.html', args)
def setUp(self): self.user = User.objects.create_user("user1", "*****@*****.**", password="******") self.user.save() self.second_user = User.objects.create_user("user2", "*****@*****.**", password="******") self.second_user.save() self._question = "Is Foo the new Bar?" self._guid = create_poll_guid(self._question) self._choice = "a" self._content = "No, Foo will always be tops!" self.poll = Poll(guid=self._guid, owner=self.user, question=self._question) self.poll.save() self.pitch = Pitch(poll=self.poll, content=self._content, choice_id=self._choice, editor=self.user) self.pitch.save() self.pitch.vote() #User who creates Pitch automatically Votes for it.
def new(request): """ Create a new Poll, with 1 initial "Pitch" for a given choice. The "Pitch" is a short blurb on why you should choice a given choice. The Pitch that the User fills out determines that User's choice for this particular Poll. TODO: Write Unit tests """ if request.method == 'POST': poll_form = PollForm(request.POST) pitch_form = PitchForm(request.POST) if poll_form.is_valid() and pitch_form.is_valid(): poll_inst = poll_form.save(commit=False) question = poll_form.cleaned_data["question"] if question in DISALLOWED_QUESTIONS: poll_form.errors.extra = "Invalid Question, please try a different one." if not hasattr(poll_form.errors, "extra"): poll_inst.owner = request.user poll_inst.last_modified = datetime.now() poll_inst.guid = create_poll_guid(question) try: poll_inst.save() #TODO add a function to Pitch to make this cleaner: pitch_inst = pitch_form.save(commit=False) pitch_inst.poll = poll_inst pitch_inst.choice_id = "a" pitch_inst.editor = poll_inst.owner pitch_inst.save() pitch_inst.vote() return HttpResponseRedirect('/') # Redirect after POST except IntegrityError: poll_form.errors.extra = "Your Question already exists, possibly created by another User." else: poll_form = PollForm() pitch_form = PitchForm() args = { "poll_form": poll_form, "pitch_form": pitch_form, "user": request.user } return render_to_response("polls/new.html", args)
def new(request): """ Create a new Poll, with 1 initial "Pitch" for a given choice. The "Pitch" is a short blurb on why you should choice a given choice. The Pitch that the User fills out determines that User's choice for this particular Poll. TODO: Write Unit tests """ if request.method == 'POST': poll_form = PollForm(request.POST) pitch_form = PitchForm(request.POST) if poll_form.is_valid() and pitch_form.is_valid(): poll_inst = poll_form.save(commit=False) question = poll_form.cleaned_data["question"] if question in DISALLOWED_QUESTIONS: poll_form.errors.extra = "Invalid Question, please try a different one." if not hasattr(poll_form.errors, "extra"): poll_inst.owner = request.user poll_inst.last_modified = datetime.now() poll_inst.guid = create_poll_guid(question) try: poll_inst.save() #TODO add a function to Pitch to make this cleaner: pitch_inst = pitch_form.save(commit=False) pitch_inst.poll = poll_inst pitch_inst.choice_id = "a" pitch_inst.editor = poll_inst.owner pitch_inst.save() pitch_inst.vote() return HttpResponseRedirect('/') # Redirect after POST except IntegrityError: poll_form.errors.extra = "Your Question already exists, possibly created by another User." else: poll_form = PollForm() pitch_form = PitchForm() args = {"poll_form":poll_form, "pitch_form":pitch_form, "user":request.user} return render_to_response("polls/new.html", args)
def poll(request, question): #TODO: check if user is logged in, then enable chat, etc. question_guid = create_poll_guid(question) try: poll = Poll.objects.get(guid=question_guid) except Poll.DoesNotExist: raise Http404 #TODO optimize queries: votes_a = Vote.objects.filter(pitch__poll__guid=question_guid, choice="a").count() votes_b = Vote.objects.filter(pitch__poll__guid=question_guid, choice="b").count() pitch_a, _ = Pitch.objects.get_or_create(poll=poll, choice_id="a", defaults={ "content": "", 'editor': request.user }) pitch_b, _ = Pitch.objects.get_or_create(poll=poll, choice_id="b", defaults={ "content": "", 'editor': request.user }) args = { "poll": poll, "pitch_a": pitch_a, "pitch_b": pitch_b, "votes_a": votes_a, "votes_b": votes_b, "user": request.user, "STOMP_PORT": settings.STOMP_PORT, "CHANNEL_NAME": question_guid, "HOST": settings.INTERFACE, "SESSION_COOKIE_NAME": settings.SESSION_COOKIE_NAME } return render_to_response('polls/poll.html', args)