Skip to content

Commit

Permalink
fix: Adds Linter CI and maintains formatting across codebase (#497)
Browse files Browse the repository at this point in the history
* ci(lint): added linter CI

Signed-off-by: Abhiuday <[email protected]>

* fix(pre-commit): updates the linter version

Signed-off-by: Abhiuday <[email protected]>

* fix(ayushma): fixed format across codebase

Signed-off-by: Abhiuday <[email protected]>

* ci(lint): fixed CI

Signed-off-by: Abhiuday <[email protected]>

---------

Signed-off-by: Abhiuday <[email protected]>
  • Loading branch information
aeswibon authored Apr 2, 2024
1 parent bde9e60 commit 9533035
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 91 deletions.
3 changes: 0 additions & 3 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ ENV PYTHONUNBUFFERED 1
# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>



32 changes: 32 additions & 0 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Lint Code Base
on:
pull_request:
branches:
- master
jobs:
build:
if: github.repository == 'coronasafe/ayushma'
name: Lint Code Base
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
statuses: write
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Lint Code Base
uses: super-linter/super-linter/slim@v6
env:
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_ALL_CODEBASE: false
VALIDATE_PYTHON_BLACK: true
VALIDATE_PYTHON_FLAKE8: true
VALIDATE_PYTHON_ISORT: true
LINTER_RULES_PATH: /
PYTHON_FLAKE8_CONFIG_FILE: "setup.cfg"
PYTHON_BLACK_CONFIG_FILE: "pyproject.toml"
PYTHON_ISORT_CONFIG_FILE: "pyproject.toml"
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
exclude: "docs|node_modules|migrations|.git|.venv"
default_stages: [commit]
fail_fast: true

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort

additional_dependencies: ["isort[pyproject]"]
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 24.3.0
hooks:
- id: black

args: ["--config=pyproject.toml"]
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
rev: 7.0.0
hooks:
- id: flake8
args: ["--config=setup.cfg"]
additional_dependencies: [flake8-isort]
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
}
},
"editor.formatOnSave": true,
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ logs:

makemigrations: up
docker exec django bash -c "python manage.py makemigrations"

checkmigration:
docker compose -f $(docker_config_file) exec django bash -c "python manage.py makemigrations --check --dry-run"

Expand Down
11 changes: 9 additions & 2 deletions ayushma/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from djangoql.admin import DjangoQLSearchMixin
from simple_history.admin import SimpleHistoryAdmin

from ayushma.models.services import Service, TempToken
from ayushma.models.testsuite import Feedback, TestQuestion, TestRun, TestSuite
Expand Down Expand Up @@ -32,7 +31,15 @@ class UserAdmin(DjangoQLSearchMixin, BaseUserAdmin):
fieldsets = (
(
None,
{"fields": ("email", "username", "password", "external_id", "allow_key")},
{
"fields": (
"email",
"username",
"password",
"external_id",
"allow_key",
)
},
),
(
"User info",
Expand Down
2 changes: 1 addition & 1 deletion ayushma/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ class AyushmaConfig(AppConfig):
name = "ayushma"

def ready(self):
#from .signals import # noqa: F401
# from .signals import # noqa: F401
pass
5 changes: 3 additions & 2 deletions ayushma/management/commands/upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class Command(BaseCommand):
def handle(self, *args, **options):
upsert_dir = "upsert"
pinecone.init(
api_key=settings.PINECONE_API_KEY, environment=settings.PINECONE_ENVIRONMENT
api_key=settings.PINECONE_API_KEY,
environment=settings.PINECONE_ENVIRONMENT,
)
print("Initialized Pinecone and OpenAI")

Expand All @@ -49,7 +50,7 @@ def handle(self, *args, **options):

batch_size = 100 # process everything in batches of 100 (creates 100 vectors per upset)

print(f"Fetching Pinecone index...")
print("Fetching Pinecone index...")
if settings.PINECONE_INDEX not in pinecone.list_indexes():
pinecone.create_index(
settings.PINECONE_INDEX,
Expand Down
1 change: 0 additions & 1 deletion ayushma/models/services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import secrets
import time

from django.db import models
from django.utils import timezone
Expand Down
6 changes: 2 additions & 4 deletions ayushma/models/token.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.conf import settings

from ayushma.token import RandomStringTokenGenerator
from utils.models.base import BaseModel
Expand All @@ -11,9 +11,7 @@ class ResetPasswordToken(BaseModel):
settings.AUTH_USER_MODEL,
related_name="password_reset_tokens",
on_delete=models.CASCADE,
verbose_name=_(
"The User which is associated to this password reset token"
),
verbose_name=_("The User which is associated to this password reset token"),
)
created_at = models.DateTimeField(
auto_now_add=True, verbose_name=_("When was this token generated")
Expand Down
2 changes: 0 additions & 2 deletions ayushma/permissions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from ipaddress import ip_address

from django.utils import timezone
from rest_framework import permissions

Expand Down
15 changes: 7 additions & 8 deletions ayushma/serializers/document.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import os
import uuid

from django.conf import settings
from django.core.files.storage import FileSystemStorage
from rest_framework import serializers
from rest_framework.response import Response

from ayushma.models import Document, DocumentType
from ayushma.models import Document


class DocumentSerializer(serializers.ModelSerializer):
Expand All @@ -23,7 +17,12 @@ class Meta:
"text_content",
"uploading",
)
read_only_fields = ("external_id", "created_at", "modified_at", "uploading")
read_only_fields = (
"external_id",
"created_at",
"modified_at",
"uploading",
)
write_only_fields = ("file",)

def validate(self, data):
Expand Down
9 changes: 7 additions & 2 deletions ayushma/serializers/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
TestSuite,
)
from ayushma.serializers.document import DocumentSerializer
from ayushma.serializers.project import ProjectSerializer, ProjectUpdateSerializer
from ayushma.serializers.project import ProjectUpdateSerializer
from ayushma.serializers.users import UserSerializer


Expand Down Expand Up @@ -67,7 +67,12 @@ class Meta:
"modified_at",
)

read_only_fields = ("user_object", "external_id", "created_at", "modified_at")
read_only_fields = (
"user_object",
"external_id",
"created_at",
"modified_at",
)


class TestResultSerializer(serializers.ModelSerializer):
Expand Down
4 changes: 1 addition & 3 deletions ayushma/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ def __init__(self, length=6):
self.length = length

def generate(self):
return "".join(
[random.choice(string.digits) for _ in range(self.length)]
)
return "".join([random.choice(string.digits) for _ in range(self.length)])
7 changes: 4 additions & 3 deletions ayushma/utils/converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def converse_api(
converse_type = "audio" if audio else "text"

# convert stream to boolean
if type(stream) != bool:
if not isinstance(stream, bool):
if stream == "false":
stream = False
else:
Expand All @@ -82,7 +82,7 @@ def converse_api(
if is_thread:
stream = False # Threads do not support streaming

if type(generate_audio) != bool:
if not isinstance(generate_audio, bool):
if generate_audio == "false":
generate_audio = False
else:
Expand Down Expand Up @@ -195,7 +195,8 @@ def converse_api(
response_message = list(response_message)[0]

return Response(
ChatMessageSerializer(response_message).data, status=status.HTTP_200_OK
ChatMessageSerializer(response_message).data,
status=status.HTTP_200_OK,
)

return response
15 changes: 5 additions & 10 deletions ayushma/views/auth.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
from drf_spectacular.utils import (
extend_schema,
OpenApiResponse,
)
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework import status
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from rest_framework.decorators import action
from rest_framework.exceptions import ParseError
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from ayushma.serializers.auth import AuthSerializer
from ayushma.serializers.users import (
UserCreateSerializer,
)
from ayushma.serializers.users import UserCreateSerializer


class AuthViewSet(GenericViewSet):
Expand Down
8 changes: 5 additions & 3 deletions ayushma/views/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.conf import settings
from drf_spectacular.utils import extend_schema
from rest_framework import filters, status
from rest_framework.decorators import action, api_view, permission_classes
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.mixins import (
CreateModelMixin,
Expand Down Expand Up @@ -122,7 +122,8 @@ def speech_to_text(self, *args, **kwarg):
stt_engine = Project.objects.get(external_id=project_id).stt_engine
except Project.DoesNotExist:
return Response(
{"error": "Project not found"}, status=status.HTTP_400_BAD_REQUEST
{"error": "Project not found"},
status=status.HTTP_400_BAD_REQUEST,
)
try:
stats["transcript_start_time"] = time.time()
Expand All @@ -139,7 +140,8 @@ def speech_to_text(self, *args, **kwarg):
)

return Response(
{"transcript": translated_text, "stats": stats}, status=status.HTTP_200_OK
{"transcript": translated_text, "stats": stats},
status=status.HTTP_200_OK,
)

@extend_schema(
Expand Down
6 changes: 1 addition & 5 deletions ayushma/views/orphan.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ class APIKeyAuth(permissions.BasePermission):
def has_permission(self, request, view):
if request.headers.get("X-API-KEY"):
api_key = request.headers.get("X-API-KEY")
try:
APIKey.objects.get(key=api_key)
return True
except APIKey.DoesNotExist:
return False
return APIKey.objects.filter(key=api_key).exists()


class OrphanChatViewSet(
Expand Down
28 changes: 3 additions & 25 deletions ayushma/views/service.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
import time
from types import SimpleNamespace

import openai
from django.conf import settings
from django.http import StreamingHttpResponse
from drf_spectacular.utils import extend_schema, extend_schema_view, inline_serializer
from rest_framework import permissions, status
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework import permissions
from rest_framework.mixins import CreateModelMixin
from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response
from rest_framework.serializers import CharField, IntegerField

from ayushma.models import APIKey, Chat, ChatMessage, Project
from ayushma.serializers import ChatDetailSerializer, ConverseSerializer
from ayushma.models import APIKey
from ayushma.serializers.services import TempTokenSerializer
from ayushma.utils.converse import converse_api
from ayushma.utils.language_helpers import translate_text
from ayushma.utils.openaiapi import converse
from utils.views.base import BaseModelViewSet

from .chat import ChatViewSet


class Struct:
def __init__(self, **entries):
Expand All @@ -33,11 +15,7 @@ class APIKeyAuth(permissions.BasePermission):
def has_permission(self, request, view):
if request.headers.get("X-API-KEY"):
api_key = request.headers.get("X-API-KEY")
try:
key = APIKey.objects.get(key=api_key)
return True
except APIKey.DoesNotExist:
return False
return APIKey.objects.filter(key=api_key).exists()


class TempTokenViewSet(BaseModelViewSet, CreateModelMixin):
Expand Down
2 changes: 1 addition & 1 deletion ayushma/views/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def validation(password):

# find the token
try:
reset_password_token = ResetPasswordToken.objects.get(key=token)
ResetPasswordToken.objects.get(key=token)
except ResetPasswordToken.DoesNotExist:
return Response(status=status.HTTP_400_BAD_REQUEST)

Expand Down
4 changes: 1 addition & 3 deletions core/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
# WhiteNoise
# ------------------------------------------------------------------------------
# http://whitenoise.evans.io/en/latest/django.html#using-whitenoise-in-development
INSTALLED_APPS = [
"whitenoise.runserver_nostatic"
] + INSTALLED_APPS # noqa F405
INSTALLED_APPS = ["whitenoise.runserver_nostatic"] + INSTALLED_APPS # noqa F405


# django-debug-toolbar
Expand Down
Loading

0 comments on commit 9533035

Please sign in to comment.