def save(self, *args, **kwargs):
        first_save = not bool(self.pk)
        saved_fields = self.get_dirty_fields() or []
        old_subjects = kwargs.pop('old_subjects', [])
        if saved_fields:
            request, user_id = get_request_and_user_id()
            request_headers = {}
            if not isinstance(request, DummyRequest):
                request_headers = {
                    k: v
                    for k, v in get_headers_from_request(request).items()
                    if isinstance(v, basestring)
                }
            user = OSFUser.load(user_id)
            if user:
                self.check_spam(user, saved_fields, request_headers)
        if not first_save and ('ever_public' in saved_fields
                               and saved_fields['ever_public']):
            raise ValidationError('Cannot set "ever_public" to False')

        ret = super(PreprintService, self).save(*args, **kwargs)

        if (not first_save
                and 'is_published' in saved_fields) or self.is_published:
            update_or_enqueue_on_preprint_updated(preprint_id=self._id,
                                                  old_subjects=old_subjects,
                                                  saved_fields=saved_fields)
        return ret
Beispiel #2
0
    def check_spam(self):
        request, user_id = get_request_and_user_id()
        user = OSFUser.load(user_id)
        if not isinstance(request, DummyRequest):
            request_headers = {
                k: v
                for k, v in get_headers_from_request(request).items()
                if isinstance(v, basestring)
            }

        node = self.wiki_page.node

        if not settings.SPAM_CHECK_ENABLED:
            return False
        if settings.SPAM_CHECK_PUBLIC_ONLY and not node.is_public:
            return False
        if 'ham_confirmed' in user.system_tags:
            return False

        content = self._get_spam_content(node)
        if not content:
            return
        is_spam = node.do_check_spam(user.fullname, user.username, content,
                                     request_headers)

        logger.info("Node ({}) '{}' smells like {} (tip: {})".format(
            node._id, node.title.encode('utf-8'), 'SPAM' if is_spam else 'HAM',
            node.spam_pro_tip))
        if is_spam:
            node._check_spam_user(user)
        return is_spam
Beispiel #3
0
    def check_spam(self):
        request, user_id = get_request_and_user_id()
        user = OSFUser.load(user_id)
        if not isinstance(request, DummyRequest):
            request_headers = {
                k: v
                for k, v in get_headers_from_request(request).items()
                if isinstance(v, basestring)
            }

        node = self.wiki_page.node

        if not settings.SPAM_CHECK_ENABLED:
            return False
        if settings.SPAM_CHECK_PUBLIC_ONLY and not node.is_public:
            return False
        if 'ham_confirmed' in user.system_tags:
            return False

        content = self._get_spam_content(node)
        if not content:
            return
        is_spam = node.do_check_spam(
            user.fullname,
            user.username,
            content,
            request_headers
        )

        logger.info("Node ({}) '{}' smells like {} (tip: {})".format(
            node._id, node.title.encode('utf-8'), 'SPAM' if is_spam else 'HAM', node.spam_pro_tip
        ))
        if is_spam:
            node._check_spam_user(user)
        return is_spam
    def save(self, *args, **kwargs):
        first_save = not bool(self.pk)
        saved_fields = self.get_dirty_fields() or []
        old_subjects = kwargs.pop('old_subjects', [])
        if saved_fields:
            request, user_id = get_request_and_user_id()
            request_headers = {}
            if not isinstance(request, DummyRequest):
                request_headers = {
                    k: v
                    for k, v in get_headers_from_request(request).items()
                    if isinstance(v, basestring)
                }
            user = OSFUser.load(user_id)
            if user:
                self.check_spam(user, saved_fields, request_headers)

        if not first_save and ('ever_public' in saved_fields and saved_fields['ever_public']):
            raise ValidationError('Cannot set "ever_public" to False')

        ret = super(Preprint, self).save(*args, **kwargs)

        if first_save:
            self._set_default_region()
            self.update_group_permissions()
            self._add_creator_as_contributor()

        if (not first_save and 'is_published' in saved_fields) or self.is_published:
            update_or_enqueue_on_preprint_updated(preprint_id=self._id, old_subjects=old_subjects, saved_fields=saved_fields)
        return ret
Beispiel #5
0
    def save(self, request=None, *args, **kwargs):
        super(NodeSettings, self).save(*args, **kwargs)
        if request:
            if not hasattr(request,
                           'user'):  # TODO: remove when Flask is removed
                _, user_id = get_request_and_user_id()
                user = OSFUser.load(user_id)
            else:
                user = request.user

            self.owner.check_spam(user, {'addons_forward_node_settings__url'},
                                  get_headers_from_request(request))
Beispiel #6
0
 def spam_check(self):
     # Since wiki_pages_current will be removed from Node model, when a new WikiVersion is saved, trigger a spam check.
     request, user_id = get_request_and_user_id()
     request_headers = {}
     if not isinstance(request, DummyRequest):
         request_headers = {
             k: v
             for k, v in get_headers_from_request(request).items()
             if isinstance(v, basestring)
         }
     user = OSFUser.load(user_id)
     if user:
         return self.wiki_page.node.check_spam(user, ['wiki_pages_latest'], request_headers)
     else:
         return False
Beispiel #7
0
 def spam_check(self):
     # Since wiki_pages_current will be removed from Node model, when a new WikiVersion is saved, trigger a spam check.
     request, user_id = get_request_and_user_id()
     request_headers = {}
     if not isinstance(request, DummyRequest):
         request_headers = {
             k: v
             for k, v in get_headers_from_request(request).items()
             if isinstance(v, basestring)
         }
     user = OSFUser.load(user_id)
     if user:
         return self.wiki_page.node.check_spam(user, ['wiki_pages_latest'],
                                               request_headers)
     else:
         return False