From 8ae7ba9b324a3f5d03eee1fb6c911f86b440f604 Mon Sep 17 00:00:00 2001 From: nati Date: Tue, 18 Feb 2020 11:38:20 +0600 Subject: [PATCH 1/4] added a new migration --- app/migrations/0003_auto_20200217_2236.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app/migrations/0003_auto_20200217_2236.py diff --git a/app/migrations/0003_auto_20200217_2236.py b/app/migrations/0003_auto_20200217_2236.py new file mode 100644 index 0000000..0b48894 --- /dev/null +++ b/app/migrations/0003_auto_20200217_2236.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.3 on 2020-02-17 17:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0002_auto_20190829_1937'), + ] + + operations = [ + migrations.AlterField( + model_name='tutorial', + name='tags', + field=models.ManyToManyField(to='app.Tag'), + ), + ] From e5464741ea7ed8607122db1e86c4313ea4964488 Mon Sep 17 00:00:00 2001 From: nati Date: Tue, 18 Feb 2020 12:40:27 +0600 Subject: [PATCH 2/4] Tutorial hit count model created, track hit count for each tutorial --- app/migrations/0004_auto_20200218_1116.py | 33 +++++++++++++++++++++++ app/models.py | 15 +++++++++++ app/templates/includes/tutorial-list.html | 17 ++++++++++++ app/templates/latest.html | 18 +------------ app/templates/search_results.html | 18 +------------ app/templates/taglinks.html | 18 +------------ app/urls.py | 1 + app/views.py | 9 +++++-- 8 files changed, 76 insertions(+), 53 deletions(-) create mode 100644 app/migrations/0004_auto_20200218_1116.py create mode 100644 app/templates/includes/tutorial-list.html diff --git a/app/migrations/0004_auto_20200218_1116.py b/app/migrations/0004_auto_20200218_1116.py new file mode 100644 index 0000000..8a9fc43 --- /dev/null +++ b/app/migrations/0004_auto_20200218_1116.py @@ -0,0 +1,33 @@ +# Generated by Django 2.2 on 2020-02-18 05:46 + +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0003_auto_20200217_2236'), + ] + + operations = [ + migrations.AddField( + model_name='tutorial', + name='total_hit_count', + field=models.IntegerField(default=0), + ), + migrations.AlterField( + model_name='tutorial', + name='tags', + field=models.ManyToManyField(to='app.Tag'), + ), + migrations.CreateModel( + name='TutorialHitCount', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_date', models.DateTimeField(default=django.utils.timezone.now)), + ('tutorial', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='app.Tutorial')), + ], + ), + ] diff --git a/app/models.py b/app/models.py index 82c38fc..b2c28be 100644 --- a/app/models.py +++ b/app/models.py @@ -36,6 +36,21 @@ class Tutorial(models.Model): category = models.CharField(max_length=20, choices=CATEGORIES) created_date = models.DateTimeField(default=timezone.now) publish = models.BooleanField(default=False) + total_hit_count = models.IntegerField(default=0) def __str__(self): return self.title + +class TutorialHitCount(models.Model): + tutorial = models.ForeignKey(Tutorial, on_delete=models.DO_NOTHING) + created_date = models.DateTimeField(default=timezone.now) + + def __str__(self): + return self.tutorial.title + '-' + str(self.created_date) + + def save(self, *args, **kwargs): + if self.tutorial.publish: + self.tutorial.total_hit_count = self.tutorial.total_hit_count + 1 + self.tutorial.save() + super(TutorialHitCount, self).save(*args, **kwargs) + diff --git a/app/templates/includes/tutorial-list.html b/app/templates/includes/tutorial-list.html new file mode 100644 index 0000000..ea5bab7 --- /dev/null +++ b/app/templates/includes/tutorial-list.html @@ -0,0 +1,17 @@ +{% for tutorial in tutorials %} +
+ {{ tutorial.title }} +
+ {% for tag in tutorial.tags.all %} + + 📌 {{ tag }} +   + {% endfor %} + +
+
+ {{ tutorial.category }} + +
+{% endfor %} diff --git a/app/templates/latest.html b/app/templates/latest.html index 41e5d00..efb99f1 100644 --- a/app/templates/latest.html +++ b/app/templates/latest.html @@ -5,23 +5,7 @@

Latest in tutorialdb

- {% for tutorial in tutorials %} -
- {{ tutorial.title }} -
- {% for tag in tutorial.tags.all %} - - 📌 {{ tag }} -   - {% endfor %} - -
-
- {{ tutorial.category }} - -
- {% endfor %} + {% include 'includes/tutorial-list.html'%}
{% else %} diff --git a/app/templates/search_results.html b/app/templates/search_results.html index 1a76b5b..b648635 100644 --- a/app/templates/search_results.html +++ b/app/templates/search_results.html @@ -7,23 +7,7 @@

Search Results for "{{ query }}"

About {{ total }} results ({{ time }} seconds)


- {% for tutorial in tutorials %} -
- {{ tutorial.title }} -
- {% for tag in tutorial.tags.all %} - - 📌 {{ tag }} -   - {% endfor %} - -
-
- {{ tutorial.category }} - -
- {% endfor %} + {% include 'includes/tutorial-list.html'%}
diff --git a/app/templates/taglinks.html b/app/templates/taglinks.html index 52edfd2..17efe9a 100644 --- a/app/templates/taglinks.html +++ b/app/templates/taglinks.html @@ -9,23 +9,7 @@

Tutorials Tagged

- {% for tutorial in tutorials %} -
- {{ tutorial.title }} -
- {% for tag in tutorial.tags.all %} - - 📌 {{ tag }} -   - {% endfor %} - -
-
- {{ tutorial.category }} - -
- {% endfor %} + {% include 'includes/tutorial-list.html'%}
diff --git a/app/urls.py b/app/urls.py index 8e8d428..1b71828 100644 --- a/app/urls.py +++ b/app/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ path('', HomePageView.as_view(), name='home'), path('search/', views.search_query, name='search-results'), + path('tutorial/', views.tutorial_redirect, name='tutorial_redirect'), path('api/', include('api.urls'), name='api'), path('latest/', views.latest, name='latest'), path('tags/', views.tags, name='tags'), diff --git a/app/views.py b/app/views.py index 297897c..d61acd8 100644 --- a/app/views.py +++ b/app/views.py @@ -3,10 +3,10 @@ from django.core.cache import cache from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from django.db.models import Q -from django.shortcuts import render +from django.shortcuts import render, redirect from django.views.generic import TemplateView from taggie.parser import generate_tags -from .models import Tag, Tutorial +from .models import Tag, Tutorial, TutorialHitCount from . import cache_constants @@ -101,6 +101,11 @@ def about(request): """about view""" return render(request, 'about.html', {'title': 'About'}) +def tutorial_redirect(request, id): + tutorial = Tutorial.objects.get(pk=id, publish=True) + TutorialHitCount.objects.create(tutorial=tutorial) + return redirect(tutorial.link) + class ContributeView(TemplateView): """view for the tutorial contribution page""" From 85a84032fd7387af4630e82b5da3e2fd6c44f9cd Mon Sep 17 00:00:00 2001 From: nati Date: Tue, 18 Feb 2020 12:50:01 +0600 Subject: [PATCH 3/4] id to pk --- app/urls.py | 2 +- app/views.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/urls.py b/app/urls.py index 1b71828..b23794f 100644 --- a/app/urls.py +++ b/app/urls.py @@ -8,7 +8,7 @@ urlpatterns = [ path('', HomePageView.as_view(), name='home'), path('search/', views.search_query, name='search-results'), - path('tutorial/', views.tutorial_redirect, name='tutorial_redirect'), + path('tutorial/', views.tutorial_redirect, name='tutorial_redirect'), path('api/', include('api.urls'), name='api'), path('latest/', views.latest, name='latest'), path('tags/', views.tags, name='tags'), diff --git a/app/views.py b/app/views.py index d61acd8..ac69dcb 100644 --- a/app/views.py +++ b/app/views.py @@ -101,8 +101,8 @@ def about(request): """about view""" return render(request, 'about.html', {'title': 'About'}) -def tutorial_redirect(request, id): - tutorial = Tutorial.objects.get(pk=id, publish=True) +def tutorial_redirect(request, pk): + tutorial = Tutorial.objects.get(pk=pk, publish=True) TutorialHitCount.objects.create(tutorial=tutorial) return redirect(tutorial.link) From f6a62e9878b3042d0eafe01a4bb7e24aaa4b1928 Mon Sep 17 00:00:00 2001 From: nati Date: Sun, 31 May 2020 14:36:16 +0600 Subject: [PATCH 4/4] save method changed --- app/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index b2c28be..c066de6 100644 --- a/app/models.py +++ b/app/models.py @@ -52,5 +52,5 @@ def save(self, *args, **kwargs): if self.tutorial.publish: self.tutorial.total_hit_count = self.tutorial.total_hit_count + 1 self.tutorial.save() - super(TutorialHitCount, self).save(*args, **kwargs) + super().save(*args, **kwargs)