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'), + ), + ] 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..c066de6 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().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..b23794f 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..ac69dcb 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, pk): + tutorial = Tutorial.objects.get(pk=pk, publish=True) + TutorialHitCount.objects.create(tutorial=tutorial) + return redirect(tutorial.link) + class ContributeView(TemplateView): """view for the tutorial contribution page"""