add comment function

This commit is contained in:
tim-vogel
2022-08-07 12:41:30 -07:00
committed by Michael Shamoon
parent c2fda245ac
commit 278e9c12e1
20 changed files with 416 additions and 1 deletions

View File

@@ -12,6 +12,7 @@ from django.core import serializers
from django.core.management.base import BaseCommand
from django.core.management.base import CommandError
from django.db import transaction
from documents.models import Comment
from documents.models import Correspondent
from documents.models import Document
from documents.models import DocumentType
@@ -126,6 +127,9 @@ class Command(BaseCommand):
serializers.serialize("json", DocumentType.objects.all()),
)
manifest += json.loads(
serializers.serialize("json", Comment.objects.all())),
documents = Document.objects.order_by("id")
document_map = {d.pk: d for d in documents}
document_manifest = json.loads(serializers.serialize("json", documents))

View File

@@ -0,0 +1,19 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('documents', '1016_auto_20210317_1351'),
]
operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('comment', models.TextField()),
('created', models.DateTimeField(auto_now_add=True)),
('document_id', models.PositiveIntegerField()),
('user_id', models.PositiveIntegerField())
],
)
]

View File

@@ -537,3 +537,40 @@ class PaperlessTask(models.Model):
blank=True,
)
acknowledged = models.BooleanField(default=False)
class Comment(models.Model):
comment = models.TextField(
_("content"),
blank=True,
help_text=_("Comment for the document")
)
created = models.DateTimeField(
_("created"),
default=timezone.now, db_index=True)
document = models.ForeignKey(
Document,
blank=True,
null=True,
related_name="documents",
on_delete=models.CASCADE,
verbose_name=_("document")
)
user = models.ForeignKey(
User,
blank=True,
null=True,
related_name="users",
on_delete=models.SET_NULL,
verbose_name=_("user")
)
class Meta:
ordering = ("created",)
verbose_name = _("comment")
verbose_name_plural = _("comments")
def __str__(self):
return self.content

View File

@@ -21,6 +21,8 @@ from django.db.models.functions import Lower
from django.http import Http404
from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.http import HttpResponseNotAllowed
from django.http import HttpResponseNotFound
from django.utils.decorators import method_decorator
from django.utils.translation import get_language
from django.views.decorators.cache import cache_control
@@ -62,6 +64,7 @@ from .matching import match_correspondents
from .matching import match_document_types
from .matching import match_storage_paths
from .matching import match_tags
from .models import Comment
from .models import Correspondent
from .models import Document
from .models import DocumentType
@@ -379,6 +382,61 @@ class DocumentViewSet(
except (FileNotFoundError, Document.DoesNotExist):
raise Http404()
def getComments(self, doc):
return [
{
"id":c.id,
"comment":c.comment,
"created":c.created,
"user":{
"id":c.user.id,
"username": c.user.username,
"firstname":c.user.first_name,
"lastname":c.user.last_name
}
} for c in Comment.objects.filter(document=doc).order_by('-created')
];
@action(methods=['get', 'post', 'delete'], detail=True)
def comments(self, request, pk=None):
if settings.PAPERLESS_COMMENTS_ENABLED != True:
return HttpResponseNotAllowed("comment function is disabled")
try:
doc = Document.objects.get(pk=pk)
except Document.DoesNotExist:
raise Http404()
currentUser = request.user;
if request.method == 'GET':
try:
return Response(self.getComments(doc));
except Exception as e:
return Response({"error": str(e)});
elif request.method == 'POST':
try:
c = Comment.objects.create(
document = doc,
comment=request.data["payload"],
user=currentUser
);
c.save();
return Response(self.getComments(doc));
except Exception as e:
return Response({
"error": str(e)
});
elif request.method == 'DELETE':
comment = Comment.objects.get(id=int(request.GET.get("commentId")));
comment.delete();
return Response(self.getComments(doc));
return Response({
"error": "error"
});
class SearchResultSerializer(DocumentSerializer):
def to_representation(self, instance):
@@ -835,3 +893,32 @@ class AcknowledgeTasksView(GenericAPIView):
return Response({"result": result})
except Exception:
return HttpResponseBadRequest()
class EnvironmentView(APIView):
permission_classes = (IsAuthenticated,)
def get(self, request, format=None):
if 'name' in request.query_params:
name = request.query_params['name']
else:
return HttpResponseBadRequest("name required")
if(name not in settings.PAPERLESS_FRONTEND_ALLOWED_ENVIRONMENTS and settings.PAPERLESS_DISABLED_FRONTEND_ENVIRONMENT_CHECK == False):
return HttpResponseNotAllowed("environment not allowed to request")
value = None
try:
value = getattr(settings, name)
except:
try:
value = os.getenv(name)
except:
value = None
if value == None:
return HttpResponseNotFound("environment not found")
return Response({
"value": str(value)
});

View File

@@ -566,6 +566,14 @@ CONVERT_MEMORY_LIMIT = os.getenv("PAPERLESS_CONVERT_MEMORY_LIMIT")
GS_BINARY = os.getenv("PAPERLESS_GS_BINARY", "gs")
# Comment settings
PAPERLESS_COMMENTS_ENABLED = __get_boolean("PAPERLESS_COMMENTS_ENABLED", "NO")
# allowed environments for frontend
PAPERLESS_DISABLED_FRONTEND_ENVIRONMENT_CHECK = __get_boolean("PAPERLESS_DISABLED_FRONTEND_ENVIRONMENT_CHECK", "NO")
PAPERLESS_FRONTEND_ALLOWED_ENVIRONMENTS = [
"PAPERLESS_COMMENTS_ENABLED"
]
# Pre-2.x versions of Paperless stored your documents locally with GPG
# encryption, but that is no longer the default. This behaviour is still

View File

@@ -8,6 +8,7 @@ from django.utils.translation import gettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import RedirectView
from documents.views import AcknowledgeTasksView
from documents.views import EnvironmentView
from documents.views import BulkDownloadView
from documents.views import BulkEditView
from documents.views import CorrespondentViewSet
@@ -94,6 +95,7 @@ urlpatterns = [
AcknowledgeTasksView.as_view(),
name="acknowledge_tasks",
),
re_path(r"^environment/", EnvironmentView.as_view()),
path("token/", views.obtain_auth_token),
]
+ api_router.urls,