Skip to content

Commit

Permalink
Merge pull request #41 from MaxCunningham19/chinaza/add-aspell-for-mvp
Browse files Browse the repository at this point in the history
Chinaza/add aspell for mvp
  • Loading branch information
ni-sauvage authored Feb 17, 2023
2 parents 612ce04 + a62f749 commit 7e56c69
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 36 deletions.
4 changes: 2 additions & 2 deletions api/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from api.models import Post
from api.models import Document

admin.site.register(Post)
admin.site.register(Document)
# Register your models here.
18 changes: 9 additions & 9 deletions api/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from . import models
from . import serializers

class PostList(generics.ListAPIView):
queryset = models.Post.objects.all()
serializer_class = serializers.PostSerializer
class DocumentList(generics.ListAPIView):
queryset = models.Document.objects.all()
serializer_class = serializers.DocumentSerializer

class PostDetail(generics.RetrieveAPIView):
queryset = models.Post.objects.all()
serializer_class = serializers.PostSerializer
class DocumentDetail(generics.RetrieveAPIView):
queryset = models.Document.objects.all()
serializer_class = serializers.DocumentSerializer


class PostDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = models.Post.objects.all()
serializer_class = serializers.PostSerializer
class DocumentDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = models.Document.objects.all()
serializer_class = serializers.DocumentSerializer
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.1.6 on 2023-02-13 23:30

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('api', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Block',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('block_content', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='Document',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=50)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
),
migrations.DeleteModel(
name='Post',
),
migrations.AddField(
model_name='block',
name='block_document',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.document'),
),
]
18 changes: 18 additions & 0 deletions api/migrations/0003_block_block_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.6 on 2023-02-13 23:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0002_block_document_delete_post_block_block_document'),
]

operations = [
migrations.AddField(
model_name='block',
name='block_order',
field=models.IntegerField(default=0),
),
]
34 changes: 31 additions & 3 deletions api/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
from django.db import models
from . import spellcheck

# Create your models here.
class Post(models.Model):
class Document(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return self.title
return self.title

class Block(models.Model):
block_document = models.ForeignKey(Document, on_delete=models.CASCADE)
"""
The parent document of a block.
"""

block_content = models.TextField()
"""
The text content of a block.
"""

block_order = models.IntegerField(default=0, unique=True)
"""
The index of a block within a document.
"""

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.block_content

def spellcheck(self) -> list[spellcheck.Mistake]:
"""
Apply a spellcheck function to the block contents.
"""
return spellcheck.check(self.block_content)
20 changes: 17 additions & 3 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
from rest_framework import serializers
from . import models

class PostSerializer(serializers.ModelSerializer):
class DocumentSerializer(serializers.ModelSerializer):
class Meta:
fields = ('id', 'title', 'content', 'created_at', 'updated_at',)
model = models.Post
fields = ('id', 'title', 'created_at', 'updated_at',)
model = models.Document

class BlockSerializer(serializers.ModelSerializer):
class Meta:
fields = ('block_order', 'block_content')
model = models.Block

class MistakeSerializer(serializers.Serializer):
word = serializers.CharField(max_length=200)
start = serializers.IntegerField()
end = serializers.IntegerField()
suggestions = serializers.ListField(
child=serializers.CharField(max_length=200),
allow_empty=True
)
59 changes: 59 additions & 0 deletions api/spellcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Module for spell check related code. Currently only contains aspell integration
for the MVP, but may be extended to use AWS Kendra, custom dictionaries,
consistency checking .etc.
"""

import subprocess

class Mistake():
def __init__(self, word: str, location: int, suggestions: list[str]):
self.word = word
"""
The actual word that was misspelt.
"""

self.start = location
"""
The character index at which the mistake starts.
"""

self.end = self.start + len(word)
"""
The character index at which the mistake ends (exclusive).
"""

self.suggestions = suggestions
"""
The suggestions returned by aspell.
"""

def __repr__(self) -> str:
return str(self.__dict__)

def check(content: str) -> list[Mistake]:
"""
Takes a string and returns an array of mistake objects representing
the errorrs in that string.
"""

raw_output = subprocess.run(
["aspell", "-a"],
text=True,
input=content,
stdout=subprocess.PIPE
).stdout

mistakes = []

for line in raw_output.splitlines()[1:]:
if len(line) == 0:
pass
elif line[0] == "&":
word, number_of_suggestions, location, *suggestions = line.split()[1:]
mistakes.append(Mistake(word, int(location.rstrip(":")), suggestions))
elif line[0] == "#":
word, location = line.split()[1:]
mistakes.append(Mistake(word, int(location.rstrip(":")), []))

return mistakes
10 changes: 10 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path
from .views import (get_document_blocks, check_document_blocks, DocumentList)

app_name = 'api'

urlpatterns = [
path('document/', DocumentList.as_view(), name='get_documents'),
path('document/<int:pk>', get_document_blocks, name='get_document_blocks'),
path('check/document/<int:pk>', check_document_blocks, name='check_document_blocks')
]
35 changes: 22 additions & 13 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
from django.shortcuts import render
import rest_framework
from django.shortcuts import render, get_list_or_404
from rest_framework import generics
from . import models
from . import serializers
from rest_framework.decorators import api_view
from .models import Document, Block
from .serializers import DocumentSerializer, BlockSerializer, MistakeSerializer

class PostList(generics.ListAPIView):
queryset = models.Post.objects.all()
serializer_class = serializers.PostSerializer
class DocumentList(generics.ListAPIView):
queryset = Document.objects.all()
serializer_class = DocumentSerializer

class PostDetail(generics.RetrieveAPIView):
queryset = models.Post.objects.all()
serializer_class = serializers.PostSerializer
class DocumentDetail(generics.RetrieveAPIView):
queryset = Document.objects.all()
serializer_class = DocumentSerializer

@api_view()
def get_document_blocks(request, pk):
blocks = Block.objects.filter(block_document=pk)
serializer = BlockSerializer(blocks, many=True)
return rest_framework.response.Response(serializer.data, status=200)

class PostDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = models.Post.objects.all()
serializer_class = serializers.PostSerializer
# Create your views here.
@api_view()
def check_document_blocks(request, pk):
mistakes = [dict(block_order=block.block_order,
mistakes=MistakeSerializer(block.spellcheck(), many=True).data) \
for block in Block.objects.filter(block_document=pk)]
return rest_framework.response.Response(mistakes, status=200)
4 changes: 2 additions & 2 deletions scripts/force-local-refresh.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
. scripts/install.sh
# Copy updated static files
echo yes | python3 manage.py collectstatic
python3 manage.py collectstatic --no-input
# Recompile javascript bundle
cd client; webpack --mode development
cd client; webpack --stats summary --mode development
# Restart httpd
1>/dev/null pgrep httpd && httpd -k restart || httpd -k start
3 changes: 2 additions & 1 deletion scripts/run-local.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
./scripts/force-local-refresh.sh
inotifyd /var/www/apache/scripts/on-change.sh server:c client:c
tail -f /var/log/apache2/error.log &
inotifyd /var/www/apache/scripts/on-change.sh server:c client:c api:c
6 changes: 3 additions & 3 deletions server/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path
from django.urls import path, include
from api import views

urlpatterns = [
path('', views.PostList.as_view()),
path('<int:pk>/', views.PostDetail.as_view()),]
path('api/', include('api.urls')),
]

0 comments on commit 7e56c69

Please sign in to comment.