Skip to content

Commit

Permalink
Kolejna aktualizacja scenriusza rozszerzonego Czat
Browse files Browse the repository at this point in the history
  • Loading branch information
xinulsw committed Apr 22, 2015
1 parent c6783e6 commit b384e5d
Show file tree
Hide file tree
Showing 25 changed files with 464 additions and 95 deletions.
30 changes: 30 additions & 0 deletions docs/python/czat/base_z10.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- czat/czat/templates/czat/base.html -->
<html>
<head>
<title>{% block tytul %} System wiadomości Czat {% endblock tytul %}</title>
</head>
<body>
<h1>{% block naglowek %} Witaj w aplikacji Czat! {% endblock naglowek %}</h1>

{% block komunikaty %}
{% if messages %}
<ul>
{% for komunikat in messages %}
<li>{{ komunikat|capfirst }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock komunikaty %}

{% block tresc %} {% endblock tresc %}

{% if user.is_authenticated %}
{% block linkilog %} {% endblock linkilog %}
<p><a href="{% url 'wyloguj' %}">Wyloguj się</a></p>
{% else %}
{% block linkizalog %} {% endblock linkizalog %}
{% endif %}

{% block linki %} {% endblock linki %}
</body>
</html>
54 changes: 42 additions & 12 deletions docs/python/czat/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ W tym momencie widok wiadomości powinien już działać. Przetestuj!
W szablonie listy wiadomości przed znacznikiem zamykającym ``</body>``
dodaj link do strony głównej.

Dodawanie wiadomości zrealizujemy wykorzystując widok ``CreateView``.
**Dodawanie wiadomości** zrealizujemy wykorzystując widok ``CreateView``.
Ponieważ nasz model wiadomości zawiera klucz obcy, mianowicie pole autor,
tym razem dostosujemy klasę widoku w pliku :file:`views.py`. Dzięki temu
będziemy mogli rozszerzyć standardową funkcjonalność widoku.
Expand Down Expand Up @@ -768,12 +768,29 @@ parametrem, np. ``r'^aktualizuj/(?P<pk>\d+)/'``. Część ``/(?P<pk>\d+)`` oznac
"klucz główny". Zmienna ta oznaczać będzie identyfikator wiadomości i dostępna
będzie w widokach.

Widok przeznaczony do usuwania danych powinien być zaimportowany razem z widokiem
``ListView``. Wszystkie ustawienia określamy w pliku :file:`urls.py` za pomocą
omówionych wcześniej właściwości. Nieco więcej pracy wymaga dostosowanie widoku
aktualizacji.
**Usuwanie danych** realizujemy za pomocą widoku ``DeleteView``, który powinien
być już zaimportowany razem z widokiem ``ListView``. Domyślny szablon
dla tego widoku przyjmuje nnazwę *<nazwa-modelu>_confir_delete.html*,
dlatego uproścliśmy jego nazwę we właściwości ``template_name``.

W pliku :file:`views.py` utworzymy klasę ``AktualizujWiadomosc`` opartą na
Ćwiczenie 6
==============

Utwórz szablon :file:`wiadomosc_usun.html` wzorując sie na wcześniejszych
szablonach. Zamiast instrukcji wyświetlającej formularz umieść kod:

.. raw:: html

<div class="code_no">Plik wiadomosc_usun.html nr <script>var plik_no = plik_no || 1; document.write(plik_no++);</script></div>

.. highlight:: html
.. literalinclude:: wiadomosc_usun_z8.html
:linenos:
:lineno-start: 17
:lines: 17

Nieco więcej pracy wymaga **dostosowanie widoku aktualizacji**. W pliku
:file:`views.py` utworzymy klasę ``AktualizujWiadomosc`` opartą na
widoku ogólnym ``UpdateView``:

.. raw:: html
Expand Down Expand Up @@ -813,13 +830,26 @@ Jej zadanie to wybranie z bazy danych i zwrócenie do formularza wiadomości,
której identyfikator został przekazany w adresie pod nazwą *pk*:
``wiadomosc = Wiadomosc.objects.get(id=self.kwargs['pk'])``.

Żeby przetestować aktualizowanie i usuwanie wiadomości, w szablonie listy
wiadomości wygenerujemy odpowiednie linki, ale tylko dla wiadomości
utworzonych przez zalogowanego użytkownika.

Ćwiczenie 6
Ćwiczenie 7
==============

Żeby przetestować aktualizowanie i usuwanie wiadomości, w szablonie
:file:`wiadomosc_list.html` wygeneruj linki *Edytuj* i *Usuń* tylko
dla wiadomości utworzonych przez zalogowanego użytkownika.

Wstaw w odpowiednie miejsce szablonu poniższy kod:

.. raw:: html

<div class="code_no">Plik wiadomosc_lista.html nr <script>var plik_no = plik_no || 1; document.write(plik_no++);</script></div>

.. highlight:: html
.. literalinclude:: wiadomosc_list_z8.html
:linenos:
:lineno-start: 20
:lines: 20-23

Dodaj również te same linki do listy wiadomości na stronach dodawania i aktualizowania.

Wiadomości jeszcze raz
************************
Expand Down Expand Up @@ -867,7 +897,7 @@ i listy wiadomości dodanych.
.. literalinclude:: wiadomosci_z9.html
:linenos:

Ćwiczenie 7
Ćwiczenie 8
=====================

Powiąż widok ``wiadomosci()`` z adresem */wiadomosci* w pliku :file:`urls.py`,
Expand Down
16 changes: 16 additions & 0 deletions docs/python/czat/index_z10.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- czat/czat/templates/czat/index.html -->
{% extends "czat/base.html" %}

{% block naglowek %}Witaj w aplikacji Czat!{% endblock naglowek %}

{% block linkilog %}
<p>Jesteś zalogowany jako {{ user.username }}.</p>
<p><a href="{% url 'wiadomosc' %}">Dodaj wiadomość</a></p>
<p><a href="{% url 'wiadomosci' %}">Lista wiadomości</a></p>
{% endblock linkilog %}

{% block linkizalog %}
<p><a href="{% url 'loguj' %}">Zaloguj się</a></p>
<p><a href="{% url 'rejestruj' %}">Zarejestruj się</a></p>
{% endblock linkizalog %}

22 changes: 22 additions & 0 deletions docs/python/czat/rejestruj_z10.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- czat/czat/templates/czat/rejestruj.html -->
{% extends "czat/base.html" %}

{% block naglowek %}Rejestracja użytkownika{% endblock naglowek %}

{% block tresc %}
{% if not user.is_authenticated %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Zarejestruj</button>
</form>
{% endif %}
{% endblock tresc %}

{% block linkilog %}
<p>Jesteś już zarejestrowany jako {{ user.username }}.</p>
{% endblock linkilog %}

{% block linki %}
<p><a href="{% url 'index' %}">Strona główna</a></p>
{% endblock linki %}
6 changes: 3 additions & 3 deletions docs/python/czat/urls_z8.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
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'),
template_name='czat/wiadomosc_usun.html',
success_url='/wiadomosci'),
login_url='/loguj'), name='usun'),

url(r'^admin/', include(admin.site.urls)),
)
6 changes: 3 additions & 3 deletions docs/python/czat/urls_z9.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
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'),
template_name='czat/wiadomosc_usun.html',
success_url='/wiadomosci'),
login_url='/loguj'), name='usun'),

url(r'^admin/', include(admin.site.urls)),
)
30 changes: 30 additions & 0 deletions docs/python/czat/wiadomosc_list_z8.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- czat/czat/templates/czat/wiadomosc_list.html -->
<html>
<body>
<h1>Wiadomości</h1>
<p>Cześć, {{ user.username }}</p>
{% if messages %}
<ul>
{% for komunikat in messages %}
<li>{{ komunikat|capfirst }}</li>
{% endfor %}
</ul>
{% endif %}

<h2>Lista wiadomości:</h2>
<ol>
{% for wiadomosc in wiadomosci %}
<li>
<strong>{{ wiadomosc.autor.username }}</strong> ({{ wiadomosc.data_pub }}):
<br /> {{ wiadomosc.tekst }}
{% if wiadomosc.autor.username == user.username %}
<a href="{% url 'aktualizuj' wiadomosc.id %}">Edytuj</a> &bull;
<a href="{% url 'usun' wiadomosc.id %}">Usuń</a>
{% endif %}
</li>
{% endfor %}
</ol>

<p><a href="{% url 'index' %}">Strona główna</a></p>
</body>
</html>
23 changes: 23 additions & 0 deletions docs/python/czat/wiadomosc_usun_z8.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- czat/czat/templates/czat/wiadomosc_usun.html -->
<html>
<body>
<h1>Wiadomości</h1>

{% if messages %}
<ul>
{% for komunikat in messages %}
<li>{{ komunikat|capfirst }}</li>
{% endfor %}
</ul>
{% endif %}

<h2>Usuń wiadomość</h2>
<form method="POST">
{% csrf_token %}
<p>Czy na pewno chcesz usunąć wiadomość:<br />"{{ object }}"?</p>
<button type="submit">Usuń</button>
</form>

<p><a href="{% url 'index' %}">Strona główna</a></p>
</body>
</html>
30 changes: 30 additions & 0 deletions python/czat/czat/templates/czat/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- czat/czat/templates/czat/base.html -->
<html>
<head>
<title>{% block tytul %} System wiadomości Czat {% endblock tytul %}</title>
</head>
<body>
<h1>{% block naglowek %} Witaj w aplikacji Czat! {% endblock naglowek %}</h1>

{% block komunikaty %}
{% if messages %}
<ul>
{% for komunikat in messages %}
<li>{{ komunikat|capfirst }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock komunikaty %}

{% block tresc %} {% endblock tresc %}

{% if user.is_authenticated %}
{% block linkilog %} {% endblock linkilog %}
<p><a href="{% url 'wyloguj' %}">Wyloguj się</a></p>
{% else %}
{% block linkizalog %} {% endblock linkizalog %}
{% endif %}

{% block linki %} {% endblock linki %}
</body>
</html>
35 changes: 13 additions & 22 deletions python/czat/czat/templates/czat/index.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
<!-- czat/czat/templates/czat/index.html -->
<html>
<head></head>
<body>
<h1>Witaj w aplikacji Czat!</h1>
{% extends "czat/base.html" %}

{% if messages %}
<ul>
{% for komunikat in messages %}
<li>{{ komunikat|capfirst }}</li>
{% endfor %}
</ul>
{% endif %}
{% block naglowek %}Witaj w aplikacji Czat!{% endblock naglowek %}

{% block linkilog %}
<p>Jesteś zalogowany jako {{ user.username }}.</p>
<p><a href="{% url 'wiadomosc' %}">Dodaj wiadomość</a></p>
<p><a href="{% url 'wiadomosci' %}">Lista wiadomości</a></p>
{% endblock linkilog %}

{% block linkizalog %}
<p><a href="{% url 'loguj' %}">Zaloguj się</a></p>
<p><a href="{% url 'rejestruj' %}">Zarejestruj się</a></p>
{% endblock linkizalog %}

{% if user.is_authenticated %}
<p>Jesteś zalogowany jako {{ user.username }}.</p>
<p><a href="{% url 'wiadomosc' %}">Dodaj wiadomość</a></p>
<p><a href="{% url 'wiadomosci' %}">Lista wiadomości</a></p>
<p><a href="{% url 'wyloguj' %}">Wyloguj się</a></p>
{% else %}
<p><a href="{% url 'loguj' %}">Zaloguj się</a></p>
<p><a href="{% url 'rejestruj' %}">Zarejestruj się</a></p>
{% endif %}
</body>
</html>
16 changes: 11 additions & 5 deletions python/czat/czat/templates/czat/loguj.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<html>
<body>
<h1>Logowanie użytkownika</h1>
<!-- czat/czat/templates/czat/loguj.html -->
{% extends "czat/base.html" %}

{% block naglowek %}Logowanie użytkownika{% endblock naglowek %}

{% block tresc %}
{% if user.is_authenticated %}
<p>Jesteś już zalogowany jako {{ user.username }}.
<br /><a href="/">Strona główna</a></p>
Expand All @@ -11,5 +14,8 @@ <h1>Logowanie użytkownika</h1>
<button type="submit">Zaloguj</button>
</form>
{% endif %}
</body>
</html>
{% endblock tresc %}

{% block linki %}
<p><a href="{% url 'index' %}">Strona główna</a></p>
{% endblock linki %}
25 changes: 25 additions & 0 deletions python/czat/czat/templates/czat/nobase_index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- czat/czat/templates/czat/index.html -->
<html>
<head></head>
<body>
<h1>Witaj w aplikacji Czat!</h1>

{% if messages %}
<ul>
{% for komunikat in messages %}
<li>{{ komunikat|capfirst }}</li>
{% endfor %}
</ul>
{% endif %}

{% if user.is_authenticated %}
<p>Jesteś zalogowany jako {{ user.username }}.</p>
<p><a href="{% url 'wiadomosc' %}">Dodaj wiadomość</a></p>
<p><a href="{% url 'wiadomosci' %}">Lista wiadomości</a></p>
<p><a href="{% url 'wyloguj' %}">Wyloguj się</a></p>
{% else %}
<p><a href="{% url 'loguj' %}">Zaloguj się</a></p>
<p><a href="{% url 'rejestruj' %}">Zarejestruj się</a></p>
{% endif %}
</body>
</html>
16 changes: 16 additions & 0 deletions python/czat/czat/templates/czat/nobase_loguj.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- czat/czat/templates/czat/loguj.html -->
<html>
<body>
<h1>Logowanie użytkownika</h1>
{% if user.is_authenticated %}
<p>Jesteś już zalogowany jako {{ user.username }}.
<br /><a href="/">Strona główna</a></p>
{% else %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Zaloguj</button>
</form>
{% endif %}
</body>
</html>
16 changes: 16 additions & 0 deletions python/czat/czat/templates/czat/nobase_rejestruj.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- czat/czat/templates/czat/rejestruj.html -->
<html>
<body>
<h1>Rejestracja użytkownika</h1>
{% if user.is_authenticated %}
<p>Jesteś już zarejestrowany jako {{ user.username }}.
<br /><a href="/">Strona główna</a></p>
{% else %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Zarejestruj</button>
</form>
{% endif %}
</body>
</html>

0 comments on commit b384e5d

Please sign in to comment.