<commit_before># -*- coding: utf-8 -*-
'''
    Mango HTTP accounts handlers.
'''

# This file is part of mango.

# Distributed under the terms of the last AGPL License.
# The full license is in the file LICENCE, distributed as part of this software.

__author__ = '<NAME>'


import time
import ujson as json
import motor

# import numpy as np
# import pandas as pd

from tornado import gen
from tornado import web

import logging

# Mango system
from mango.system import accounts
from mango.system import records

from mango.tools import content_type_validation
from mango.tools import check_json
from mango.tools import new_resource

from mango.tools import errors

# system handlers
from mango.handlers import BaseHandler


@content_type_validation
class UsersActiveHandler(accounts.MangoAccounts, BaseHandler):
    '''
        User active accounts HTTP request handlers
    '''

    @gen.coroutine
    def head(self, page_num=0):
        '''
            Head stuff
        '''
        pass

    @gen.coroutine
    def get(self, page_num=0):
        '''
            Get user accounts
        '''
        # logging request query arguments
        logging.info('request query arguments {0}'.format(self.request.arguments))

        # request query arguments
        query_args = self.request.arguments

        # get the current frontend logged username
        username = self.get_current_username()

        # if the user don't provide an account we use the frontend username as last resort
        account = (query_args.get('account', [username])[0] if not account else None)

        # account type flag
        account_type = 'user'

        # status
        status = 'all'
        

        users = yield self.get_account_list(account_type, status, page_num)

        self.set_status(200)
        self.finish({'users':users})
        


@content_type_validation
class UsersDisableHandler(accounts.MangoAccounts, BaseHandler):
    '''
        User disable accounts HTTP request handlers
    '''
    pass


@content_type_validation
class UsersSuspendedHandler(accounts.MangoAccounts, BaseHandler):
    '''
        User suspended accounts HTTP request handlers
    '''
    pass


@content_type_validation
class UsersHandler(accounts.MangoAccounts, BaseHandler):
    '''
        User accounts HTTP request handlers
    '''

    @gen.coroutine
    def head(self, account=None, page_num=0):
        '''
            Head users
        '''
        # logging request query arguments
        logging.info('request query arguments {0}'.format(self.request.arguments))

        # request query arguments
        query_args = self.request.arguments

        # get the current frontend logged username
        username = self.get_current_username()

        # if the user don't provide an account we use the frontend username as last resort
        account = (query_args.get('account', [username])[0] if not account else account)

        # account type flag
        account_type = 'user'

        # status
        status = 'all'

        # cache data
        data = None

        # return result message
        result = None

        if not account:
            users = yield self.get_account_list(account_type, status, page_num)
            self.finish({'users':users})
        else:
            # try to get stuff from cache first
            logging.info('getting users:{0} from cache'.format(account))

            try:
                data = self.cache.get('users:{0}'.format(account))
            except Exception, e:
                logging.exception(e)

            if data is not None:
                logging.info('users:{0} done retrieving!'.format(account))
                result = data
            else:
                data = yield self.get_account(account.rstrip('/'), account_type)
                try:
                    if self.cache.add('users:{0}'.format(account), data, 60):
                        logging.info('new cache entry {0}'.format(str(data)))
                except Exception, e:
                    logging.exception(e)
            
            result = (data if data else None)

            if not result:

                # -- nead moar info

                self.set_status(400)
                self.finish({'missing':account.rstrip('/')})
            else:
                self.set_status(200)
                self.finish(result)

    ###<EMAIL>
    @gen.coroutine
    def get(self, account=None, page_num=0):
        '''
            Get user accounts
        '''
        # logging request query arguments
        logging.info('request query arguments {0}'.format(self.request.arguments))

        # request query arguments
        query_args = self.request.arguments

        # get the current frontend logged username
        username = self.get_current_username()

        # if the user don't provide an account we use the frontend username as last resort
        account = (query_args.get('account', [username])[0] if not account else account)

        # account type flag
        account_type = 'user'

        # status
        status = 'all'

        # cache data
        data = None

        # return result message
        result = None
        
        if not account:
            users = yield self.get_account_list(account_type, status, page_num)
            self.finish({'users':users})
        else:
            # try to get stuff from cache first
            logging.info('getting users:{0} from cache'.format(account))

            try:
                data = self.cache.get('users:{0}'.format(account))
            except Exception, e:
                logging.exception(e)

            if data is not None:
                logging.info('users:{0} done retrieving!'.format(account))
            else:
                data = yield self.get_account(account.rstrip('/'), account_type)
                try:
                    if self.cache.add('users:{0}'.format(account), data, 60):
                        logging.info('new cache entry {0}'.format(str(data)))
                except Exception, e:
                    logging.exception(e)

            result = (data if data else None)
            
            #result = yield self.get_account(account.rstrip('/'), account_type)

            if not result:
                # -- need more info
                self.set_status(400)
                self.finish({'missing':account.rstrip('/')})
            else:
                self.set_status(200)
                self.finish(result)
                
    @gen.coroutine
    def post(self):
        '''
            Create user account
        '''
        struct = yield check_json(self.request.body)
        
        format_pass = (True if struct else False)
        if not format_pass:
            self.set_status(400)
            self.finish({'JSON':format_pass})
            return

        struct['account_type'] = 'user'

        logging.info('new account structure %s' % str(struct))

        result = yield self.new_account(struct)

        if 'error' in result:
            model = 'User'
            reason = {'duplicates': [(model, 'account'), (model, 'email')]}

            message = yield self.let_it_crash(struct, model, result, reason)

            logging.warning(message)

            self.set_status(400)
            self.finish(message)
            return


        # -- handle SIP account creation out-of-band

        # postgresql insert sip account
        if result:
            sip_account = yield self.new_sip_account(struct)

            coturn_account = yield self.new_coturn_account(struct)

            new_address = yield self.new_address(struct)


        self.set_status(201)
        self.finish({'uuid':result})

    @gen.coroutine
    def patch(self, account):
        '''
            Update user account
        '''

        logging.info('request.arguments {0}'.format(self.request.arguments))
        logging.info('request.body {0}'.format(self.request.body))

        struct = yield check_json(self.request.body)<commit_msg>aa yeah<commit_after># -*- coding: utf-8 -*-
'''
    Mango HTTP accounts handlers.
'''

# This file is part of mango.

# Distributed under the terms of the last AGPL License.
# The full license is in the file LICENCE, distributed as part of this software.

__author__ = '<NAME>'


import time
import ujson as json
import motor

# import numpy as np
# import pandas as pd

from tornado import gen
from tornado import web

import logging

# Mango system
from mango.system import accounts
from mango.system import records

from mango.tools import content_type_validation
from mango.tools import check_json
from mango.tools import new_resource

from mango.tools import errors

# system handlers
from mango.handlers import BaseHandler


@content_type_validation
class UsersActiveHandler(accounts.MangoAccounts, BaseHandler):
    '''
        User active accounts HTTP request handlers
    '''

    @gen.coroutine
    def head(self, page_num=0):
        '''
            Head stuff
        '''
        pass

    @gen.coroutine
    def get(self, page_num=0):
        '''
            Get user accounts
        '''
        # logging request query arguments
        logging.info('request query arguments {0}'.format(self.request.arguments))

        # request query arguments
        query_args = self.request.arguments

        # get the current frontend logged username
        username = self.get_current_username()

        # if the user don't provide an account we use the frontend username as last resort
        account = (query_args.get('account', [username])[0] if not account else None)

        # account type flag
        account_type = 'user'

        # status
        status = 'all'
        

        users = yield self.get_account_list(account_type, status, page_num)

        self.set_status(200)
        self.finish({'users':users})
        


@content_type_validation
class UsersDisableHandler(accounts.MangoAccounts, BaseHandler):
    '''
        User disable accounts HTTP request handlers
    '''
    pass


@content_type_validation
class UsersSuspendedHandler(accounts.MangoAccounts, BaseHandler):
    '''
        User suspended accounts HTTP request handlers
    '''
    pass


@content_type_validation
class UsersHandler(accounts.MangoAccounts, BaseHandler):
    '''
        User accounts HTTP request handlers
    '''

    @gen.coroutine
    def head(self, account=None, page_num=0):
        '''
            Head users
        '''
        # logging request query arguments
        logging.info('request query arguments {0}'.format(self.request.arguments))

        # request query arguments
        query_args = self.request.arguments

        # get the current frontend logged username
        username = self.get_current_username()

        # if the user don't provide an account we use the frontend username as last resort
        account = (query_args.get('account', [username])[0] if not account else account)

        # account type flag
        account_type = 'user'

        # status
        status = 'all'

        # cache data
        data = None

        # return result message
        result = None

        if not account:
            users = yield self.get_account_list(account_type, status, page_num)
            self.finish({'users':users})
        else:
            # try to get stuff from cache first
            logging.info('getting users:{0} from cache'.format(account))

            try:
                data = self.cache.get('users:{0}'.format(account))
            except Exception, e:
                logging.exception(e)

            if data is not None:
                logging.info('users:{0} done retrieving!'.format(account))
                result = data
            else:
                data = yield self.get_account(account.rstrip('/'), account_type)
                try:
                    if self.cache.add('users:{0}'.format(account), data, 60):
                        logging.info('new cache entry {0}'.format(str(data)))
                except Exception, e:
                    logging.exception(e)
            
            result = (data if data else None)

            if not result:

                # -- nead moar info

                self.set_status(400)
                self.finish({'missing':account.rstrip('/')})
            else:
                self.set_status(200)
                self.finish(result)

    ###@web.authenticated
    @gen.coroutine
    def get(self, account=None, page_num=0):
        '''
            Get user accounts
        '''
        # logging request query arguments
        logging.info('request query arguments {0}'.format(self.request.arguments))

        # request query arguments
        query_args = self.request.arguments

        # get the current frontend logged username
        username = self.get_current_username()

        # if the user don't provide an account we use the frontend username as last resort
        account = (query_args.get('account', [username])[0] if not account else account)

        # account type flag
        account_type = 'user'

        # status
        status = 'all'

        # cache data
        data = None

        # return result message
        result = None
        
        if not account:
            users = yield self.get_account_list(account_type, status, page_num)
            self.finish({'users':users})
        else:
            # try to get stuff from cache first
            logging.info('getting users:{0} from cache'.format(account))

            try:
                data = self.cache.get('users:{0}'.format(account))
            except Exception, e:
                logging.exception(e)

            if data is not None:
                logging.info('users:{0} done retrieving!'.format(account))
            else:
                data = yield self.get_account(account.rstrip('/'), account_type)
                try:
                    if self.cache.add('users:{0}'.format(account), data, 60):
                        logging.info('new cache entry {0}'.format(str(data)))
                except Exception, e:
                    logging.exception(e)

            result = (data if data else None)
            
            #result = yield self.get_account(account.rstrip('/'), account_type)

            if not result:
                # -- need more info
                self.set_status(400)
                self.finish({'missing':account.rstrip('/')})
            else:
                self.set_status(200)
                self.finish(result)
                
    @gen.coroutine
    def post(self):
        '''
            Create user account
        '''
        struct = yield check_json(self.request.body)
        
        format_pass = (True if struct else False)
        if not format_pass:
            self.set_status(400)
            self.finish({'JSON':format_pass})
            return

        struct['account_type'] = 'user'

        logging.info('new account structure %s' % str(struct))

        result = yield self.new_account(struct)

        if 'error' in result:
            model = 'User'
            reason = {'duplicates': [(model, 'account'), (model, 'email')]}

            message = yield self.let_it_crash(struct, model, result, reason)

            logging.warning(message)

            self.set_status(400)
            self.finish(message)
            return


        # -- handle SIP account creation out-of-band

        # postgresql insert sip account
        if result:
            sip_account = yield self.new_sip_account(struct)

            coturn_struct = {
                account: struct['account'],
                label: 'coturn',
                source: struct['password']
            }

            coturn_account = yield self.new_coturn_account(coturn_struct)

            new_address = yield self.new_address(struct)


        self.set_status(201)
        self.finish({'uuid':result})

    @gen.coroutine
    def patch(self, account):
        '''
            Update user account
        '''

        logging.info('request.arguments {0}'.format(self.request.arguments))
        logging.info('request.body {0}'.format(self.request.body))

        struct = yield check_json(self.request.body)
{'fraction_non_alphanumeric': 0.09516158932212816, 'fraction_numerical': 0.0063029104615955015, 'mean_word_length': 1.9066091954022988, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '2236325', 'n_tokens_mistral': 4370, 'n_tokens_neox': 4123, 'n_words': 1177}