Exemple #1
0
def edit_rooms(request, id=None):
    context = {'page_title': u'Salas', 'edit_name': 'room', 'list_title': u'Baias', 'list_edit_name': 'stall', 'header_name_list': stall_list_header, 'has_back': False, 'features':get_user_features(request)}
    t = get_template('edit.html')
    room = None
    form = RoomForm()
    try:
        if request.method == 'POST':
            form = RoomForm(request.POST)
            if form.is_valid():
                cd = form.cleaned_data
                room = _save_room(cd)
                messages.success(request, 'Sala salva com sucesso.')
                initial=room.__dict__
                initial['syndic'] = room.syndic
                form = RoomForm(initial=initial)

        elif id:
            room = Room.objects.get(id=id)
            initial=room.__dict__
            initial['syndic'] = room.syndic
            form = RoomForm(initial=initial)
    except Exception as e:
        log.error(e)
        messages.error(request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.')

    context = _set_room_form_context(room, form, context, request)
    return render_to_response('edit.html', context, context_instance=RequestContext(request))
Exemple #2
0
def enter_room(request):
    if request.method == 'POST':
        form = RoomForm(request.POST)
        if form.is_valid():
            try:
                form.save()
            except ValueError:
                return render_to_response('error.html', {
                    'error_type':
                    "Room",
                    'error_name':
                    "[" + form.cleaned_data['name'] + "]",
                    'error_info':
                    "Room name cannot be validated, most likely a duplicate room"
                },
                                          context_instance=RequestContext(
                                              request))
            return render_to_response(
                'thanks.html', {
                    'data_type': "Room",
                    'data_name': "[" + form.cleaned_data['name'] + "]",
                    'data_modification': "CREATED",
                    'enter_again': True
                },
                context_instance=RequestContext(request))
    else:
        form = RoomForm()
    return render_to_response('data_entry.html', {
        'form': form,
        'title': 'Create Room'
    },
                              context_instance=RequestContext(request))
Exemple #3
0
def view_room(request, room_id):
    room_id = int(room_id)
    try:
        room = Room.objects.get(pk=room_id)
    except Room.DoesNotExist:
        return render_to_response('error.html', 
                                 {'error_type': "View Room",
                                  'error_name': str(room_id),
                                  'error_info':"No such room"}, 
                                  context_instance=RequestContext(request))
    if request.method == 'POST':
        form = RoomForm(request.POST,instance=room)
        if form.is_valid():
            try:
               form.save()
            except ValueError:
                return render_to_response('error.html', 
                                         {'error_type': "Room",
                                          'error_name': "["+form.cleaned_data['name']+"]",
                                          'error_info':"Room name cannot be validated, most likely a non-existent room"}, 
                                          context_instance=RequestContext(request))
            return render_to_response('thanks.html', 
                                     {'data_type': "Room",
                                      'data_name': "["+form.cleaned_data['name']+"]"}, 
                                      context_instance=RequestContext(request))
    else:
        form = RoomForm(instance=room)
        links = [('/room/'+str(room_id)+'/delete/', 'Delete', True)]
        return render_to_response('data_entry.html', 
                                 {'form': form,
                                  'links': links,
                                  'title': "Viewing Room: %s"%(room.name)}, 
                                 context_instance=RequestContext(request))
Exemple #4
0
def edit(request, room):
    was_private = room.is_private

    if request.method == 'POST':
        form = RoomForm(request.POST, instance=room)

        if form.is_valid():
            form.save(commit=False)
            room.save()

            if room.is_private and not was_private:
                redis = create_redis_connection()
                redis.publish('web_channel', 'room_private:' + str(room.id))
                try:
                    redis.hdel(redis_room_key(room.id), *redis.hkeys(redis_room_key(room.id)))
                except:
                    pass

            return HttpResponseRedirect(request.get_full_path())
    else:
        form = RoomForm(instance=room)

    response_data = { 'form' : form }

    if room.is_private:
        response_data['invited_users'] = room.invited.order_by('username').all()
        response_data['users'] = User.objects.exclude(pk=room.owner.id).exclude(rooms__pk=room.id).order_by('username').all()

    return response_data
Exemple #5
0
def view_room(request, room_id):
    room_id = int(room_id)
    try:
        room = Room.objects.get(pk=room_id)
    except Room.DoesNotExist:
        return render_to_response('error.html', 
                                 {'error_type': "View Room",
                                  'error_name': str(room_id),
                                  'error_info':"No such room"}, 
                                  context_instance=RequestContext(request))
    if request.method == 'POST':
        form = RoomForm(request.POST,instance=room)
        if form.is_valid():
            try:
               form.save()
            except ValueError:
                return render_to_response('error.html', 
                                         {'error_type': "Room",
                                          'error_name': "["+form.cleaned_data['name']+"]",
                                          'error_info':"Room name cannot be validated, most likely a non-existent room"}, 
                                          context_instance=RequestContext(request))
            return render_to_response('thanks.html', 
                                     {'data_type': "Room",
                                      'data_name': "["+form.cleaned_data['name']+"]"}, 
                                      context_instance=RequestContext(request))
    else:
        form = RoomForm(instance=room)
        links = [('/room/'+str(room_id)+'/delete/', 'Delete', True)]
        return render_to_response('data_entry.html', 
                                 {'form': form,
                                  'links': links,
                                  'title': "Viewing Room: %s"%(room.name)}, 
                                 context_instance=RequestContext(request))
Exemple #6
0
 def post(self, request):
     bound_form = RoomForm(request.POST)
     if bound_form.is_valid():
         new_room = bound_form.save()
         print(new_room)
         return redirect(reverse('rooms_url'))
     return render(request,
                   'hostel/add_a_room.html',
                   context={'form': bound_form})
Exemple #7
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))
Exemple #8
0
def submitRoom(request):
    if request.method == "POST":
        f = RoomForm(request.POST)
        if f.is_valid():
            newRoom = Room(name=request.POST['name'], info=request.POST['info'])
            newRoom.save()
            return HttpResponseRedirect(reverse('juakstore:roomDetail', args=(newRoom.id,)))
        else:
            return render(request, 'juakstore/room_add.html', {'form' : f})
    else:
        return HttpResponseRedirect(reverse('juakstore:roomCreate'))
Exemple #9
0
def add_room(request):
    list_rooms = Room.objects.order_by('number')
    if request.POST:
        form = RoomForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/success/')
    else:
        form = RoomForm()
    args={}
    args.update(csrf(request))
    args['form']=form
    args['list_rooms'] = list_rooms
    return render(request,'add_room.html',args)
Exemple #10
0
def create(request):

    if request.method == 'POST':
        form = RoomForm(request.POST)

        if form.is_valid():
            room = form.save(commit=False)
            room.owner = request.user
            room.save()
            url = reverse('rooms.views.edit', args=[room.id])
            return HttpResponseRedirect(url)
    else:
        form = RoomForm()

    return { 'form' : form }
Exemple #11
0
def enter_room(request):
    if request.method == 'POST':
        form = RoomForm(request.POST)
        if form.is_valid():
            try:
                form.save()
            except ValueError:
                return render_to_response('error.html', 
                                         {'error_type': "Room",'error_name': "["+form.cleaned_data['name']+"]",
                                          'error_info':"Room name cannot be validated, most likely a duplicate room"}, 
                                          context_instance=RequestContext(request))
            return render_to_response('thanks.html', 
                                     {'data_type': "Room",
                                      'data_name': "["+form.cleaned_data['name']+"]",
                                      'data_modification': "CREATED",
                                      'enter_again': True}, 
                                      context_instance=RequestContext(request))
    else:
        form = RoomForm()
    return render_to_response('data_entry.html',
                             {'form': form, 'title': 'Create Room'},
                             context_instance=RequestContext(request))
Exemple #12
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))