Skip to content

Commit 196c679

Browse files
committed
- chore: format code with black
1 parent b604a4a commit 196c679

30 files changed

+231
-221
lines changed

api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
default_app_config = 'api.apps.CoreConfig'
1+
default_app_config = "api.apps.CoreConfig"

api/apps.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33

44
class CoreConfig(AppConfig):
5-
default_auto_field = 'django.db.models.BigAutoField'
6-
name = 'api'
7-
label = 'api'
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "api"
7+
label = "api"

api/authentication/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
default_app_config = 'api.authentication.apps.AuthenticationConfig'
1+
default_app_config = "api.authentication.apps.AuthenticationConfig"

api/authentication/apps.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
class AuthenticationConfig(AppConfig):
5-
default_auto_field = 'django.db.models.BigAutoField'
6-
name = 'api.authentication'
7-
label = 'api_authentication'
8-
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "api.authentication"
7+
label = "api_authentication"

api/authentication/backends.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99

1010
class ActiveSessionAuthentication(authentication.BaseAuthentication):
1111

12-
auth_error_message = {
13-
"success": False,
14-
"msg": "User is not logged on."
15-
}
12+
auth_error_message = {"success": False, "msg": "User is not logged on."}
1613

1714
def authenticate(self, request):
1815

@@ -23,7 +20,7 @@ def authenticate(self, request):
2320
if not auth_header:
2421
return None
2522

26-
token = auth_header.decode('utf-8')
23+
token = auth_header.decode("utf-8")
2724

2825
return self._authenticate_credentials(token)
2926

@@ -42,17 +39,11 @@ def _authenticate_credentials(self, token):
4239
try:
4340
user = active_session.user
4441
except User.DoesNotExist:
45-
msg = {
46-
"success": False,
47-
"msg": 'No user matching this token was found.'
48-
}
42+
msg = {"success": False, "msg": "No user matching this token was found."}
4943
raise exceptions.AuthenticationFailed(msg)
5044

5145
if not user.is_active:
52-
msg = {
53-
"success": False,
54-
"msg": 'This user has been deactivated.'
55-
}
46+
msg = {"success": False, "msg": "This user has been deactivated."}
5647
raise exceptions.AuthenticationFailed(msg)
5748

5849
return (user, token)

api/authentication/migrations/0001_initial.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,26 @@ class Migration(migrations.Migration):
1515

1616
operations = [
1717
migrations.CreateModel(
18-
name='ActiveSession',
18+
name="ActiveSession",
1919
fields=[
20-
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21-
('token', models.CharField(max_length=255)),
22-
('date', models.DateTimeField(auto_now_add=True)),
23-
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
20+
(
21+
"id",
22+
models.BigAutoField(
23+
auto_created=True,
24+
primary_key=True,
25+
serialize=False,
26+
verbose_name="ID",
27+
),
28+
),
29+
("token", models.CharField(max_length=255)),
30+
("date", models.DateTimeField(auto_now_add=True)),
31+
(
32+
"user",
33+
models.ForeignKey(
34+
on_delete=django.db.models.deletion.CASCADE,
35+
to=settings.AUTH_USER_MODEL,
36+
),
37+
),
2438
],
2539
),
2640
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .active_session import ActiveSession
1+
from .active_session import ActiveSession

api/authentication/models/active_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
class ActiveSession(models.Model):
55
user = models.ForeignKey("api_user.User", on_delete=models.CASCADE)
66
token = models.CharField(max_length=255)
7-
date = models.DateTimeField(auto_now_add=True)
7+
date = models.DateTimeField(auto_now_add=True)

api/authentication/serializers/login.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010

1111
def _generate_jwt_token(user):
12-
token = jwt.encode({
13-
'id': user.pk,
14-
'exp': datetime.utcnow() + timedelta(days=7)
15-
}, settings.SECRET_KEY)
12+
token = jwt.encode(
13+
{"id": user.pk, "exp": datetime.utcnow() + timedelta(days=7)},
14+
settings.SECRET_KEY,
15+
)
1616

1717
return token
1818

@@ -23,62 +23,43 @@ class LoginSerializer(serializers.Serializer):
2323
password = serializers.CharField(max_length=128, write_only=True)
2424

2525
def validate(self, data):
26-
email = data.get('email', None)
27-
password = data.get('password', None)
26+
email = data.get("email", None)
27+
password = data.get("password", None)
2828

2929
if email is None:
3030
raise serializers.ValidationError(
31-
{
32-
"success": False,
33-
"msg": "Email is required to login"
34-
}
31+
{"success": False, "msg": "Email is required to login"}
3532
)
3633
if password is None:
3734
raise serializers.ValidationError(
38-
{
39-
"success": False,
40-
"msg": "Password is required to log in."
41-
}
35+
{"success": False, "msg": "Password is required to log in."}
4236
)
4337
user = authenticate(username=email, password=password)
4438

4539
if user is None:
4640
raise serializers.ValidationError(
47-
{
48-
"success": False,
49-
"msg": "Wrong credentials"
50-
}
41+
{"success": False, "msg": "Wrong credentials"}
5142
)
5243

5344
if not user.is_active:
5445
raise serializers.ValidationError(
55-
{
56-
"success": False,
57-
"msg": "User is not active"
58-
}
46+
{"success": False, "msg": "User is not active"}
5947
)
6048

6149
try:
62-
session = ActiveSession.objects.get(
63-
user=user
64-
)
50+
session = ActiveSession.objects.get(user=user)
6551
if not session.token:
6652
raise ValueError
6753

6854
jwt.decode(session.token, settings.SECRET_KEY, algorithms=["HS256"])
6955

7056
except (ObjectDoesNotExist, ValueError, jwt.ExpiredSignatureError):
7157
session = ActiveSession.objects.create(
72-
user=user,
73-
token=_generate_jwt_token(user)
58+
user=user, token=_generate_jwt_token(user)
7459
)
7560

7661
return {
7762
"success": True,
7863
"token": session.token,
79-
"user": {
80-
"_id": user.pk,
81-
"username": user.username,
82-
"email": user.email
83-
}
64+
"user": {"_id": user.pk, "username": user.username, "email": user.email},
8465
}

api/authentication/serializers/register.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,4 @@ def create(self, validated_data):
2020
except ObjectDoesNotExist:
2121
return User.objects.create_user(**validated_data)
2222

23-
raise ValidationError({
24-
"success": False,
25-
"msg": "Email already taken"
26-
}
27-
)
23+
raise ValidationError({"success": False, "msg": "Email already taken"})

0 commit comments

Comments
 (0)