本文整理汇总了Python中models.Room.get_absolute_url方法的典型用法代码示例。如果您正苦于以下问题:Python Room.get_absolute_url方法的具体用法?Python Room.get_absolute_url怎么用?Python Room.get_absolute_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Room
的用法示例。
在下文中一共展示了Room.get_absolute_url方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_anonymous_access
# 需要导入模块: from models import Room [as 别名]
# 或者: from models.Room import get_absolute_url [as 别名]
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: create_room
# 需要导入模块: from models import Room [as 别名]
# 或者: from models.Room import get_absolute_url [as 别名]
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))