예제 #1
0
    def test_anonymous_access(self):
        anon_room = Room(
                        allow_anonymous_access=True,
                        name="Anonymous Room",
                        slug="anonymous-room")
        login_req_room = Room(
                        allow_anonymous_access=False,
                        name="Login required room",
                        slug="login-required-room")
        anon_room.save()
        login_req_room.save()

        client = Client()

        response = client.get(login_req_room.get_absolute_url())
        # a login view may not have been implemented, so assertRedirects fails
        self.assertEquals(response.status_code, 302)
        url = response['Location']
        expected_url = get_login_url(login_req_room.get_absolute_url())
        e_scheme, e_netloc, e_path, e_query, e_fragment = urlparse.urlsplit(
                                                                expected_url)
        if not (e_scheme or e_netloc):
            expected_url = urlparse.urlunsplit(('http', 'testserver', e_path,
                e_query, e_fragment))
        self.assertEquals(url, expected_url)

        response = client.get(
            anon_room.get_absolute_url(),
            follow=True)

        # assert redirect
        self.assertRedirects(
            response,
            'http://testserver/chat/setguestname/?room_slug=anonymous-room')

        # post guestname
        guestname_posted = client.post(
            response.redirect_chain[0][0],
            {'guest_name': 'guest',
             'room_slug': 'anonymous-room'},
            follow=True)
        self.assertRedirects(
            guestname_posted,
            anon_room.get_absolute_url()
        )
예제 #2
0
def create_room(request, extra_context={}):
	"""
	View for creating a room. Uses a clever combination of PollForm, RoomForm and ChoiceFormSet to achieve
	3-way model creation:
		- PollForm allows the user to specify the question
		- ChoiceFormSet allows the user to specify an arbitrary number of "choices" to go with the question
			(each one represented by its own DB object)
		- RoomForm gives more advanced "tweaks" for the room, for example:
			- period length (how long each period lasts, default is 30)
			- join threshold (the amount of time that a room is in lock mode before a poll begins)
	"""
	if request.method == "POST":
		# if the user has submitted the form, get the data from all three
		poll_form = PollForm(request.POST, request.FILES)
		choice_formset = ChoiceFormSet(request.POST, request.FILES)
		room_form = RoomForm(request.POST, request.FILES)
		
		if poll_form.is_valid() and choice_formset.is_valid() and room_form.is_valid():
			# if all 3 forms are valid, create a new question object and save it
			q = Question(text=poll_form.cleaned_data['question'])
			q.save()
			
			# create a new poll object that points to the question, and save it
			p = Poll(question=q)
			p.save()
			
			# for every choice the user has inputted
			for form in choice_formset.forms:
				# create a new choice object, and point it at the question created earlier
				c = Choice(question=q, text=form.cleaned_data['choice'])
				c.save()
			
			# finally, create the room itself, pointing to the question object, with the creator of the
			# currently logged in user, and the period length & join threshold as specified in the form
			# data.
			r = Room(question=q, opened_by=request.user, controller=request.user,
				period_length=room_form.cleaned_data['period_length'],
				join_threshold=room_form.cleaned_data['join_threshold'])
			r.save()
			
			# redirect the user to their newly created room
			return HttpResponseRedirect(r.get_absolute_url())
	else:
		# if the user has not submitted the form (i.e. wishes to fill the form in)
		# then initialise the 3 forms with no data
		poll_form = PollForm()
		choice_formset = ChoiceFormSet()
		room_form = RoomForm()
	
	# put the forms into a context dictionary (and update that with any extra context we have been given)
	data = {'poll_form': poll_form, 'choice_formset': choice_formset, 'room_form': room_form}
	data.update(extra_context)
	
	# render the page
	return render_to_response('rooms/room_form.html', data, context_instance=RequestContext(request))
예제 #3
0
    def test_anonymous_access(self):
        anon_room = Room(allow_anonymous_access=True,
                         name="Anonymous Room",
                         slug="anonymous-room")
        login_req_room = Room(allow_anonymous_access=False,
                              name="Login required room",
                              slug="login-required-room")
        anon_room.save()
        login_req_room.save()

        client = Client()

        response = client.get(login_req_room.get_absolute_url())
        # a login view may not have been implemented, so assertRedirects fails
        self.assertEquals(response.status_code, 302)
        url = response['Location']
        expected_url = get_login_url(login_req_room.get_absolute_url())
        e_scheme, e_netloc, e_path, e_query, e_fragment = urlparse.urlsplit(
            expected_url)
        if not (e_scheme or e_netloc):
            expected_url = urlparse.urlunsplit(
                ('http', 'testserver', e_path, e_query, e_fragment))
        self.assertEquals(url, expected_url)

        response = client.get(anon_room.get_absolute_url(), follow=True)

        # assert redirect
        self.assertRedirects(
            response,
            'http://testserver/chat/setguestname/?room_slug=anonymous-room')

        # post guestname
        guestname_posted = client.post(response.redirect_chain[0][0], {
            'guest_name': 'guest',
            'room_slug': 'anonymous-room'
        },
                                       follow=True)
        self.assertRedirects(guestname_posted, anon_room.get_absolute_url())
예제 #4
0
def create_room(request, extra_context={}):
    """
	View for creating a room. Uses a clever combination of PollForm, RoomForm and ChoiceFormSet to achieve
	3-way model creation:
		- PollForm allows the user to specify the question
		- ChoiceFormSet allows the user to specify an arbitrary number of "choices" to go with the question
			(each one represented by its own DB object)
		- RoomForm gives more advanced "tweaks" for the room, for example:
			- period length (how long each period lasts, default is 30)
			- join threshold (the amount of time that a room is in lock mode before a poll begins)
	"""
    if request.method == "POST":
        # if the user has submitted the form, get the data from all three
        poll_form = PollForm(request.POST, request.FILES)
        choice_formset = ChoiceFormSet(request.POST, request.FILES)
        room_form = RoomForm(request.POST, request.FILES)

        if poll_form.is_valid() and choice_formset.is_valid(
        ) and room_form.is_valid():
            # if all 3 forms are valid, create a new question object and save it
            q = Question(text=poll_form.cleaned_data['question'])
            q.save()

            # create a new poll object that points to the question, and save it
            p = Poll(question=q)
            p.save()

            # for every choice the user has inputted
            for form in choice_formset.forms:
                # create a new choice object, and point it at the question created earlier
                c = Choice(question=q, text=form.cleaned_data['choice'])
                c.save()

            # finally, create the room itself, pointing to the question object, with the creator of the
            # currently logged in user, and the period length & join threshold as specified in the form
            # data.
            r = Room(question=q,
                     opened_by=request.user,
                     controller=request.user,
                     period_length=room_form.cleaned_data['period_length'],
                     join_threshold=room_form.cleaned_data['join_threshold'])
            r.save()

            # redirect the user to their newly created room
            return HttpResponseRedirect(r.get_absolute_url())
    else:
        # if the user has not submitted the form (i.e. wishes to fill the form in)
        # then initialise the 3 forms with no data
        poll_form = PollForm()
        choice_formset = ChoiceFormSet()
        room_form = RoomForm()

    # put the forms into a context dictionary (and update that with any extra context we have been given)
    data = {
        'poll_form': poll_form,
        'choice_formset': choice_formset,
        'room_form': room_form
    }
    data.update(extra_context)

    # render the page
    return render_to_response('rooms/room_form.html',
                              data,
                              context_instance=RequestContext(request))