<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
""" Cornice services.
"""
from campaign import LOG
from campaign.auth.default import DefaultAuth
from mozsvc.metrics import Service
from mako.template import Template
import pyramid.httpexceptions as err
from time import strptime
from webob import Response
import json
import os


fetch = Service(name='fetch',
        path='/announcements/{channel}/{platform}/{version}',
        description='Fetcher')
fetchall = Service(name="fetchall",
        path='/announcements/',
        description='Fetch Everything')
authorx = Service(name='authorx',
        path='/author/{id}',
        description='Authoring Interface with record')
author = Service(name='author',
        path='/author/',
        description='Authoring Interface')
fstatic = Service(name='fstatic',
        path='/{file}',
        description='hack')
logout = Service(name='logout',
        path='/logout/',
        description='logout')
redirl = Service(name='redir2',
        path='/redirect/{locale}/{token}',
        description='redir with locale')
redir = Service(name='redir',
        path='/redirect/{token}',
        description='redir')


_TMPL = os.path.join(os.path.dirname(__file__), 'templates')


def get_lang_loc(request):
    header = request.headers.get('Accept-Language', 'en-us')
    langloc = header.split(',')[0]
    if ('-' in langloc):
        (lang, loc) = langloc.split('-')
    else:
        (lang, loc) = (langloc, None)
    return {'lang': lang, 'locale': loc}


def get_last_accessed(request):
    last_accessed = None
    try:
        if 'If-Modified-Since' in request.headers:
            last_accessed_str = request.headers.get('If-Modified-Since')
            last_accessed = strptime(last_accessed_str)
    except Exception, e:
        import pdb; pdb.set_trace()
        request.registry['metlog'].metlog(type='campaign_error',
                severity=LOG.ERROR,
                payload='Exception: %s' % str(e))
    return {'last_accessed': last_accessed}


def log_fetched(request, reply):
    logger = request.registry['metlog'].metlog
    for item in reply['announcements']:
        continue; ## NOOP
        logger(type='campaign_log',
                severity=LOG.INFO,
                payload=json.dumps(item))
        #metlogger.metlog('msgtype', payload='payload')
        pass

@fetch.get()
def get_snippets(request):
    """Returns campaigns in JSON."""
    # get the valid user information from the request.
    metlog = request.registry.get('metlog')
    storage = request.registry.get('storage')
    args = request.matchdict
    args.update(get_lang_loc(request))
    last_accessed = get_last_accessed(request)
    args.update(last_accessed)
    reply = {'announcements': storage.get_announce(args)}
    metlog.metlog(type='campaign', payload='fetch', fields=args)
    if not len(reply):
        if last_accessed:
            raise err.HTTPNotModified
        else:
            raise err.HTTPNoContent
    log_fetched(request, reply)
    return reply


def get_template(name):
    name = os.path.join(_TMPL, '%s.mako' % name)
    try:
        return Template(filename=name)
    except IOError:
        raise err.HTTPServerError


def get_file(name):
    try:
        name = os.path.join(_TMPL, '%s' % name)
        ff = open(name)
        return ff.read()
    except IOError:
        raise err.HTTPNotFound


def get_ideltime(request):
    return {'idle_time': int(request.params.get('idle', 0))}


def authorized(request, email):
    if email is None:
        return False
    settings = request.registry.settings
    try:
        domains = json.loads(settings.get('auth.valid.domains',
            "['@mozilla.com', '@mozilla.org']"))
        for valid_domain in domains:
            if email.lower().endswith(valid_domain):
                return True
    except TypeError, e:
        pass
    except Exception, e:
        import pdb; pdb.set_trace();
        pass
    return False


@fetchall.get()
def fetchall_snippets(request):
    if not authorized(request, request.session.get('uid')):
        return login(request)
    storage = request.registry.get('storage')
    tdata = {"notes": storage.get_all_announce()}
    return tdata


@author.get()<commit_msg>Fixed json quoting for auth email domain defaults<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
""" Cornice services.
"""
from campaign import LOG
from campaign.auth.default import DefaultAuth
from mozsvc.metrics import Service
from mako.template import Template
import pyramid.httpexceptions as err
from time import strptime
from webob import Response
import json
import os


fetch = Service(name='fetch',
        path='/announcements/{channel}/{platform}/{version}',
        description='Fetcher')
fetchall = Service(name="fetchall",
        path='/announcements/',
        description='Fetch Everything')
authorx = Service(name='authorx',
        path='/author/{id}',
        description='Authoring Interface with record')
author = Service(name='author',
        path='/author/',
        description='Authoring Interface')
fstatic = Service(name='fstatic',
        path='/{file}',
        description='hack')
logout = Service(name='logout',
        path='/logout/',
        description='logout')
redirl = Service(name='redir2',
        path='/redirect/{locale}/{token}',
        description='redir with locale')
redir = Service(name='redir',
        path='/redirect/{token}',
        description='redir')


_TMPL = os.path.join(os.path.dirname(__file__), 'templates')


def get_lang_loc(request):
    header = request.headers.get('Accept-Language', 'en-us')
    langloc = header.split(',')[0]
    if ('-' in langloc):
        (lang, loc) = langloc.split('-')
    else:
        (lang, loc) = (langloc, None)
    return {'lang': lang, 'locale': loc}


def get_last_accessed(request):
    last_accessed = None
    try:
        if 'If-Modified-Since' in request.headers:
            last_accessed_str = request.headers.get('If-Modified-Since')
            last_accessed = strptime(last_accessed_str)
    except Exception, e:
        import pdb; pdb.set_trace()
        request.registry['metlog'].metlog(type='campaign_error',
                severity=LOG.ERROR,
                payload='Exception: %s' % str(e))
    return {'last_accessed': last_accessed}


def log_fetched(request, reply):
    logger = request.registry['metlog'].metlog
    for item in reply['announcements']:
        continue; ## NOOP
        logger(type='campaign_log',
                severity=LOG.INFO,
                payload=json.dumps(item))
        #metlogger.metlog('msgtype', payload='payload')
        pass

@fetch.get()
def get_snippets(request):
    """Returns campaigns in JSON."""
    # get the valid user information from the request.
    metlog = request.registry.get('metlog')
    storage = request.registry.get('storage')
    args = request.matchdict
    args.update(get_lang_loc(request))
    last_accessed = get_last_accessed(request)
    args.update(last_accessed)
    reply = {'announcements': storage.get_announce(args)}
    metlog.metlog(type='campaign', payload='fetch', fields=args)
    if not len(reply):
        if last_accessed:
            raise err.HTTPNotModified
        else:
            raise err.HTTPNoContent
    log_fetched(request, reply)
    return reply


def get_template(name):
    name = os.path.join(_TMPL, '%s.mako' % name)
    try:
        return Template(filename=name)
    except IOError:
        raise err.HTTPServerError


def get_file(name):
    try:
        name = os.path.join(_TMPL, '%s' % name)
        ff = open(name)
        return ff.read()
    except IOError:
        raise err.HTTPNotFound


def get_ideltime(request):
    return {'idle_time': int(request.params.get('idle', 0))}


def authorized(request, email):
    if email is None:
        return False
    settings = request.registry.settings
    try:
        domains = json.loads(settings.get('auth.valid.domains',
            '["@mozilla.com", "@mozilla.org"]'))
        for valid_domain in domains:
            if email.lower().endswith(valid_domain):
                return True
    except TypeError, e:
        pass
    except Exception, e:
        import pdb; pdb.set_trace();
        pass
    return False


@fetchall.get()
def fetchall_snippets(request):
    if not authorized(request, request.session.get('uid')):
        return login(request)
    storage = request.registry.get('storage')
    tdata = {"notes": storage.get_all_announce()}
    return tdata


@author.get()
{'fraction_non_alphanumeric': 0.1289372309086789, 'fraction_numerical': 0.0015862225243598459, 'mean_word_length': 2.9654088050314464, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '6297127', 'n_tokens_mistral': 2679, 'n_tokens_neox': 2621, 'n_words': 628}