From 560ffbdd65b2848514ed4ce55dafb9e85ca281bf Mon Sep 17 00:00:00 2001 From: DerTyp187 Date: Mon, 22 Nov 2021 22:16:35 +0100 Subject: [PATCH] bug fixes --- TealCode/TealCode/settings.py | 3 ++- TealCode/main/templates/main/topic.html | 7 +++-- TealCode/main/views.py | 34 +++++++++++++------------ requirements.txt | 3 ++- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/TealCode/TealCode/settings.py b/TealCode/TealCode/settings.py index 6b45e23..c54eca6 100644 --- a/TealCode/TealCode/settings.py +++ b/TealCode/TealCode/settings.py @@ -23,7 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = 'django-insecure-84w$au&)%5rl8ud!82e%&)e&r+c0f9z%zlr4m9(76mebvx-r2@' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False ALLOWED_HOSTS = ['code.tealfire.de'] @@ -42,6 +42,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'main.apps.MainConfig', 'analytics.apps.AnalyticsConfig', + 'ipware', ] MIDDLEWARE = [ diff --git a/TealCode/main/templates/main/topic.html b/TealCode/main/templates/main/topic.html index 9894e2a..54abf98 100644 --- a/TealCode/main/templates/main/topic.html +++ b/TealCode/main/templates/main/topic.html @@ -79,8 +79,11 @@ -
- {{ read_more }} +
+

Read More

+

+ {{ read_more | safe }} +

diff --git a/TealCode/main/views.py b/TealCode/main/views.py index cc4a631..3652c9a 100644 --- a/TealCode/main/views.py +++ b/TealCode/main/views.py @@ -4,7 +4,7 @@ from .models import Category, Topic, Rating from analytics.models import View def index(req): - view = View(ip=get_client_ip(req), home=True) + view = View(ip=get_ip(req), home=True) view.save() categorys_obj = Category.objects.all() return render(req, "main/index.html", {'categorys': categorys_obj}) @@ -20,13 +20,13 @@ def topic(req, category, topic): helpful_count = 0 notHelpful_count = 0 - for rating in Rating.objects.all(): + for rating in Rating.objects.filter(topic=topic_obj): if rating.is_positive == 1: helpful_count = helpful_count+1 else: notHelpful_count = notHelpful_count +1 - if topic_obj: + if topic_obj:# topic_obj.title context = { 'title': topic_obj.title, 'code': topic_obj.code_text, @@ -42,7 +42,7 @@ def topic(req, category, topic): } - view = View(ip=get_client_ip(req), topic=topic_obj) + view = View(ip=get_ip(req), topic=topic_obj) view.save() return render(req, "main/topic.html", context) @@ -57,7 +57,7 @@ def category(req, category): if category_obj: topics_obj = Topic.objects.filter(category=category_obj) - view = View(ip=get_client_ip(req), category=category_obj) + view = View(ip=get_ip(req), category=category_obj) view.save() context = { @@ -100,26 +100,28 @@ def privacy(req): return render(req, "main/privacy.html", {'current': 'privacy'}) def rating(req, topic_title, is_positive): - if(Rating.objects.filter(ip=get_client_ip(req))): - return redirect("main-index") - - topic = Topic.objects.filter(title=topic_title).first() + + if(Rating.objects.filter(ip=get_ip(req), topic=topic)): + return redirect("main-index") + if is_positive == 1: is_positive = True else: is_positive = False - rating = Rating(topic=topic, is_positive=is_positive, ip=get_client_ip(req)) + rating = Rating(topic=topic, is_positive=is_positive, ip=get_ip(req)) rating.save() return redirect("main-index") -def get_client_ip(req): - x_forwarded_for = req.META.get("HTTP_X_FORWARDED_FOR") - if x_forwarded_for: - ip = x_forwarded_for.split[","][0] +def get_ip(req): + ip = req.META.get('HTTP_X_REAL_IP') + if ip is None: + return "x.x.x.x" else: - ip = req.META.get("REMOTE_ADDR") - return ip + # We got the client's IP address + return ip + + diff --git a/requirements.txt b/requirements.txt index 8fb37d3..169a9a3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Django==3.2.9 -mysqlclient \ No newline at end of file +mysqlclient +django-ipware \ No newline at end of file