Skip to content

Commit

Permalink
Merge pull request #43 from mantiumai/test-and-linter-actions
Browse files Browse the repository at this point in the history
Test and linter GiHub actions
  • Loading branch information
zimventures authored Jul 7, 2023
2 parents 970805c + 3e02156 commit 16f1d89
Show file tree
Hide file tree
Showing 68 changed files with 486 additions and 369 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/django-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Django CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: ["3.10", "3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pylint
pip install pylint-django
pip install isort
- name: Generate Fernet Key
run: |
cd chirps;
python -c "from cryptography.fernet import Fernet; print(f'FERNET_KEY={Fernet.generate_key().decode()}')" > .env
- name: Run Tests
run: |
cd chirps; python manage.py test
- name: Run Linting
run: |
isort --check-only --diff .
pylint --load-plugins pylint_django --django-settings-module="chirps.settings" $(find . -name "*.py" | xargs)
8 changes: 8 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
[FORMAT]
max-line-length=120

[MASTER]
# Ignore Django migration files
ignore-patterns=\d{4}_.*?.py

[MESSAGES CONTROL]
disable=duplicate-code,too-few-public-methods,imported-auth-user
ignored-modules=pinecone
23 changes: 22 additions & 1 deletion chirps/account/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
"""Define admin interface models for the account application."""
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User

# Register your models here.
from .models import Profile


class ProfileInline(admin.StackedInline):
"""Inline admin descriptor for the Profile model."""

model = Profile
can_delete = False
verbose_name_plural = 'profile'


class UserAdmin(BaseUserAdmin):
"""Define a new User admin."""
inlines = [ProfileInline]


# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
3 changes: 3 additions & 0 deletions chirps/account/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Configures the account app."""""
from django.apps import AppConfig


class AccountConfig(AppConfig):
"""Configuration options for the account application."""

default_auto_field = 'django.db.models.BigAutoField'
name = 'account'
9 changes: 9 additions & 0 deletions chirps/account/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Form classes for the account app."""
from django import forms
from django.contrib.auth.hashers import make_password
from django.forms import ModelForm
Expand All @@ -6,21 +7,29 @@


class ProfileForm(ModelForm):
"""Form for the Profile model."""

def clean_openai_key(self):
"""Hash the openai_key before saving it to the database."""
data = self.cleaned_data['openai_key']
return make_password(data)

class Meta:
"""Meta class for ProfileForm."""
model = Profile
fields = ['openai_key']

widgets = {'openai_key': forms.PasswordInput(attrs={'class': 'form-control'})}

class LoginForm(forms.Form):
"""Form for logging in."""

username = forms.CharField(max_length=256)
password = forms.CharField(max_length=256, widget=forms.PasswordInput)

class SignupForm(forms.Form):
"""Form for signing up."""

username = forms.CharField(max_length=256)
email = forms.EmailField(max_length=256)
password1 = forms.CharField(max_length=256, widget=forms.PasswordInput)
Expand Down
2 changes: 1 addition & 1 deletion chirps/account/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by Django 4.2.2 on 2023-06-29 13:56

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):
Expand Down
23 changes: 3 additions & 20 deletions chirps/account/models.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
"""Models for the account appliation."""
from django.contrib.auth.models import User
from django.db import models


class Profile(models.Model):
"""Custom profile model for users."""

user = models.OneToOneField(User, on_delete=models.CASCADE)
openai_key = models.CharField(max_length=100, blank=True)


# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class ProfileInline(admin.StackedInline):
model = Profile
can_delete = False
verbose_name_plural = 'profile'


# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = [ProfileInline]


# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
7 changes: 5 additions & 2 deletions chirps/account/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from django.contrib.auth.hashers import check_password, make_password
"""Tests for the account application."""
from django.test import TestCase
from django.urls import reverse

from .forms import ProfileForm
from django.contrib.auth.forms import AuthenticationForm


class AccountTests(TestCase):
"""Main test class for the account application."""

def test_openai_key_hash(self):
"""Verify that the openai_key paramater is correctly hashed by the form"""
secret_val = 'secret_12345abcd'
Expand Down
5 changes: 3 additions & 2 deletions chirps/account/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""URLs for the account app."""
from django.contrib.auth import views as auth_views
from django.urls import path

from .views import login_view
from .views import profile as profile_view
from .views import signup, login_view
from .views import signup

urlpatterns = [
# path('login/', auth_views.LoginView.as_view(template_name='account/login.html', next_page='/'), name='login'),
path('login/', login_view, name='login'),
path(
'logout/', auth_views.LogoutView.as_view(template_name='account/logout.html', next_page='login'), name='logout'
Expand Down
23 changes: 13 additions & 10 deletions chirps/account/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
"""Views for the account application."""
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User # noqa: E5142
from django.shortcuts import redirect, render
from django.urls import reverse
from django.contrib.auth.models import User
from django.contrib.auth import login

from .forms import LoginForm, ProfileForm, SignupForm
from .models import Profile

from .forms import ProfileForm, SignupForm, LoginForm
from django.contrib.auth import authenticate

def profile(request):

"""Render the user profile page and handle updates"""
if request.method == 'POST':
form = ProfileForm(request.POST, instance=request.user.profile)

if form.is_valid():

profile = form.save(commit=False)
profile.user = request.user
profile.save()
profile_form = form.save(commit=False)
profile_form.user = request.user
profile_form.save()

# Redirect the user back to the dashboard
return redirect('profile')
Expand All @@ -27,6 +28,7 @@ def profile(request):
return render(request, 'account/profile.html', {'form': form})

def signup(request):
"""Render the signup page and handle posts."""
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
Expand All @@ -53,8 +55,8 @@ def signup(request):
user.save()

# Create the user profile
profile = Profile(user=user)
profile.save()
user_profile = Profile(user=user)
user_profile.save()

# Login the user
login(request, user)
Expand All @@ -67,6 +69,7 @@ def signup(request):
return render(request, 'account/signup.html', {'form': form})

def login_view(request):
"""Render the login page."""

# If there are no users, redirect to the installation page
if User.objects.count() == 0:
Expand Down
4 changes: 1 addition & 3 deletions chirps/base_app/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from django.contrib import admin

# Register your models here.
"""Define any admin interface models for the base application."""
3 changes: 3 additions & 0 deletions chirps/base_app/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Application configration for the base_app app."""
from django.apps import AppConfig


class BaseAppConfig(AppConfig):
"""Application configration for the base_app app."""

default_auto_field = 'django.db.models.BigAutoField'
name = 'base_app'
11 changes: 8 additions & 3 deletions chirps/base_app/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Forms for the base application."""
from django import forms

class InstallForm(forms.Form):

class InstallForm(forms.Form):
"""Form to render the new installation page."""
superuser_username = forms.CharField(label='Superuser Username', max_length=100)
superuser_email = forms.EmailField(label='Superuser Email', max_length=100)
superuser_password = forms.CharField(label='Superuser Password', max_length=100, widget=forms.PasswordInput)
superuser_password_confirm = forms.CharField(label='Superuser Password (Confirm)', max_length=100, widget=forms.PasswordInput)

superuser_password_confirm = forms.CharField(
label='Superuser Password (Confirm)',
max_length=100,
widget=forms.PasswordInput
)
6 changes: 5 additions & 1 deletion chirps/base_app/management/commands/celery.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Celery management command."""
import os

from django.core.management.base import BaseCommand


class Command(BaseCommand):
"""Manage a local celery installation with this command."""
help = 'Interact with the local celery broker'

def add_arguments(self, parser):
Expand All @@ -13,7 +15,7 @@ def add_arguments(self, parser):
parser.add_argument('--restart', action='store_true', help='Restart celery server')

def handle(self, *args, **options):

"""Handle the command."""
if options['start']:
self.start()
elif options['stop']:
Expand All @@ -23,9 +25,11 @@ def handle(self, *args, **options):
self.start()

def start(self):
"""Start the celery server."""
os.system('sudo mkdir -p /var/run/celery; sudo chmod 777 /var/run/celery')
os.system('sudo mkdir -p /var/log/celery; sudo chmod 777 /var/log/celery')
os.system('celery multi start w1 -A chirps -l INFO')

def stop(self):
"""Stop the celery server."""
os.system('celery multi stopwait w1 -A chirps -l INFO')
Loading

0 comments on commit 16f1d89

Please sign in to comment.