mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
add comment function
This commit is contained in:

committed by
Michael Shamoon

parent
c2fda245ac
commit
278e9c12e1
@@ -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))
|
||||
|
19
src/documents/migrations/1023_add_comments.py
Normal file
19
src/documents/migrations/1023_add_comments.py
Normal 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())
|
||||
],
|
||||
)
|
||||
]
|
@@ -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
|
@@ -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)
|
||||
});
|
||||
|
Reference in New Issue
Block a user