aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-05-28 00:41:28 -0400
committerBobby <[email protected]>2023-05-28 00:41:28 -0400
commit1bb0aa7433ec5f9d1bc6252204c8d6e8a682396c (patch)
tree38d17386300c1355418d5bed274f65045c8223ca
parentac5057a31021cf0c72fa9ad02d238fd0184f508e (diff)
downloadthatcomputerscientist-1bb0aa7433ec5f9d1bc6252204c8d6e8a682396c.tar.xz
thatcomputerscientist-1bb0aa7433ec5f9d1bc6252204c8d6e8a682396c.zip
Post show view count now, Django session uses redis cache
-rw-r--r--announcements/migrations/0001_initial.py2
-rw-r--r--announcements/models.py5
-rw-r--r--blog/admin.py3
-rw-r--r--blog/context_processors.py12
-rw-r--r--blog/migrations/0001_initial.py2
-rw-r--r--blog/migrations/0013_post_views.py17
-rw-r--r--blog/models.py4
-rw-r--r--blog/urls.py1
-rw-r--r--blog/views.py54
-rw-r--r--blog_admin/urls.py1
-rw-r--r--blog_admin/views.py10
-rw-r--r--chat/chat_cache.py3
-rw-r--r--chat/consumers.py8
-rw-r--r--chat/routing.py1
-rw-r--r--chat/skippy.py1
-rw-r--r--dev_status/urls.py1
-rw-r--r--dev_status/views.py9
-rw-r--r--ignis/admin.py2
-rw-r--r--ignis/migrations/0001_initial.py2
-rw-r--r--ignis/migrations/0003_remove_objectdirectory_name_objectdirectory_slug.py2
-rw-r--r--ignis/migrations/0007_alter_object_location.py2
-rw-r--r--ignis/migrations/0008_postimage_repositoryimages_repositorytitles_and_more.py2
-rw-r--r--ignis/migrations/0010_alter_postimage_post.py2
-rw-r--r--ignis/migrations/0012_coverimage.py2
-rw-r--r--ignis/models.py4
-rw-r--r--ignis/urls.py1
-rw-r--r--ignis/views.py23
-rw-r--r--middleware/oldbrowsermiddleware.py2
-rw-r--r--middleware/subdomainmiddleware.py2
-rw-r--r--requirements.txt1
-rw-r--r--templates/blog/post.html2
-rw-r--r--thatcomputerscientist/asgi.py5
-rw-r--r--thatcomputerscientist/backends.py3
-rw-r--r--thatcomputerscientist/error_handler.py9
-rw-r--r--thatcomputerscientist/search_indexes.py6
-rw-r--r--thatcomputerscientist/settings.py6
-rw-r--r--thatcomputerscientist/sitemaps.py8
-rw-r--r--thatcomputerscientist/templatetags/ad.py5
-rw-r--r--thatcomputerscientist/templatetags/random_numbers.py1
-rw-r--r--thatcomputerscientist/templatetags/remove_tags.py3
-rw-r--r--thatcomputerscientist/urls.py8
-rw-r--r--userpages/urls.py1
-rw-r--r--userpages/views.py6
-rw-r--r--users/accountFunctions.py7
-rw-r--r--users/admin.py2
-rw-r--r--users/forms.py13
-rw-r--r--users/mail_send.py6
-rw-r--r--users/migrations/0001_initial.py2
-rw-r--r--users/migrations/0011_tokenstore.py2
-rw-r--r--users/models.py1
-rw-r--r--users/tokens.py3
-rw-r--r--users/urls.py3
-rw-r--r--users/views.py13
53 files changed, 202 insertions, 94 deletions
diff --git a/announcements/migrations/0001_initial.py b/announcements/migrations/0001_initial.py
index aca74adf..7d78fdaa 100644
--- a/announcements/migrations/0001_initial.py
+++ b/announcements/migrations/0001_initial.py
@@ -1,8 +1,8 @@
# Generated by Django 4.0.6 on 2022-10-02 15:22
+import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
-import django.db.models.deletion
class Migration(migrations.Migration):
diff --git a/announcements/models.py b/announcements/models.py
index 58e434ea..d24efa52 100644
--- a/announcements/models.py
+++ b/announcements/models.py
@@ -1,6 +1,7 @@
-from django.utils import timezone
-from django.db import models
from django.conf import settings
+from django.db import models
+from django.utils import timezone
+
# Create your models here.
class Announcement(models.Model):
diff --git a/blog/admin.py b/blog/admin.py
index 3c0df2fb..e81f0b56 100644
--- a/blog/admin.py
+++ b/blog/admin.py
@@ -1,7 +1,8 @@
from django.contrib import admin
# Register your models here.
-from .models import Post, Comment, Category, Tag
+from .models import Category, Comment, Post, Tag
+
admin.site.register(Post)
admin.site.register(Comment)
admin.site.register(Category)
diff --git a/blog/context_processors.py b/blog/context_processors.py
index bc8d3039..3560ea4d 100644
--- a/blog/context_processors.py
+++ b/blog/context_processors.py
@@ -1,12 +1,14 @@
-from .models import Post, Category, Comment
import os
-from django.conf import settings
+import re
+
from bs4 import BeautifulSoup
+from django.conf import settings
from pygments import highlight
-from pygments.lexers import get_lexer_by_name
-from pygments.lexers import guess_lexer
from pygments.formatters import HtmlFormatter
-import re
+from pygments.lexers import get_lexer_by_name, guess_lexer
+
+from .models import Category, Comment, Post
+
def add_excerpt(post):
soup = BeautifulSoup(post.body, 'html.parser')
diff --git a/blog/migrations/0001_initial.py b/blog/migrations/0001_initial.py
index fd8234e6..d6ef46c8 100644
--- a/blog/migrations/0001_initial.py
+++ b/blog/migrations/0001_initial.py
@@ -1,8 +1,8 @@
# Generated by Django 4.0.6 on 2022-09-19 00:31
+import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
-import django.db.models.deletion
class Migration(migrations.Migration):
diff --git a/blog/migrations/0013_post_views.py b/blog/migrations/0013_post_views.py
new file mode 100644
index 00000000..42244f1f
--- /dev/null
+++ b/blog/migrations/0013_post_views.py
@@ -0,0 +1,17 @@
+# Generated by Django 4.1.4 on 2023-05-28 04:39
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("blog", "0012_alter_post_date"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="post",
+ name="views",
+ field=models.IntegerField(default=0),
+ ),
+ ]
diff --git a/blog/models.py b/blog/models.py
index c9f03399..771a9561 100644
--- a/blog/models.py
+++ b/blog/models.py
@@ -1,6 +1,7 @@
-from django.db import models
from django.conf import settings
+from django.db import models
from django.utils.text import slugify
+
UPLOAD_ROOT = 'images/'
# Create your models here.
@@ -48,6 +49,7 @@ class Post(models.Model):
)
tags = models.ManyToManyField('Tag', blank=True)
is_public = models.BooleanField(default=False)
+ views = models.IntegerField(default=0)
def save(self, *args, **kwargs):
if not self.slug or self.slug == '':
diff --git a/blog/urls.py b/blog/urls.py
index 0910484a..d07ef152 100644
--- a/blog/urls.py
+++ b/blog/urls.py
@@ -1,5 +1,6 @@
from django.urls import path
from django.views.generic import RedirectView
+
from . import views
app_name = 'blog'
diff --git a/blog/views.py b/blog/views.py
index 0c21f9b6..a40aef35 100644
--- a/blog/views.py
+++ b/blog/views.py
@@ -1,22 +1,30 @@
+import os
+import re
from datetime import datetime
-from django.shortcuts import render, redirect, reverse
-from django.http import HttpResponse, Http404
-from users.models import UserProfile
-from django.contrib.auth.models import User
-from django.core.paginator import Paginator
from random import choice
from string import ascii_letters, digits
-from .models import Category, Post, Comment
-from .context_processors import recent_posts, avatar_list, add_excerpt, add_num_comments, highlight_code_blocks, comment_processor
+
+from bs4 import BeautifulSoup
+from django.contrib import messages
+from django.contrib.auth.models import User
+from django.core.cache import cache
+from django.core.paginator import Paginator
+from django.http import Http404, HttpResponse
+from django.shortcuts import redirect, render, reverse
+from dotenv import load_dotenv
+from haystack.query import SearchQuerySet
+from user_agents import parse
+
from announcements.models import Announcement
from users.forms import RegisterForm, UpdateUserDetailsForm
+from users.models import UserProfile
from users.tokens import CaptchaTokenGenerator
-from django.contrib import messages
-from bs4 import BeautifulSoup
-from haystack.query import SearchQuerySet
-import re
-import os
-from dotenv import load_dotenv
+
+from .context_processors import (add_excerpt, add_num_comments, avatar_list,
+ comment_processor, highlight_code_blocks,
+ recent_posts)
+from .models import Category, Comment, Post
+
load_dotenv()
@@ -101,6 +109,22 @@ def post(request, slug):
try:
post = Post.objects.get(slug=slug)
+ # Get the number of views for this post
+ x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
+ if x_forwarded_for:
+ ip = x_forwarded_for.split(',')[0]
+ else:
+ ip = request.META.get('REMOTE_ADDR')
+ user_agent_string = request.META.get('HTTP_USER_AGENT', '')
+ user_agent = parse(user_agent_string)
+ user_identifier = f'{ip}_{user_agent.browser.family}_{user_agent.browser.version_string}_{user_agent.os.family}_{user_agent.os.version_string}'
+ cache_key = f'post_view_count_{slug}_{user_identifier}'
+ view_count = cache.get(cache_key, 0)
+ if view_count == 0:
+ post.views += 1
+ post.save()
+ cache.set(cache_key, 1, 60 * 60 * 24 * 7) # 1 week
+
# code stored in .ql-syntax class
soup = BeautifulSoup(post.body, 'html.parser')
code_blocks = soup.find_all('pre', class_='ql-syntax')
@@ -140,10 +164,10 @@ def post(request, slug):
request.meta['description'] = BeautifulSoup(first_paragraph, 'html.parser').get_text()
request.meta['image'] = 'https://thatcomputerscientist.com/ignis/post_image/720/' + str(post.id) + '.gif'
- return render(request, 'blog/post.html', {'title': post.title, 'post': post, 'tags': tags, 'comments': comments})
+ return render(request, 'blog/post.html', {'title': post.title, 'post': post, 'tags': tags, 'comments': comments, 'view_count': view_count})
else:
if request.user.is_authenticated and request.user.is_superuser or request.user.is_staff:
- return render(request, 'blog/post.html', {'title': post.title, 'post': post, 'tags': tags, 'comments': comments})
+ return render(request, 'blog/post.html', {'title': post.title, 'post': post, 'tags': tags, 'comments': comments, 'view_count': view_count})
else:
raise Http404
except Post.DoesNotExist:
diff --git a/blog_admin/urls.py b/blog_admin/urls.py
index 444fef8e..9b02fa89 100644
--- a/blog_admin/urls.py
+++ b/blog_admin/urls.py
@@ -1,4 +1,5 @@
from django.urls import path
+
from . import views
app_name = 'blog-admin'
diff --git a/blog_admin/views.py b/blog_admin/views.py
index fd724faa..fff86217 100644
--- a/blog_admin/views.py
+++ b/blog_admin/views.py
@@ -1,10 +1,12 @@
+import re
from datetime import datetime
-from django.shortcuts import render, redirect, reverse
+
from django.contrib import messages
-from blog.models import Post, Category, Tag
-from ignis.models import CoverImage
-import re
from django.http import HttpResponseRedirect
+from django.shortcuts import redirect, render, reverse
+
+from blog.models import Category, Post, Tag
+from ignis.models import CoverImage
# Create your views here.
diff --git a/chat/chat_cache.py b/chat/chat_cache.py
index 08cbf178..43354ef7 100644
--- a/chat/chat_cache.py
+++ b/chat/chat_cache.py
@@ -1,6 +1,7 @@
-import redis
import json
+import redis
+
r = redis.Redis(host='localhost', port=6379, db=0)
def handle_connect():
diff --git a/chat/consumers.py b/chat/consumers.py
index 0b3017c4..da81449c 100644
--- a/chat/consumers.py
+++ b/chat/consumers.py
@@ -1,9 +1,13 @@
import json
-from channels.generic.websocket import WebsocketConsumer
+
from asgiref.sync import async_to_sync
-from .chat_cache import handle_connect, handle_disconnect, handle_alone_user, discard_user_messages
+from channels.generic.websocket import WebsocketConsumer
+
+from .chat_cache import (discard_user_messages, handle_alone_user,
+ handle_connect, handle_disconnect)
from .skippy import invokeMFSkippy
+
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.room_group_name = "chat"
diff --git a/chat/routing.py b/chat/routing.py
index 69101be1..fe6b4681 100644
--- a/chat/routing.py
+++ b/chat/routing.py
@@ -1,4 +1,5 @@
from django.urls import re_path
+
from . import consumers
websocket_urlpatterns = [
diff --git a/chat/skippy.py b/chat/skippy.py
index bf6ab6ac..6fa3aaa1 100644
--- a/chat/skippy.py
+++ b/chat/skippy.py
@@ -1,5 +1,6 @@
from .chat_cache import get_user_messages, save_user_messages
+
def invokeMFSkippy(message, identifier):
save_user_messages(user_identifier=identifier, message={'content': message, 'role': 'user'})
user_messages = get_user_messages(user_identifier=identifier)
diff --git a/dev_status/urls.py b/dev_status/urls.py
index c4250b78..62c4c3a2 100644
--- a/dev_status/urls.py
+++ b/dev_status/urls.py
@@ -1,5 +1,6 @@
from django.urls import path
from django.views.generic import RedirectView
+
from . import views
app_name = 'dev_status'
diff --git a/dev_status/views.py b/dev_status/views.py
index 0291159a..0a7adf7f 100644
--- a/dev_status/views.py
+++ b/dev_status/views.py
@@ -1,9 +1,10 @@
-from django.shortcuts import render
-from github import Github
-from dotenv import load_dotenv
+import math
import os
+
import requests
-import math
+from django.shortcuts import render
+from dotenv import load_dotenv
+from github import Github
load_dotenv()
g = Github(os.getenv('GH_TOKEN'))
diff --git a/ignis/admin.py b/ignis/admin.py
index b262f0d0..d481803d 100644
--- a/ignis/admin.py
+++ b/ignis/admin.py
@@ -1,7 +1,7 @@
from django.contrib import admin
# Register your models here.
-from .models import PostImage, RepositoryTitle, CoverImage
+from .models import CoverImage, PostImage, RepositoryTitle
admin.site.register(PostImage)
admin.site.register(RepositoryTitle)
diff --git a/ignis/migrations/0001_initial.py b/ignis/migrations/0001_initial.py
index 1cf08fda..e937dead 100644
--- a/ignis/migrations/0001_initial.py
+++ b/ignis/migrations/0001_initial.py
@@ -1,7 +1,7 @@
# Generated by Django 4.0.6 on 2022-11-22 06:31
-from django.db import migrations, models
import django.db.models.deletion
+from django.db import migrations, models
class Migration(migrations.Migration):
diff --git a/ignis/migrations/0003_remove_objectdirectory_name_objectdirectory_slug.py b/ignis/migrations/0003_remove_objectdirectory_name_objectdirectory_slug.py
index 72fea363..46a90281 100644
--- a/ignis/migrations/0003_remove_objectdirectory_name_objectdirectory_slug.py
+++ b/ignis/migrations/0003_remove_objectdirectory_name_objectdirectory_slug.py
@@ -1,7 +1,7 @@
# Generated by Django 4.0.6 on 2022-11-22 06:40
-from django.db import migrations, models
import django.db.models.deletion
+from django.db import migrations, models
class Migration(migrations.Migration):
diff --git a/ignis/migrations/0007_alter_object_location.py b/ignis/migrations/0007_alter_object_location.py
index c168f637..de1e9134 100644
--- a/ignis/migrations/0007_alter_object_location.py
+++ b/ignis/migrations/0007_alter_object_location.py
@@ -1,7 +1,7 @@
# Generated by Django 4.0.6 on 2022-11-22 12:00
-from django.db import migrations, models
import django.db.models.deletion
+from django.db import migrations, models
class Migration(migrations.Migration):
diff --git a/ignis/migrations/0008_postimage_repositoryimages_repositorytitles_and_more.py b/ignis/migrations/0008_postimage_repositoryimages_repositorytitles_and_more.py
index 8cd1b668..85a4658c 100644
--- a/ignis/migrations/0008_postimage_repositoryimages_repositorytitles_and_more.py
+++ b/ignis/migrations/0008_postimage_repositoryimages_repositorytitles_and_more.py
@@ -1,7 +1,7 @@
# Generated by Django 4.0.6 on 2022-11-22 15:13
-from django.db import migrations, models
import django.db.models.deletion
+from django.db import migrations, models
class Migration(migrations.Migration):
diff --git a/ignis/migrations/0010_alter_postimage_post.py b/ignis/migrations/0010_alter_postimage_post.py
index 3fda7708..4cd187a2 100644
--- a/ignis/migrations/0010_alter_postimage_post.py
+++ b/ignis/migrations/0010_alter_postimage_post.py
@@ -1,7 +1,7 @@
# Generated by Django 4.0.6 on 2022-11-22 15:30
-from django.db import migrations, models
import django.db.models.deletion
+from django.db import migrations, models
class Migration(migrations.Migration):
diff --git a/ignis/migrations/0012_coverimage.py b/ignis/migrations/0012_coverimage.py
index 0a35a322..48433ebe 100644
--- a/ignis/migrations/0012_coverimage.py
+++ b/ignis/migrations/0012_coverimage.py
@@ -1,7 +1,7 @@
# Generated by Django 4.1.4 on 2022-12-31 17:21
-from django.db import migrations, models
import django.db.models.deletion
+from django.db import migrations, models
class Migration(migrations.Migration):
diff --git a/ignis/models.py b/ignis/models.py
index e1f96669..0b8b099a 100644
--- a/ignis/models.py
+++ b/ignis/models.py
@@ -1,6 +1,7 @@
+from django.conf import settings
from django.db import models
+
from blog.models import Post
-from django.conf import settings
UPLOAD_ROOT = 'images/'
@@ -35,6 +36,7 @@ class RepositoryTitle(models.Model):
from django.db.models.signals import post_delete
from django.dispatch import receiver
+
@receiver(post_delete, sender=PostImage)
def submission_delete(sender, instance, **kwargs):
instance.image.delete(False)
diff --git a/ignis/urls.py b/ignis/urls.py
index 13cda935..dbc73a5d 100644
--- a/ignis/urls.py
+++ b/ignis/urls.py
@@ -1,4 +1,5 @@
from django.urls import path
+
from . import views
app_name = 'ignis'
diff --git a/ignis/views.py b/ignis/views.py
index 8a9b9894..4414bdb0 100644
--- a/ignis/views.py
+++ b/ignis/views.py
@@ -1,21 +1,22 @@
-from PIL import Image
-from io import BytesIO
-from django.http import HttpResponse
-from django.views.decorators.csrf import csrf_exempt
-from blog.models import Post
-from .models import PostImage, RepositoryTitle
import json
+import os
+import time
+from io import BytesIO
+
import requests
-from django.core.files.base import ContentFile
from captcha.image import ImageCaptcha
-from users.tokens import CaptchaTokenGenerator
+from django.core.files.base import ContentFile
from django.http import HttpResponse
from django.views.decorators.cache import never_cache
+from django.views.decorators.csrf import csrf_exempt
from PIL import Image
-from io import BytesIO
from selenium import webdriver
-import time
-import os
+
+from blog.models import Post
+from users.tokens import CaptchaTokenGenerator
+
+from .models import PostImage, RepositoryTitle
+
# from .github import get_cover
# Create your views here.
diff --git a/middleware/oldbrowsermiddleware.py b/middleware/oldbrowsermiddleware.py
index 979c8d20..6e19be1e 100644
--- a/middleware/oldbrowsermiddleware.py
+++ b/middleware/oldbrowsermiddleware.py
@@ -1,4 +1,6 @@
import re
+
+
class OldBrowserMiddleware:
def __init__(self, get_response):
self.get_response = get_response
diff --git a/middleware/subdomainmiddleware.py b/middleware/subdomainmiddleware.py
index b92d1e92..5157d909 100644
--- a/middleware/subdomainmiddleware.py
+++ b/middleware/subdomainmiddleware.py
@@ -1,5 +1,7 @@
from django.conf import settings
from django.shortcuts import redirect
+
+
class SubdomainMiddleware:
def __init__(self, get_response):
self.get_response = get_response
diff --git a/requirements.txt b/requirements.txt
index 92ee0500..40365543 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -20,3 +20,4 @@ django-haystack
channels
channels_redis
daphne
+user_agents
diff --git a/templates/blog/post.html b/templates/blog/post.html
index 67b9dda3..2a5de531 100644
--- a/templates/blog/post.html
+++ b/templates/blog/post.html
@@ -32,6 +32,8 @@
</a>
<span style="position: relative; top: 2.5px; margin-left: 4px;"><b>|</b></span>
<span style="position: relative; top: 2.5px; margin-left: 4px;">{% localtime on %}{{ post.date | date:"M d, Y" }}{% endlocaltime %}</span>
+ <span style="position: relative; top: 2.5px; margin-left: 4px;"><b>|</b></span>
+ <span style="position: relative; top: 2.5px; margin-left: 4px;">{{ post.views }} view{% if not post.views == 1%}s{% endif %}</span>
</div>
</div>
diff --git a/thatcomputerscientist/asgi.py b/thatcomputerscientist/asgi.py
index 11e01fe6..9766bd2e 100644
--- a/thatcomputerscientist/asgi.py
+++ b/thatcomputerscientist/asgi.py
@@ -9,9 +9,10 @@ https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
import os
-from django.core.asgi import get_asgi_application
-from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
+from channels.routing import ProtocolTypeRouter, URLRouter
+from django.core.asgi import get_asgi_application
+
import chat.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'thatcomputerscientist.settings')
diff --git a/thatcomputerscientist/backends.py b/thatcomputerscientist/backends.py
index 09867862..de0c8f1d 100644
--- a/thatcomputerscientist/backends.py
+++ b/thatcomputerscientist/backends.py
@@ -1,5 +1,6 @@
-from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
+from django.contrib.auth.backends import ModelBackend
+
class CaseInsensitiveModelBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
diff --git a/thatcomputerscientist/error_handler.py b/thatcomputerscientist/error_handler.py
index e04826c5..e22bdc3a 100644
--- a/thatcomputerscientist/error_handler.py
+++ b/thatcomputerscientist/error_handler.py
@@ -1,10 +1,13 @@
-from django.shortcuts import render
-from django.contrib.auth.models import User
-from blog.models import Post
import re
+
import jellyfish
+from django.contrib.auth.models import User
+from django.shortcuts import render
from fuzzywuzzy import process
+from blog.models import Post
+
+
def get_similar_posts(slug):
posts = Post.objects.all().filter(is_public=True)
diff --git a/thatcomputerscientist/search_indexes.py b/thatcomputerscientist/search_indexes.py
index edfff3a9..5f1684a0 100644
--- a/thatcomputerscientist/search_indexes.py
+++ b/thatcomputerscientist/search_indexes.py
@@ -1,6 +1,8 @@
-from haystack import indexes
-from blog.models import Post, Comment
from django.contrib.auth.models import User
+from haystack import indexes
+
+from blog.models import Comment, Post
+
class PostIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
diff --git a/thatcomputerscientist/settings.py b/thatcomputerscientist/settings.py
index aaf8bcbb..c345cbb0 100644
--- a/thatcomputerscientist/settings.py
+++ b/thatcomputerscientist/settings.py
@@ -10,9 +10,10 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
+import os
from pathlib import Path
+
from dotenv import load_dotenv
-import os
load_dotenv()
@@ -22,6 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# set n_connected_lc_users to 0 on startup
import redis
+
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('n_connected_lc_users', 0)
@@ -167,6 +169,8 @@ CACHES = {
}
}
+SESSION_ENGINE = "django.contrib.sessions.backends.cache"
+
# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
diff --git a/thatcomputerscientist/sitemaps.py b/thatcomputerscientist/sitemaps.py
index 4703686d..0c5cd5c8 100644
--- a/thatcomputerscientist/sitemaps.py
+++ b/thatcomputerscientist/sitemaps.py
@@ -1,9 +1,11 @@
+import os
+
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
-from blog.models import Post, Category, Tag
-from github import Github
from dotenv import load_dotenv
-import os
+from github import Github
+
+from blog.models import Category, Post, Tag
load_dotenv()
diff --git a/thatcomputerscientist/templatetags/ad.py b/thatcomputerscientist/templatetags/ad.py
index 4dfacfd9..58454a45 100644
--- a/thatcomputerscientist/templatetags/ad.py
+++ b/thatcomputerscientist/templatetags/ad.py
@@ -1,8 +1,9 @@
+import base64
import os
-from django.conf import settings
import random
+
from django import template
-import base64
+from django.conf import settings
register = template.Library()
diff --git a/thatcomputerscientist/templatetags/random_numbers.py b/thatcomputerscientist/templatetags/random_numbers.py
index 05bbbf6d..eb29990b 100644
--- a/thatcomputerscientist/templatetags/random_numbers.py
+++ b/thatcomputerscientist/templatetags/random_numbers.py
@@ -1,4 +1,5 @@
import random
+
from django import template
register = template.Library()
diff --git a/thatcomputerscientist/templatetags/remove_tags.py b/thatcomputerscientist/templatetags/remove_tags.py
index a07c25d4..31086090 100644
--- a/thatcomputerscientist/templatetags/remove_tags.py
+++ b/thatcomputerscientist/templatetags/remove_tags.py
@@ -1,6 +1,7 @@
-from django import template
import re
+from django import template
+
register = template.Library()
@register.filter(name='remove_tags')
diff --git a/thatcomputerscientist/urls.py b/thatcomputerscientist/urls.py
index 96cc8a9b..a2d9cdfd 100644
--- a/thatcomputerscientist/urls.py
+++ b/thatcomputerscientist/urls.py
@@ -13,14 +13,16 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
-from django.contrib import admin
-from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
+from django.contrib import admin
from django.contrib.sitemaps.views import sitemap
-from .sitemaps import PostSitemap, CategorySitemap, TagSitemap, StaticViewSitemap, GithubSitemap
+from django.urls import include, path
from haystack.views import search_view_factory
+from .sitemaps import (CategorySitemap, GithubSitemap, PostSitemap,
+ StaticViewSitemap, TagSitemap)
+
sitemaps = {
'posts': PostSitemap,
'categories': CategorySitemap,
diff --git a/userpages/urls.py b/userpages/urls.py
index f1c55f96..fe50a9f1 100644
--- a/userpages/urls.py
+++ b/userpages/urls.py
@@ -1,4 +1,5 @@
from django.urls import path
+
from . import views
# Configure the URL patterns for username.*
diff --git a/userpages/views.py b/userpages/views.py
index 46da5eb8..7ca89778 100644
--- a/userpages/views.py
+++ b/userpages/views.py
@@ -1,7 +1,9 @@
+from django.contrib.auth.models import User
+from django.http import Http404, HttpResponse
from django.shortcuts import render
-from django.http import HttpResponse, Http404
+
from users.models import UserProfile
-from django.contrib.auth.models import User
+
# Create your views here.
def home(request):
diff --git a/users/accountFunctions.py b/users/accountFunctions.py
index be60e29f..f5d77d72 100644
--- a/users/accountFunctions.py
+++ b/users/accountFunctions.py
@@ -1,8 +1,11 @@
-from users.models import TokenStore, UserProfile
-import uuid
import secrets
+import uuid
+
from django.utils import timezone
+from users.models import TokenStore, UserProfile
+
+
def generate_token():
uid = uuid.uuid4().hex
token = secrets.token_urlsafe(32)
diff --git a/users/admin.py b/users/admin.py
index 8d54136f..0f1d5fdb 100644
--- a/users/admin.py
+++ b/users/admin.py
@@ -1,7 +1,7 @@
from django.contrib import admin
# Register your models here.
-from .models import UserProfile, TokenStore
+from .models import TokenStore, UserProfile
admin.site.register(UserProfile)
admin.site.register(TokenStore)
diff --git a/users/forms.py b/users/forms.py
index 8ce104b1..e10da9f7 100644
--- a/users/forms.py
+++ b/users/forms.py
@@ -1,15 +1,20 @@
# Registration form
+import string
+from random import choice
+
from django import forms
from django.contrib.auth.models import User
-from users.models import UserProfile
from django.template.loader import render_to_string
from django.utils.html import strip_tags
+
+from blog.context_processors import avatar_list
+from users.models import UserProfile
+
from .accountFunctions import store_token
from .mail_send import send_email
-from random import choice
-from blog.context_processors import avatar_list
-import string
+
+
class RegisterForm(forms.Form):
username = forms.CharField(label='Username', max_length=30, min_length=4)
email = forms.EmailField(label='Email')
diff --git a/users/mail_send.py b/users/mail_send.py
index 742f84f5..be167eb1 100644
--- a/users/mail_send.py
+++ b/users/mail_send.py
@@ -1,10 +1,12 @@
# python script for sending SMTP configuration with Oracle Cloud Infrastructure Email Delivery
-import smtplib
import email.utils
-from email.message import EmailMessage
+import smtplib
import ssl
+from email.message import EmailMessage
+
from django.conf import settings
+
def send_email(sender, sender_name, recipient, subject, body_html, body_text):
# Replace [email protected] with your "From" address.
# This address must be verified.
diff --git a/users/migrations/0001_initial.py b/users/migrations/0001_initial.py
index fd66accf..a8a78c6b 100644
--- a/users/migrations/0001_initial.py
+++ b/users/migrations/0001_initial.py
@@ -1,8 +1,8 @@
# Generated by Django 4.0.6 on 2022-07-29 15:58
+import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
-import django.db.models.deletion
class Migration(migrations.Migration):
diff --git a/users/migrations/0011_tokenstore.py b/users/migrations/0011_tokenstore.py
index 78a5478b..f0bf9330 100644
--- a/users/migrations/0011_tokenstore.py
+++ b/users/migrations/0011_tokenstore.py
@@ -1,8 +1,8 @@
# Generated by Django 4.1.4 on 2023-04-30 03:19
+import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
-import django.db.models.deletion
class Migration(migrations.Migration):
diff --git a/users/models.py b/users/models.py
index 76dfc896..f11f2f46 100644
--- a/users/models.py
+++ b/users/models.py
@@ -1,6 +1,7 @@
from django.conf import settings
from django.db import models
+
# User Profile Model
class UserProfile(models.Model):
user = models.ForeignKey(
diff --git a/users/tokens.py b/users/tokens.py
index 1e481b3e..05d73362 100644
--- a/users/tokens.py
+++ b/users/tokens.py
@@ -1,8 +1,9 @@
import os
+
+from Crypto.Cipher import AES
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from dotenv import load_dotenv
from six import text_type
-from Crypto.Cipher import AES
load_dotenv()
diff --git a/users/urls.py b/users/urls.py
index 9e900638..132049b6 100644
--- a/users/urls.py
+++ b/users/urls.py
@@ -1,6 +1,7 @@
+from django.contrib import admin
from django.urls import path
+
from . import views
-from django.contrib import admin
app_name = 'users'
urlpatterns = [
diff --git a/users/views.py b/users/views.py
index 3780d80f..209fb1cd 100644
--- a/users/views.py
+++ b/users/views.py
@@ -1,14 +1,17 @@
-from django.http import HttpResponseRedirect, HttpResponse
-from django.shortcuts import redirect, reverse
-from django.contrib.auth import authenticate, login, logout, update_session_auth_hash
from django.contrib import messages
-from .models import UserProfile
+from django.contrib.auth import (authenticate, login, logout,
+ update_session_auth_hash)
from django.contrib.auth.models import User
+from django.http import HttpResponse, HttpResponseRedirect
+from django.shortcuts import redirect, reverse
from django.template.loader import render_to_string
from django.utils.html import strip_tags
-from .forms import UpdateUserDetailsForm
+
from .accountFunctions import store_token, verify_token
+from .forms import UpdateUserDetailsForm
from .mail_send import send_email
+from .models import UserProfile
+
# Create your views here.
def login_user(request):