Skip to content

linting for comments app #2886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion prospector-more.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ strictness: medium
ignore-paths:
- api/
- cdn/
- comments/
- core/
- doc_builder/
- donate/
Expand Down
2 changes: 2 additions & 0 deletions readthedocs/comments/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""ModelAdmin configurations for comments app."""

from django.contrib import admin
from .models import DocumentNode, DocumentComment, NodeSnapshot

Expand Down
3 changes: 2 additions & 1 deletion readthedocs/comments/backend.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Storage backends for the comments app."""

import json

from django.core import serializers
from sphinx.websupport.storage import StorageBackend

from .models import DocumentNode
from readthedocs.comments.models import NodeSnapshot


class DjangoStorage(StorageBackend):
Expand Down
25 changes: 19 additions & 6 deletions readthedocs/comments/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Models for the comments app."""

from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers

from readthedocs.privacy.backend import AdminPermission, AdminNotAuthorized
from readthedocs.restapi.serializers import VersionSerializer


Expand All @@ -23,6 +24,7 @@ def create(self, *args, **kwargs):
return node

def from_hash(self, version_slug, page, node_hash, project_slug=None):
"""Return a node matching a given hash."""
snapshots = NodeSnapshot.objects.filter(hash=node_hash,
node__version__slug=version_slug,
node__page=page)
Expand Down Expand Up @@ -58,6 +60,8 @@ def from_hash(self, version_slug, page, node_hash, project_slug=None):

class DocumentNode(models.Model):

"""Document node."""

objects = DocumentNodeManager()

project = models.ForeignKey('projects.Project', verbose_name=_('Project'),
Expand All @@ -71,10 +75,6 @@ class DocumentNode(models.Model):
def __unicode__(self):
return "node %s on %s for %s" % (self.id, self.page, self.project)

def save(self, *args, **kwargs):
pass
super(DocumentNode, self).save(*args, **kwargs)

def latest_hash(self):
return self.snapshots.latest().hash

Expand Down Expand Up @@ -127,6 +127,9 @@ class Meta:
# in a later commit.
unique_together = ("hash", "node", "commit")

def __unicode__(self):
return self.hash


# class DocumentCommentManager(models.Manager):
#
Expand All @@ -149,6 +152,9 @@ class Meta:


class DocumentComment(models.Model):

"""Comment on a ``DocumentNode`` by a user."""

date = models.DateTimeField(_('Date'), auto_now_add=True)
rating = models.IntegerField(_('Rating'), default=0)
text = models.TextField(_('Text'))
Expand Down Expand Up @@ -197,8 +203,12 @@ def perform_create(self):

class ModerationActionManager(models.Model):

def __unicode__(self):
return str(self.id)

def current_approvals(self):
most_recent_change = self.comment.node.snapshots.latest().date
# pylint: disable=unused-variable
most_recent_change = self.comment.node.snapshots.latest().date # noqa


class ModerationAction(models.Model):
Expand All @@ -211,6 +221,9 @@ class ModerationAction(models.Model):
))
date = models.DateTimeField(_('Date'), auto_now_add=True)

def __unicode__(self):
return "%s - %s" % (self.user_id, self.get_decision_display())

class Meta:
get_latest_by = 'date'

Expand Down
4 changes: 3 additions & 1 deletion readthedocs/comments/session.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Authentication backends for DRF."""

from rest_framework.authentication import SessionAuthentication


class UnsafeSessionAuthentication(SessionAuthentication):

def authenticate(self, request):
http_request = request._request
http_request = request._request # pylint: disable=protected-access
user = getattr(http_request, 'user', None)

if not user or not user.is_active:
Expand Down
4 changes: 3 additions & 1 deletion readthedocs/comments/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.conf.urls import url, include
"""URL configuration for comments app."""

from django.conf.urls import url

from readthedocs.comments import views

Expand Down
19 changes: 9 additions & 10 deletions readthedocs/comments/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
"""Views for comments app."""

from django.contrib.auth.decorators import login_required
from django.http.response import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.decorators import method_decorator
Expand All @@ -14,19 +13,16 @@
detail_route
)
from rest_framework.exceptions import ParseError
from rest_framework.permissions import IsAuthenticated
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer, Serializer
from rest_framework.viewsets import ModelViewSet
from sphinx.websupport import WebSupport

from readthedocs.comments.models import (
DocumentComment, DocumentNode, NodeSnapshot, DocumentCommentSerializer,
DocumentNodeSerializer, ModerationActionSerializer)
from readthedocs.privacy.backend import AdminNotAuthorized
from readthedocs.projects.models import Project
from readthedocs.restapi.permissions import IsOwner, CommentModeratorOrReadOnly
from readthedocs.restapi.permissions import CommentModeratorOrReadOnly

from .backend import DjangoStorage
from .session import UnsafeSessionAuthentication
Expand All @@ -48,7 +44,7 @@
@api_view(['GET'])
@permission_classes([permissions.IsAuthenticatedOrReadOnly])
@renderer_classes((JSONRenderer,))
def get_options(request):
def get_options(request): # pylint: disable=unused-argument
base_opts = support.base_comment_opts
base_opts['addCommentURL'] = '/api/v2/comments/'
base_opts['getCommentsURL'] = '/api/v2/comments/'
Expand Down Expand Up @@ -88,11 +84,11 @@ def attach_comment(request):
# Normal Views
#######

def build(request):
def build(request): # pylint: disable=unused-argument
support.build()


def serve_file(request, file):
def serve_file(request, file): # pylint: disable=redefined-builtin
document = support.get_document(file)

return render_to_response('doc.html',
Expand Down Expand Up @@ -158,6 +154,9 @@ def update_node(request):


class CommentViewSet(ModelViewSet):

"""Viewset for Comment model."""

serializer_class = DocumentCommentSerializer
permission_classes = [CommentModeratorOrReadOnly, permissions.IsAuthenticatedOrReadOnly]

Expand Down Expand Up @@ -199,7 +198,7 @@ def create(self, request):
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

@detail_route(methods=['put'])
def moderate(self, request, pk):
def moderate(self, request, pk): # pylint: disable=unused-argument
comment = self.get_object()
decision = request.data['decision']
moderation_action = comment.moderate(request.user, decision)
Expand Down