-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from MaxCunningham19/chinaza/add-aspell-for-mvp
Chinaza/add aspell for mvp
- Loading branch information
Showing
12 changed files
with
215 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
api/migrations/0002_block_document_delete_post_block_block_document.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')), | ||
] |