Skip to content

Commit

Permalink
Patch username changing
Browse files Browse the repository at this point in the history
  • Loading branch information
polarity-ac committed Dec 25, 2021
1 parent 378be14 commit 8d8bd08
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
*.env
/arugo/.env
/db.sqlite3
/base/__pycache__
/arugo/__pycache__
/base/__pycache__/
/arugo/__pycache__/
/base/__init__.py
/arugo/__init__.py
/base/migrations/__pycache__/
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed base/migrations/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
31 changes: 30 additions & 1 deletion base/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from io import StringIO
import numpy as np
import math

import requests

def read_data(url):
try:
Expand Down Expand Up @@ -542,3 +542,32 @@ def validate_auth_query(query):
def discard_challenge(profile):
profile.in_progress = False
profile.save()

def update_username(username):
url = 'https://codeforces.com/profile/' + username
res = requests.get(url)
res_url = res.url
slash_pos = 0
for i in range(len(res_url)):
if res_url[i] == '/':
slash_pos = i
nxt_username = res_url[(slash_pos + 1):]
# return nxt_username

if nxt_username != username:
user = User.objects.get(username=username)
profile = Profile.objects.get(handle=username)
queries = AuthQuery.objects.filter(handle=username)

if len(queries):
queries.delete()

profile.handle = nxt_username
profile.save()

user.username = nxt_username
user.save()

print("updated to " + nxt_username)

return nxt_username
3 changes: 3 additions & 0 deletions base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def index(request):
context["user_count"] = len(User.objects.all())

if request.user.is_authenticated:
print(user.username)
profile = Profile.objects.get(user=user)

if request.method == "POST" and user.check_password(
Expand Down Expand Up @@ -109,6 +110,8 @@ def login_view(request):
user = authenticate(request, username=username, password=password)

if user is not None:
username = update_username(username)
user = authenticate(request, username=username, password=password)
login(request, user)
response = redirect("home-page")
return response
Expand Down

0 comments on commit 8d8bd08

Please sign in to comment.