Skip to content

Commit

Permalink
enroll room, chat room
Browse files Browse the repository at this point in the history
  • Loading branch information
Jpadilla1 committed Apr 28, 2014
1 parent fc21d64 commit 8e04eb0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 7 deletions.
9 changes: 9 additions & 0 deletions apps/chats/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import forms
from django.core.validators import MinLengthValidator

from .models import ChatRoom

Expand All @@ -15,3 +16,11 @@ class Meta:
helper = FormHelper()
helper.form_method = 'POST'
helper.add_input(Submit('Create', 'Create', css_class='btn-lg btn-primary'))


class EnrollRoomForm(forms.Form):
key = forms.CharField(max_length=15, required=True,
validators=[MinLengthValidator(6), ])
helper = FormHelper()
helper.form_method = 'POST'
helper.add_input(Submit('Enroll', 'Enroll', css_class='btn-lg btn-primary'))
7 changes: 4 additions & 3 deletions apps/chats/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.conf.urls import patterns, url

from .views import RoomsView, CreateRoomView, RoomView
from .views import RoomsView, CreateRoomView, RoomView, EnrollRoomView

urlpatterns = patterns(
'',
url(r'^$', RoomsView.as_view(), name='index'),
url(r'^create/', CreateRoomView.as_view(), name='create'),
url(r'^(?P<slug>[-\w]+)/', RoomView.as_view(), name='room'),
url(r'^create/$', CreateRoomView.as_view(), name='create'),
url(r'^(?P<slug>[-\w]+)/$', RoomView.as_view(), name='room'),
url(r'^(?P<slug>[-\w]+)/check/$', EnrollRoomView.as_view(), name='check'),
)
81 changes: 77 additions & 4 deletions apps/chats/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from django.views.generic import ListView, CreateView, FormView
from django.shortcuts import redirect
from django.views.generic import ListView, CreateView, DetailView
from django.shortcuts import redirect, get_object_or_404
from django.views.generic.edit import FormMixin

from braces.views import LoginRequiredMixin, StaffuserRequiredMixin

from ..messages.forms import CreateMessageForm
from ..messages.models import Message

from .models import ChatRoom
from .forms import CreateRoomForm
from .forms import CreateRoomForm, EnrollRoomForm


class RoomsView(LoginRequiredMixin, ListView):
Expand All @@ -26,6 +28,77 @@ def form_valid(self, form):
return redirect('users:myrooms')


class RoomView(LoginRequiredMixin, FormView):
class RoomView(LoginRequiredMixin, FormMixin, DetailView):
model = ChatRoom
form_class = CreateMessageForm
template_name = "chats/room.html"

def get_success_url(self):
return redirect('chats:room', slug=self.kwargs['slug'])

def get(self, request, *args, **kwargs):
if ChatRoom.objects.filter(members=request.user, slug=kwargs['slug']):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
else:
return redirect('chats:check', slug=kwargs['slug'])

def get_context_data(self, **kwargs):
if 'view' not in kwargs:
kwargs['view'] = self
kwargs['form'] = self.form_class
kwargs['room'] = get_object_or_404(ChatRoom, slug=self.kwargs['slug'])
kwargs['messages'] = Message.objects.filter(room=kwargs['room'])
return kwargs

def post(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)

def form_valid(self, form):
form.instance.user = self.request.user
form.instance.room = get_object_or_404(
ChatRoom, slug=self.kwargs['slug'])
form.save()
return redirect('chats:room', slug=self.kwargs['slug'])


class EnrollRoomView(LoginRequiredMixin, FormMixin, DetailView):
model = ChatRoom
form_class = EnrollRoomForm
template_name = "chats/enroll.html"

def get_success_url(self):
return redirect('chats:room', slug=self.kwargs['slug'])

def get_context_data(self, **kwargs):
if 'view' not in kwargs:
kwargs['view'] = self
kwargs['form'] = self.form_class
kwargs['room'] = get_object_or_404(ChatRoom, slug=self.kwargs['slug'])
return kwargs

def post(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)

def form_valid(self, form):
key = form.cleaned_data['key']
room = get_object_or_404(ChatRoom, slug=self.kwargs['slug'])
if room.key == key:
room.members.add(self.request.user)
room.save()
else:
print "The key given is not correct."
return redirect('chats:room', slug=self.kwargs['slug'])
12 changes: 12 additions & 0 deletions templates/chats/enroll.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}Rooms | Enroll{% endblock title %}
{% block content %}
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<h1>{{ room.name }}</h1>
<hr>
{% crispy form %}
</div>
</div>
{% endblock content %}
15 changes: 15 additions & 0 deletions templates/chats/room.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}Chats | {{ room.name }}{% endblock title %}
{% block content %}
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
{% for m in messages %}
<h4>{{ m.user.username }}</h4>
<p>{{ m }}</p>
<hr>
{% endfor %}
{% crispy form %}
</div>
</div>
{% endblock content %}

0 comments on commit 8e04eb0

Please sign in to comment.