from dace.util import find_catalog
from pyramid.view import view_config

from novaideo.content.interface import (
    IPerson,
    Iidea,
    IQuestion,
    IChallenge,
    IProposal,
    INovaIdeoApplication)


@view_config(name='stats', context=INovaIdeoApplication, renderer='json')
def site_data(context, request):
    counts = {}
    dace_catalog = find_catalog('dace')
    novaideo_catalog = find_catalog('novaideo')
    created_at_index = novaideo_catalog['created_at']
    states_index = dace_catalog['object_states']
    object_provides_index = dace_catalog['object_provides']

    latest_content = object_provides_index.any(
        (IPerson.__identifier__,
         Iidea.__identifier__,
         IQuestion.__identifier__,
         IProposal.__identifier__,
         IChallenge.__identifier__)).execute(
        ).sort(created_at_index, reverse=True).first()

    query = object_provides_index.any((IPerson.__identifier__,)) & \
        states_index.notany(['deactivated'])
    counts['members'] = len(query.execute())

    query = object_provides_index.any((Iidea.__identifier__,)) & \
        states_index.any(['published'])
    counts['ideas'] = len(query.execute())

    counts['questions'] = 0
    if 'question' in request.content_to_manage:
        query = object_provides_index.any((IQuestion.__identifier__,)) & \
            states_index.any(['published'])
        counts['questions'] = len(query.execute())

    counts['proposals'] = 0
    if 'proposal' in request.content_to_manage:
        query = object_provides_index.any((IProposal.__identifier__,)) & \
            states_index.notany(['archived', 'draft'])
        counts['proposals'] = len(query.execute())

    counts['challenges'] = 0
    if 'challenge' in request.content_to_manage:
        query = object_provides_index.any((IChallenge.__identifier__,)) & \
            states_index.any(['published'])
        counts['challenges'] = len(query.execute())

    root = request.root
    result = {}
    result['counts'] = counts
    logo = getattr(root, 'picture', None)
    result["logoUrl"] = logo.url if logo else \
        request.static_url('novaideo:static/images/novaideo_logo.png')
    result["latestContentCreatedDate"] = latest_content.created_at.strftime('%Y-%m-%d') if latest_content is not None else None
    result["creationDate"] = root.created_at.strftime('%Y-%m-%d')
    result["title"] = root.title
    result["url"] = request.resource_url(root, '')
    result["description"] = root.description
    result["type"] = 'private' if \
        getattr(root, 'only_for_members', False) else 'public'
    return result

@@ -1940,72 +1940,58 @@
    r
-oot = request.root%0A    result = %7B%7D%0A    result%5B'counts'%5D = counts
+esult = %7B'counts': counts%7D%0A    root = request.root
 %0A   
@@ -2507,16 +2507,16 @@
 e' if %5C%0A
-
         
@@ -2570,16 +2570,551 @@
 public'%0A
+    config = %7B%7D%0A    config%5B%22moderate_proposals%22%5D = root.moderate_proposals%0A    config%5B%22moderate_ideas%22%5D = root.moderate_ideas%0A    config%5B%22examine_proposals%22%5D = root.examine_proposals%0A    config%5B%22examine_ideas%22%5D = root.examine_ideas%0A    config%5B%22support_proposals%22%5D = root.support_proposals%0A    config%5B%22support_ideas%22%5D = root.support_ideas%0A    config%5B%22manage_challenges%22%5D = root.manage_challenges%0A    config%5B%22manage_questions%22%5D = root.manage_questions%0A    config%5B%22manage_proposals%22%5D = root.manage_proposals%0A    result%5B'config'%5D = config%0A
     retu

add instance confi to the stats view