Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions twisted/twisted_site/hca.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import requests

from .models import Profile

HCA_BASE_URL = "https://auth.hackclub.com/api/v1"


Expand Down Expand Up @@ -45,10 +43,8 @@ def get_auth_headers(access_token: str, headers: dict[str, str] | None = None) -
return {"Authorization": f"Bearer {access_token}", **headers}


def get_user_data(profile: Profile) -> Identity:
access_token = profile.hca_access_token

headers = get_auth_headers(access_token) # ty: ignore[invalid-argument-type]
def get_user_data(access_token: str) -> Identity:
headers = get_auth_headers(access_token)

r = requests.get(HCA_BASE_URL + "/me", headers=headers, timeout=10)
r.raise_for_status()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 6.0.7 on 2026-09-19 03:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('twisted_site', '0033_project_hackatime_project_names'),
]

operations = [
migrations.AddField(
model_name='profile',
name='country',
field=models.CharField(blank=True, default='', max_length=20),
),
migrations.AddField(
model_name='profile',
name='country_cached_until',
field=models.DateTimeField(default=None, null=True),
),
]
23 changes: 21 additions & 2 deletions twisted/twisted_site/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from django.db import models
from django.db.models import TextField
from django.utils import timezone
from requests import HTTPError

from . import hackatime
from . import hackatime, hca

if TYPE_CHECKING:
from datetime import datetime
Expand Down Expand Up @@ -67,9 +68,27 @@ class Profile(models.Model):
)
my_referral_code = models.CharField(max_length=200, blank=True, default="")

country = models.CharField(max_length=20, default="", blank=True)
country_cached_until = models.DateTimeField(null=True, default=None)

@override
def __str__(self) -> str:
return cast("str", self.user.username) # pyrefly: ignore[missing-attribute]
return cast("str", self.user.username) # pyrefly: ignore[missing-attribute] # ty: ignore[unresolved-attribute]

def get_country(self) -> str:
if self.country_cached_until is not None and self.country_cached_until > timezone.now():
return str(self.country)
try:
user_data = hca.get_user_data(self.hca_access_token) # ty: ignore[invalid-argument-type]
country = user_data.primary_address.country if user_data.primary_address else "Unknown"
except HTTPError:
country = "Unknown"

self.country = country # ty: ignore[invalid-assignment]
self.country_cached_until = timezone.now() + timezone.timedelta(hours=3)
self.save()

return country

def shipped_projects(self) -> list["Project"]:
return [
Expand Down
8 changes: 1 addition & 7 deletions twisted/twisted_site/views/admin/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from requests.exceptions import HTTPError

from twisted_site import hca
from twisted_site.models import Journal

from .admin import AdminView
Expand Down Expand Up @@ -35,11 +33,7 @@ def get(self, request: HttpRequest) -> HttpResponse:

logged_project_type[journal.project.get_project_type_display()] += hours

try:
iden = hca.get_user_data(journal.project.user.profile)
country = iden.primary_address.country if iden.primary_address else "Unknown"
except HTTPError:
country = "Unknown"
country = journal.project.user.profile.get_country()

logged_region_hours.setdefault(country, 0)
logged_region_hours[country] += hours
Expand Down
Loading