-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uzuepłnienia scenariusza Czat (wer. rozszerzona).
- Loading branch information
Showing
7 changed files
with
185 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
# czat/czat/urls.py | ||
|
||
from django.conf.urls import patterns, include, url | ||
from django.contrib import admin | ||
from czat import views # importujemy zdefiniowane w pliku views.py widoki | ||
|
||
admin.autodiscover() # potrzebne tylko w Django 1.6 | ||
|
||
from django.views.generic.edit import CreateView | ||
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm | ||
from django.core.urlresolvers import reverse_lazy | ||
|
||
urlpatterns = patterns('', | ||
url(r'^$', views.index, name='index'), | ||
#url(r'^rejestruj/', views.rejestruj, name='rejestruj'), | ||
#url(r'^loguj/', views.loguj, name='loguj'), | ||
#url(r'^wyloguj/', views.wyloguj, name='wyloguj'), | ||
url(r'^rejestruj/', CreateView.as_view( | ||
template_name='czat/rejestruj.html', | ||
form_class=UserCreationForm, | ||
success_url='/'), name='rejestruj'), | ||
url(r'^loguj/', 'django.contrib.auth.views.login', | ||
{'template_name': 'czat/loguj.html'}, | ||
name='loguj'), | ||
url(r'^wyloguj/', 'django.contrib.auth.views.logout', | ||
{'next_page': reverse_lazy('index')}, | ||
name='wyloguj'), | ||
url(r'^wiadomosci/', views.wiadomosci, name='wiadomosci'), | ||
|
||
url(r'^admin/', include(admin.site.urls)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
# czat/czat/views.py | ||
|
||
#from django.http import HttpResponse | ||
from django.shortcuts import render | ||
|
||
def index(request): | ||
"""Strona główna aplikacji.""" | ||
#return HttpResponse("Witaj w aplikacji Czat!") | ||
kontekst = {'user': request.user} | ||
return render(request, 'czat/index.html', kontekst) | ||
|
||
from django.shortcuts import redirect | ||
from django.core.urlresolvers import reverse | ||
from django.contrib.auth import authenticate, login, logout | ||
from django.contrib import messages | ||
|
||
def rejestruj(request): | ||
"""Rejestracja nowego użytkownika.""" | ||
from django.contrib.auth.forms import UserCreationForm | ||
|
||
if request.method == 'POST': | ||
form = UserCreationForm(request.POST) | ||
if form.is_valid(): | ||
form.save() | ||
messages.success(request, "Zostałeś zarejestrowany.") | ||
user = authenticate( | ||
username=form.data['username'], | ||
password=form.data['password1']) | ||
login(request, user) | ||
messages.success(request, "Zostałeś zalogowany.") | ||
return redirect(reverse('index')) | ||
|
||
kontekst = {'form': UserCreationForm()} | ||
return render(request, 'czat/rejestruj.html', kontekst) | ||
|
||
def loguj(request): | ||
"""Logowanie użytkownika""" | ||
from django.contrib.auth.forms import AuthenticationForm | ||
|
||
if request.method == 'POST': | ||
form = AuthenticationForm(request, request.POST) | ||
if form.is_valid(): | ||
login(request, form.get_user()) | ||
messages.success(request, "Zostałeś zalogowany!") | ||
return redirect(reverse('index')) | ||
|
||
kontekst = {'form': AuthenticationForm()} | ||
return render(request, 'czat/loguj.html', kontekst) | ||
|
||
def wyloguj(request): | ||
"""Wylogowanie użytkownika""" | ||
logout(request) | ||
messages.info(request, "Zostałeś wylogowany!") | ||
return redirect(reverse('index')) | ||
|
||
from czat.models import Wiadomosc | ||
from django.utils import timezone | ||
from django.contrib.auth.decorators import login_required | ||
|
||
@login_required(login_url='/loguj') | ||
def wiadomosci(request): | ||
"""Dodawanie i wyświetlanie wiadomości""" | ||
|
||
if request.method == 'POST': | ||
tekst = request.POST.get('tekst', '') | ||
if not 0 < len(tekst) <= 250: | ||
messages.error(request, | ||
"Wiadomość nie może być pusta, może mieć maks. 250 znaków!") | ||
else: | ||
wiadomosc = Wiadomosc(tekst=tekst, | ||
data_pub=timezone.now(), | ||
autor=request.user) | ||
wiadomosc.save() | ||
return redirect(reverse('wiadomosci')) | ||
|
||
wiadomosci = Wiadomosc.objects.all() | ||
kontekst = {'user': request.user, 'wiadomosci': wiadomosci} | ||
return render(request, 'czat/wiadomosci.html', kontekst) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!-- czat/czat/templates/czat/wiadomosci.html --> | ||
<html> | ||
<body> | ||
<h1>Wiadomości</h1> | ||
|
||
{% if messages %} | ||
<ul> | ||
{% for komunikat in messages %} | ||
<li>{{ komunikat|capfirst }}</li> | ||
{% endfor %} | ||
</ul> | ||
{% endif %} | ||
|
||
<h2>Dodaj wiadomość</h2> | ||
<form method="POST"> | ||
{% csrf_token %} | ||
<input type="text" name="tekst" /> | ||
<button type="submit">Zapisz</button> | ||
</form> | ||
|
||
<h2>Lista wiadomości:</h2> | ||
<ol> | ||
{% for wiadomosc in wiadomosci %} | ||
<li> | ||
<strong>{{ wiadomosc.autor.username }}</strong> ({{ wiadomosc.data_pub }}): | ||
<br /> {{ wiadomosc.tekst }} | ||
</li> | ||
{% endfor %} | ||
</ol> | ||
</body> | ||
</html> |