-
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.
Rozwijanie i poprawki scenariusza rozszerzonego Czat
- Loading branch information
Showing
18 changed files
with
517 additions
and
138 deletions.
There are no files selected for viewing
Binary file not shown.
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,11 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from django.forms import ModelForm, TextInput | ||
from czat.models import Wiadomosc | ||
|
||
class AktualizujWiadomoscForm(ModelForm): | ||
class Meta: | ||
model = Wiadomosc | ||
fields = ['tekst', 'data_pub'] | ||
exclude = ['autor'] | ||
widgets = {'tekst': TextInput(attrs={'size': 80})} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,53 @@ | ||
# -*- 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 | ||
from django.contrib.auth.decorators import login_required | ||
from django.views.generic import ListView, DeleteView | ||
from czat.models import Wiadomosc | ||
|
||
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/', login_required( | ||
ListView.as_view( | ||
model=Wiadomosc, | ||
context_object_name='wiadomosci', | ||
paginate_by=10), | ||
login_url='/loguj'), | ||
name='wiadomosci'), | ||
url(r'^wiadomosc/$', login_required( | ||
views.UtworzWiadomosc.as_view(), | ||
login_url='/loguj'), name='wiadomosc'), | ||
url(r'^aktualizuj/(?P<pk>\d+)/', login_required( | ||
views.AktualizujWiadomosc.as_view(), | ||
login_url='/loguj'), name='aktualizuj'), | ||
url(r'^usun/(?P<pk>\d+)/', login_required( | ||
DeleteView.as_view( | ||
model=Wiadomosc, | ||
template_name='wiadomosc_usun.html', | ||
success_url='/lista'), | ||
login_url='/login'), name='usun'), | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- 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 | ||
from django.contrib.auth.decorators import login_required | ||
from django.views.generic import ListView, DeleteView | ||
from czat.models import Wiadomosc | ||
|
||
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'^wiadomosci/', views.wiadomosci, name='wiadomosci'), | ||
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/', login_required( | ||
ListView.as_view( | ||
model=Wiadomosc, | ||
context_object_name='wiadomosci', | ||
paginate_by=10), | ||
login_url='/loguj'), | ||
name='wiadomosci'), | ||
url(r'^wiadomosc/$', login_required( | ||
views.UtworzWiadomosc.as_view(), | ||
login_url='/loguj'), name='wiadomosc'), | ||
url(r'^aktualizuj/(?P<pk>\d+)/', login_required( | ||
views.AktualizujWiadomosc.as_view(), | ||
login_url='/loguj'), name='aktualizuj'), | ||
url(r'^usun/(?P<pk>\d+)/', login_required( | ||
DeleteView.as_view( | ||
model=Wiadomosc, | ||
template_name='wiadomosc_usun.html', | ||
success_url='/lista'), | ||
login_url='/login'), name='usun'), | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# -*- 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 django.views.generic.edit import CreateView | ||
from czat.models import Wiadomosc | ||
from django.utils import timezone | ||
|
||
class UtworzWiadomosc(CreateView): | ||
model = Wiadomosc | ||
fields = ['tekst', 'data_pub'] | ||
context_object_name = 'wiadomosci' | ||
success_url = '/wiadomosc' | ||
|
||
def get_initial(self): | ||
initial = super(UtworzWiadomosc, self).get_initial() | ||
initial['data_pub'] = timezone.now() | ||
return initial | ||
|
||
def get_context_data(self, **kwargs): | ||
kwargs['wiadomosci'] = Wiadomosc.objects.filter( | ||
autor=self.request.user) | ||
return super(UtworzWiadomosc, self).get_context_data(**kwargs) | ||
|
||
def form_valid(self, form): | ||
wiadomosc = form.save(commit=False) | ||
wiadomosc.autor = self.request.user | ||
wiadomosc.save() | ||
return super(UtworzWiadomosc, self).form_valid(form) | ||
|
||
|
||
from django.views.generic.edit import UpdateView | ||
|
||
class AktualizujWiadomosc(UpdateView): | ||
model = Wiadomosc | ||
from czat.forms import AktualizujWiadomoscForm | ||
form_class = AktualizujWiadomoscForm | ||
context_object_name = 'wiadomosci' | ||
template_name = 'czat/wiadomosc_form.html' | ||
success_url = '/wiadomosci' | ||
|
||
def get_context_data(self, **kwargs): | ||
kwargs['wiadomosci'] = Wiadomosc.objects.filter( | ||
autor=self.request.user) | ||
return super(AktualizujWiadomosc, self).get_context_data(**kwargs) | ||
|
||
def get_object(self, queryset=None): | ||
wiadomosc = Wiadomosc.objects.get(id=self.kwargs['pk']) | ||
return wiadomosc | ||
|
||
|
||
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
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 was deleted.
Oops, something went wrong.
File renamed without changes.
Oops, something went wrong.