<commit_before># -*- coding: utf-8 -*-
u"""
Copyright 2013-2014 <NAME> <<EMAIL>>.

This file is part of the 1flow project.

1flow is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

1flow is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with 1flow.  If not, see http://www.gnu.org/licenses/

"""

import logging

from constance import config
from mongoengine import Q

from django.http import (
    HttpResponsePermanentRedirect,
    HttpResponseForbidden,
    HttpResponseBadRequest,
    HttpResponse
)
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib import messages
from django.shortcuts import render, redirect, get_object_or_404
from django.utils.translation import ugettext_lazy as _, ungettext

from endless_pagination.utils import get_page_number_from_request

from sparks.django.utils import HttpResponseTemporaryServerError

from ..forms import ReadShareForm
from ..models.common import READ_STATUS_DATA
from ..models import (
    Subscription,
    Article, Read,
    Folder,
    SimpleTag as Tag,
    CONTENT_TYPES_FINAL
)

LOGGER = logging.getLogger(__name__)


def _rwep_generate_query_kwargs(request, **kwargs):
    """ Not a view, but an helper for read_with_endless_pagination(). """

    query_kwargs = {}
    primary_mode = None
    combinations = set()
    attributes   = READ_STATUS_DATA.keys()

    # First, get the view mode we were called from.
    if kwargs.get('all', False):
        # No particular kwargs for this one, but we
        # needed to pass in the 'if' to avoid the
        # default case where we get only the unread.
        primary_mode = (u'all', False)

    else:
        for attrname in attributes:
            mykwarg = kwargs.get(attrname, None)

            if mykwarg is not None:
                primary_mode = (attrname, mykwarg)

                query_kwargs[attrname] = mykwarg

                # TODO: KEEP ?
                if request.user.is_superuser or request.user.is_staff:
                    combinations.union(set(
                        attr2, request.GET.get(attr2, None), bool)
                        for attr2 in attributes if attr2 not in attributes
                    )

    # Then allow the user to mix with manual query
    # parameters, but check them to avoid crashes.
    for parameter, value, checker in combinations:
        if value is None:
            continue

        try:
            checked_value = checker(value)

        except:
            LOGGER.exception(u'Check %s on value "%s" of parameter %s '
                             u'failed; skipped.', checker, value, parameter)
            continue

        query_kwargs[parameter] = checked_value

    return query_kwargs, primary_mode


def _rwep_generate_order_by(request, **kwargs):

    def check_order_by(value):
        if value in (u'date_created', u'date_added',
                     u'name', 'id', ):
            return

        raise NotImplementedError('order_by needs love!')

    default_order_by = (u'-date_created', )

    if request.user.is_staff_or_superuser_and_enabled:
        order_by = request.GET.get('order_by', None)

        if order_by:
            order_by = unicode(order_by).split(',')

        else:
            order_by = default_order_by
    else:
        order_by = default_order_by

    try:
        for order in order_by:
            if order.startswith(u'-'):
                check_order_by(order[1:])

            else:
                check_order_by(order)
    except:
        LOGGER.exception(u'order_by check failed (value "%s"); '
                         u'using default.', order_by)
        order_by = default_order_by

    return order_by


def _rwep_ajax_update_counters(kwargs, query_kwargs,
                               subscription, folder,
                               user, count):

    attr_name = None

    if kwargs.get('all', False):
        attr_name = u'all_items_count'

    elif query_kwargs.get('is_starred', False):
        attr_name = u'starred_items_count'

    elif query_kwargs.get('is_bookmarked', False):
        attr_name = u'bookmarked_items_count'

    elif query_kwargs.get('is_read__ne', None) is True:
        attr_name = u'unread_items_count'

    if attr_name:
        if subscription:
            current_count = getattr(subscription, attr_name)

            if current_count != count:
                setattr(subscription, attr_name, count)

                LOGGER.info(u'Updated Subscription#%s.%s=%s for '
                            u'Read.%s (old was: %s).',
                            subscription.id, attr_name, count,
                            unicode(query_kwargs), current_count)

                # TODO: update folder with diff.

        elif folder:
            current_count = getattr(folder, attr_name)

            if current_count != count:
                setattr(folder, attr_name, count)

                LOGGER.info(u'Updated Folder#%s.%s=%s for Read.%s '
                            u'(old was: %s).', folder.id, attr_name,
                            count, unicode(query_kwargs), current_count)

                # TODO: recount()/update all subscriptions counters…<commit_msg>Fix unread counters not updating.<commit_after># -*- coding: utf-8 -*-
u"""
Copyright 2013-2014 <NAME> <<EMAIL>>.

This file is part of the 1flow project.

1flow is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

1flow is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with 1flow.  If not, see http://www.gnu.org/licenses/

"""

import logging

from constance import config
from mongoengine import Q

from django.http import (
    HttpResponsePermanentRedirect,
    HttpResponseForbidden,
    HttpResponseBadRequest,
    HttpResponse
)
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib import messages
from django.shortcuts import render, redirect, get_object_or_404
from django.utils.translation import ugettext_lazy as _, ungettext

from endless_pagination.utils import get_page_number_from_request

from sparks.django.utils import HttpResponseTemporaryServerError

from ..forms import ReadShareForm
from ..models.common import READ_STATUS_DATA
from ..models import (
    Subscription,
    Article, Read,
    Folder,
    SimpleTag as Tag,
    CONTENT_TYPES_FINAL
)

LOGGER = logging.getLogger(__name__)


def _rwep_generate_query_kwargs(request, **kwargs):
    """ Not a view, but an helper for read_with_endless_pagination(). """

    query_kwargs = {}
    primary_mode = None
    combinations = set()
    attributes   = READ_STATUS_DATA.keys()

    # First, get the view mode we were called from.
    if kwargs.get('all', False):
        # No particular kwargs for this one, but we
        # needed to pass in the 'if' to avoid the
        # default case where we get only the unread.
        primary_mode = (u'all', False)

    else:
        for attrname in attributes:
            mykwarg = kwargs.get(attrname, None)

            if mykwarg is not None:
                primary_mode = (attrname, mykwarg)

                query_kwargs[attrname] = mykwarg

                # TODO: KEEP ?
                if request.user.is_superuser or request.user.is_staff:
                    combinations.union(set(
                        attr2, request.GET.get(attr2, None), bool)
                        for attr2 in attributes if attr2 not in attributes
                    )

    # Then allow the user to mix with manual query
    # parameters, but check them to avoid crashes.
    for parameter, value, checker in combinations:
        if value is None:
            continue

        try:
            checked_value = checker(value)

        except:
            LOGGER.exception(u'Check %s on value "%s" of parameter %s '
                             u'failed; skipped.', checker, value, parameter)
            continue

        query_kwargs[parameter] = checked_value

    return query_kwargs, primary_mode


def _rwep_generate_order_by(request, **kwargs):

    def check_order_by(value):
        if value in (u'date_created', u'date_added',
                     u'name', 'id', ):
            return

        raise NotImplementedError('order_by needs love!')

    default_order_by = (u'-date_created', )

    if request.user.is_staff_or_superuser_and_enabled:
        order_by = request.GET.get('order_by', None)

        if order_by:
            order_by = unicode(order_by).split(',')

        else:
            order_by = default_order_by
    else:
        order_by = default_order_by

    try:
        for order in order_by:
            if order.startswith(u'-'):
                check_order_by(order[1:])

            else:
                check_order_by(order)
    except:
        LOGGER.exception(u'order_by check failed (value "%s"); '
                         u'using default.', order_by)
        order_by = default_order_by

    return order_by


def _rwep_ajax_update_counters(kwargs, query_kwargs,
                               subscription, folder,
                               user, count):

    attr_name = None

    if kwargs.get('all', False):
        attr_name = u'all_items_count'

    elif query_kwargs.get('is_starred', False):
        attr_name = u'starred_items_count'

    elif query_kwargs.get('is_bookmarked', False):
        attr_name = u'bookmarked_items_count'

    elif query_kwargs.get('is_read', None) is False:
        attr_name = u'unread_items_count'

    if attr_name:
        if subscription:
            current_count = getattr(subscription, attr_name)

            if current_count != count:
                setattr(subscription, attr_name, count)

                LOGGER.info(u'Updated Subscription#%s.%s=%s for '
                            u'Read.%s (old was: %s).',
                            subscription.id, attr_name, count,
                            unicode(query_kwargs), current_count)

                # TODO: update folder with diff.

        elif folder:
            current_count = getattr(folder, attr_name)

            if current_count != count:
                setattr(folder, attr_name, count)

                LOGGER.info(u'Updated Folder#%s.%s=%s for Read.%s '
                            u'(old was: %s).', folder.id, attr_name,
                            count, unicode(query_kwargs), current_count)

                # TODO: recount()/update all subscriptions counters…
{'fraction_non_alphanumeric': 0.07786775829847824, 'fraction_numerical': 0.003915635845866334, 'mean_word_length': 2.2423542989036354, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '90380', 'n_tokens_mistral': 3207, 'n_tokens_neox': 3065, 'n_words': 957}