<commit_before># -*- coding: utf-8 -*-

import time

from flask.ext.restful import fields, marshal
from flask_restful_swagger import swagger

from ..decorators import *
from ..base_resources import BaseItem, BaseList, Response

from .models import Project, ProjectImage
from ..users.models import User
from ..users.resources import UsersListAPI, UserResponse, UsersListResponse
from ..location.models import ProjectLocation, UserLocation
from ..models.reward import Reward
from ..models.cost import Cost
from ..models.support import Support

@swagger.model
class ProjectResponse(Response):
    """ProjectResponse"""

    resource_fields = {
        "id"                : fields.String,
        "name"              : fields.String,
        "node"              : fields.String,
        "date-created"      : fields.DateTime(dt_format='rfc822'), # iso8601 maybe?
        "date-published"    : fields.DateTime(dt_format='rfc822'), # iso8601 maybe?
        "project-url"       : fields.String,
        "image-url" : fields.String,
        "latitude" : fields.Float,
        "longitude" : fields.Float,
        "owner" : fields.String,
        "status" : fields.String,
    }

    required = resource_fields.keys()

@swagger.model
class ProjectGalleryResponse(Response):
    """ProjectGalleryResponse"""

    resource_fields = {
        "image-url" : fields.String,
        "resource-url"              : fields.String,
    }

    required = resource_fields.keys()

@swagger.model
class ProjectRewardResponse(Response):
    """ProjectRewardResponse"""

    resource_fields = {
        "reward" : fields.String,
        "description"              : fields.String,
        "license"              : fields.String,
        "type"              : fields.String,
        "icon"              : fields.String,
    }

    required = resource_fields.keys()

@swagger.model
class ProjectCostResponse(Response):
    """ProjectNeedResponse"""

    resource_fields = {
        "cost" : fields.String,
        "description"              : fields.String,
        "type"              : fields.String,
        "amount"              : fields.Float,
        "required"              : fields.String,
        "from-date"              : fields.DateTime(dt_format='rfc822'),
        "to-date"              : fields.DateTime(dt_format='rfc822'),
    }

    required = resource_fields.keys()

@swagger.model
class ProjectNeedResponse(Response):
    """ProjectNeedResponse"""

    resource_fields = {
        "support" : fields.String,
        "description"              : fields.String,
        "type"              : fields.String,
    }

    required = resource_fields.keys()

@swagger.model
class ProjectLocationResponse(Response):
    """ProjectLocationResponse"""

    resource_fields = {
        "city"              : fields.String,
        "region"              : fields.String,
        "country"              : fields.String,
        "country-code"              : fields.String,
        "latitude" : fields.Float,
        "longitude" : fields.Float,
    }

    required = resource_fields.keys()


@swagger.model
class ProjectCompleteResponse(Response):
    """ProjectCompleteResponse"""

    resource_fields = {
        "id"                : fields.String,
        "name"              : fields.String,
        "description-short" : fields.String,
        "description" : fields.String,
        "motivation" : fields.String,
        "goal" : fields.String,
        "about" : fields.String,
        "lang" : fields.String,
        "currency" : fields.String,
        "currency-rate" : fields.Float,
        "minimum" : fields.Float,
        "optimum" : fields.Float,
        "amount" : fields.Float,
        "status" : fields.String,
        "node"              : fields.String,
        "date-created"      : fields.DateTime(dt_format='rfc822'),
        "date-published"    : fields.DateTime(dt_format='rfc822'),
        "date-updated"    : fields.DateTime(dt_format='rfc822'),
        "date-succeeded"    : fields.DateTime(dt_format='rfc822'),
        "date-closed"    : fields.DateTime(dt_format='rfc822'),
        "date-passed"    : fields.DateTime(dt_format='rfc822'),
        "location" : fields.List(fields.Nested(ProjectLocationResponse.resource_fields)),
        "owner" : fields.String,
        "project-url"       : fields.String,
        "widget-url"       : fields.String,
        "image-url" : fields.String,
        "image-url-big" : fields.String,
        "image-gallery" : fields.List(fields.Nested(ProjectGalleryResponse.resource_fields)),
        "video-url" : fields.String,
        "rewards" : fields.List(fields.Nested(ProjectRewardResponse.resource_fields)),
        "costs" : fields.List(fields.Nested(ProjectCostResponse.resource_fields)),
        "needs" : fields.List(fields.Nested(ProjectNeedResponse.resource_fields)),
    }

    required = resource_fields.keys()

@swagger.model
@swagger.nested(**{
                'items' : ProjectResponse.__name__,
                }
            )
class ProjectsListResponse(Response):
    """ProjectsListResponse"""

    resource_fields = {
        "items"         : fields.List(fields.Nested(ProjectResponse.resource_fields)),
    }

    required = resource_fields.keys()


class ProjectsListAPI(BaseList):
    """Get Project list"""


    @swagger.operation(
        notes='Projects list',
        nickname='projects',
        responseClass=ProjectsListResponse.__name__,
        parameters=BaseList.INPUT_FILTERS,
        responseMessages=BaseList.RESPONSE_MESSAGES
    )
    @requires_auth
    @ratelimit()
    def get(self):
        """Get the projects list
        <a href="http://developers.goteo.org/doc/projects">developers.goteo.org/doc/projects</a>
        """
        res = self._get()

        if res.ret['items'] == []:
            return bad_request('No projects to list', 404)

        return res.response()

    def _get(self):
        """Get()'s method dirty work"""

        time_start = time.time()
        args = self.parse_args()

        items = []
        for p in Project.list(**args):
            item = marshal(p, ProjectResponse.resource_fields)
            item['date-created'] =p.date_created
            item['date-published'] = p.date_published
            item['project-url'] = project_url(p.id)
            item['status'] = p.status_string
            item['image-url'] = image_url(p.image, 'medium', False)
            location = ProjectLocation.get(p.id)
            if location:
                item['latitude'] = location.latitude
                item['longitude'] = location.longitude
            items.append( item )

        res = ProjectsListResponse(
            starttime = time_start,
            attributes = {'items' : items},
            filters = args.items(),
            total = Project.total(**args)
        )

        return res



class ProjectAPI(BaseItem):
    """Get Project Details"""

    @swagger.operation(
        notes='Project profile',
        nickname='project',
        responseClass=ProjectCompleteResponse.__name__,
        responseMessages=BaseItem.RESPONSE_MESSAGES
    )
    @requires_auth<commit_msg>description short in list<commit_after># -*- coding: utf-8 -*-

import time

from flask.ext.restful import fields, marshal
from flask_restful_swagger import swagger

from ..decorators import *
from ..base_resources import BaseItem, BaseList, Response

from .models import Project, ProjectImage
from ..users.models import User
from ..users.resources import UsersListAPI, UserResponse, UsersListResponse
from ..location.models import ProjectLocation, UserLocation
from ..models.reward import Reward
from ..models.cost import Cost
from ..models.support import Support

@swagger.model
class ProjectResponse(Response):
    """ProjectResponse"""

    resource_fields = {
        "id"                : fields.String,
        "name"              : fields.String,
        "description-short"              : fields.String,
        "node"              : fields.String,
        "date-created"      : fields.DateTime(dt_format='rfc822'), # iso8601 maybe?
        "date-published"    : fields.DateTime(dt_format='rfc822'), # iso8601 maybe?
        "project-url"       : fields.String,
        "image-url" : fields.String,
        "latitude" : fields.Float,
        "longitude" : fields.Float,
        "owner" : fields.String,
        "status" : fields.String,
    }

    required = resource_fields.keys()

@swagger.model
class ProjectGalleryResponse(Response):
    """ProjectGalleryResponse"""

    resource_fields = {
        "image-url" : fields.String,
        "resource-url"              : fields.String,
    }

    required = resource_fields.keys()

@swagger.model
class ProjectRewardResponse(Response):
    """ProjectRewardResponse"""

    resource_fields = {
        "reward" : fields.String,
        "description"              : fields.String,
        "license"              : fields.String,
        "type"              : fields.String,
        "icon"              : fields.String,
    }

    required = resource_fields.keys()

@swagger.model
class ProjectCostResponse(Response):
    """ProjectNeedResponse"""

    resource_fields = {
        "cost" : fields.String,
        "description"              : fields.String,
        "type"              : fields.String,
        "amount"              : fields.Float,
        "required"              : fields.String,
        "from-date"              : fields.DateTime(dt_format='rfc822'),
        "to-date"              : fields.DateTime(dt_format='rfc822'),
    }

    required = resource_fields.keys()

@swagger.model
class ProjectNeedResponse(Response):
    """ProjectNeedResponse"""

    resource_fields = {
        "support" : fields.String,
        "description"              : fields.String,
        "type"              : fields.String,
    }

    required = resource_fields.keys()

@swagger.model
class ProjectLocationResponse(Response):
    """ProjectLocationResponse"""

    resource_fields = {
        "city"              : fields.String,
        "region"              : fields.String,
        "country"              : fields.String,
        "country-code"              : fields.String,
        "latitude" : fields.Float,
        "longitude" : fields.Float,
    }

    required = resource_fields.keys()


@swagger.model
class ProjectCompleteResponse(Response):
    """ProjectCompleteResponse"""

    resource_fields = {
        "id"                : fields.String,
        "name"              : fields.String,
        "description-short" : fields.String,
        "description" : fields.String,
        "motivation" : fields.String,
        "goal" : fields.String,
        "about" : fields.String,
        "lang" : fields.String,
        "currency" : fields.String,
        "currency-rate" : fields.Float,
        "minimum" : fields.Float,
        "optimum" : fields.Float,
        "amount" : fields.Float,
        "status" : fields.String,
        "node"              : fields.String,
        "date-created"      : fields.DateTime(dt_format='rfc822'),
        "date-published"    : fields.DateTime(dt_format='rfc822'),
        "date-updated"    : fields.DateTime(dt_format='rfc822'),
        "date-succeeded"    : fields.DateTime(dt_format='rfc822'),
        "date-closed"    : fields.DateTime(dt_format='rfc822'),
        "date-passed"    : fields.DateTime(dt_format='rfc822'),
        "location" : fields.List(fields.Nested(ProjectLocationResponse.resource_fields)),
        "owner" : fields.String,
        "project-url"       : fields.String,
        "widget-url"       : fields.String,
        "image-url" : fields.String,
        "image-url-big" : fields.String,
        "image-gallery" : fields.List(fields.Nested(ProjectGalleryResponse.resource_fields)),
        "video-url" : fields.String,
        "rewards" : fields.List(fields.Nested(ProjectRewardResponse.resource_fields)),
        "costs" : fields.List(fields.Nested(ProjectCostResponse.resource_fields)),
        "needs" : fields.List(fields.Nested(ProjectNeedResponse.resource_fields)),
    }

    required = resource_fields.keys()

@swagger.model
@swagger.nested(**{
                'items' : ProjectResponse.__name__,
                }
            )
class ProjectsListResponse(Response):
    """ProjectsListResponse"""

    resource_fields = {
        "items"         : fields.List(fields.Nested(ProjectResponse.resource_fields)),
    }

    required = resource_fields.keys()


class ProjectsListAPI(BaseList):
    """Get Project list"""


    @swagger.operation(
        notes='Projects list',
        nickname='projects',
        responseClass=ProjectsListResponse.__name__,
        parameters=BaseList.INPUT_FILTERS,
        responseMessages=BaseList.RESPONSE_MESSAGES
    )
    @requires_auth
    @ratelimit()
    def get(self):
        """Get the projects list
        <a href="http://developers.goteo.org/doc/projects">developers.goteo.org/doc/projects</a>
        """
        res = self._get()

        if res.ret['items'] == []:
            return bad_request('No projects to list', 404)

        return res.response()

    def _get(self):
        """Get()'s method dirty work"""

        time_start = time.time()
        args = self.parse_args()

        items = []
        for p in Project.list(**args):
            item = marshal(p, ProjectResponse.resource_fields)
            item['date-created'] =p.date_created
            item['date-published'] = p.date_published
            item['project-url'] = project_url(p.id)
            item['description-short'] = p.subtitle
            item['status'] = p.status_string
            item['image-url'] = image_url(p.image, 'medium', False)
            location = ProjectLocation.get(p.id)
            if location:
                item['latitude'] = location.latitude
                item['longitude'] = location.longitude
            items.append( item )

        res = ProjectsListResponse(
            starttime = time_start,
            attributes = {'items' : items},
            filters = args.items(),
            total = Project.total(**args)
        )

        return res



class ProjectAPI(BaseItem):
    """Get Project Details"""

    @swagger.operation(
        notes='Project profile',
        nickname='project',
        responseClass=ProjectCompleteResponse.__name__,
        responseMessages=BaseItem.RESPONSE_MESSAGES
    )
    @requires_auth
{'fraction_non_alphanumeric': 0.12378826975381826, 'fraction_numerical': 0.005858149103842667, 'mean_word_length': 2.207336166405726, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3926473', 'n_tokens_mistral': 4106, 'n_tokens_neox': 3955, 'n_words': 708}