0003-seo-add-settings-for-meta-tags-20263.patch
hobo/seo/forms.py | ||
---|---|---|
20 | 20 | |
21 | 21 |
class RobotsTxtForm(forms.Form): |
22 | 22 |
content = forms.CharField( |
23 | 23 |
label=_('Content of robots.txt file'), |
24 | 24 |
required=False, |
25 | 25 |
widget=forms.Textarea) |
26 | 26 | |
27 | 27 | |
28 |
class SettingsForm(forms.Form): |
|
29 |
meta_description = forms.CharField( |
|
30 |
label=_('Description for indexing'), |
|
31 |
required=False) |
|
32 |
meta_keywords = forms.CharField( |
|
33 |
label=_('Keywords for indexing'), |
|
34 |
required=False) |
|
35 | ||
36 | ||
28 | 37 |
class EnableForm(forms.Form): |
29 | 38 |
pass |
hobo/seo/templates/hobo/seo_home.html | ||
---|---|---|
50 | 50 | |
51 | 51 |
{% if mode == 'customize' or mode == 'disallow' %} |
52 | 52 |
<div class="section padded"> |
53 | 53 |
<span>{% trans "Contents of robots.txt file:" %}</span> |
54 | 54 |
<pre>{{ robots_txt }}</pre> |
55 | 55 |
</div> |
56 | 56 |
{% endif %} |
57 | 57 | |
58 |
{% if mode == 'customize' or mode == 'allow' %} |
|
59 |
<div class="section padded"> |
|
60 |
<form method="post"> |
|
61 |
{% csrf_token %} |
|
62 |
{{ form.as_p }} |
|
63 |
<div class="buttons"> |
|
64 |
<button class="submit-button">{% trans "Save" %}</button> |
|
65 |
</div> |
|
66 |
</form> |
|
67 |
</div> |
|
68 |
{% endif %} |
|
69 | ||
58 | 70 |
{% endblock %} |
hobo/seo/views.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 | 17 |
from django.core.urlresolvers import reverse_lazy |
18 | 18 |
from django.http import HttpResponseRedirect |
19 | 19 |
from django.views.generic import FormView, TemplateView |
20 | 20 | |
21 | 21 |
from hobo.environment.utils import get_variable, set_variable |
22 |
from .forms import RobotsTxtForm |
|
22 |
from .forms import RobotsTxtForm, SettingsForm
|
|
23 | 23 | |
24 | 24 | |
25 | 25 |
ALLOW = "" |
26 | 26 |
DISALLOW = """User-agent: * |
27 | 27 |
Disallow: /""" |
28 | 28 | |
29 | 29 | |
30 | 30 |
def get_mode(content): |
31 | 31 |
content = content.strip().replace('\r', '') |
32 | 32 |
if content == ALLOW: |
33 | 33 |
return 'allow' |
34 | 34 |
if content == DISALLOW: |
35 | 35 |
return 'disallow' |
36 | 36 |
return 'customize' |
37 | 37 | |
38 | 38 | |
39 |
class HomeView(TemplateView):
|
|
39 |
class HomeView(FormView):
|
|
40 | 40 |
template_name = 'hobo/seo_home.html' |
41 |
form_class = SettingsForm |
|
41 | 42 |
success_url = reverse_lazy('seo-home') |
42 | 43 | |
44 |
def get_initial(self): |
|
45 |
initial = super(HomeView, self).get_initial() |
|
46 |
initial['meta_description'] = get_variable('meta_description').value |
|
47 |
initial['meta_keywords'] = get_variable('meta_keywords').value |
|
48 |
return initial |
|
49 | ||
50 |
def form_valid(self, form): |
|
51 |
set_variable('meta_description', form.cleaned_data['meta_description']) |
|
52 |
set_variable('meta_keywords', form.cleaned_data['meta_keywords']) |
|
53 |
return super(HomeView, self).form_valid(form) |
|
54 | ||
43 | 55 |
def get_context_data(self, **kwargs): |
44 | 56 |
context = super(HomeView, self).get_context_data(**kwargs) |
45 | 57 |
context['robots_txt'] = get_variable('robots_txt').value |
46 | 58 |
context['mode'] = get_mode(context['robots_txt']) |
47 | 59 |
return context |
48 | 60 | |
49 | 61 |
home = HomeView.as_view() |
50 | 62 |
hobo/static/css/style.css | ||
---|---|---|
228 | 228 |
ul#id_scopes li { |
229 | 229 |
list-style: none; |
230 | 230 |
margin: 0; |
231 | 231 |
padding: 0; |
232 | 232 |
-moz-column-width: 20em; |
233 | 233 |
-webkit-column-width: 20em; |
234 | 234 |
column-width: 20em; |
235 | 235 |
} |
236 | ||
237 |
#id_meta_description, |
|
238 |
#id_meta_keywords { |
|
239 |
width: 100%; |
|
240 |
} |
tests/test_seo.py | ||
---|---|---|
93 | 93 |
resp = app.get('/seo/customize', status=200) |
94 | 94 |
assert resp.html.textarea['name'] == 'content' |
95 | 95 |
assert resp.html.textarea.text.strip() == 'some content' |
96 | 96 |
resp.form['content'] = 'some new content' |
97 | 97 |
resp = resp.form.submit() |
98 | 98 |
assert get_variable('robots_txt').value == 'some new content' |
99 | 99 |
resp = resp.follow() |
100 | 100 |
assert resp.html.pre.text == 'some new content' |
101 | ||
102 | ||
103 |
def test_meta(app, admin_user): |
|
104 |
login(app) |
|
105 |
resp = app.get('/seo/') |
|
106 |
resp.form['meta_description'] = 'meta description' |
|
107 |
resp.form['meta_keywords'] = 'meta, keywords' |
|
108 |
resp = resp.form.submit() |
|
101 |
- |