Example #1
0
def init_db():
    config = DefaultConfig()
    con = Connection(config.MONGODB_HOST, config.MONGODB_PORT)
    #con.drop_database(config.MONGODB_DATABASE)
    con.register(User)
    db = Database(con, config.MONGODB_DATABASE)

    index = 0
    for email in config.ADMIN:
        user = db.User()
        user['email'] = email
        user['password'] = db.User.encode_pwd(u'123456')
        user['create_time'] = datetime.datetime.now()
        user['nickname'] = u''
        if db.User.find({'email': email}).count() == 0:
            user.save()
            index += 1

    print '%s done.' % index
Example #2
0
def main():
    # local configuration
    """
   MONGODB_HOST = 'localhost'
   MONGODB_PORT = 27017
   DB_NAME = "dmatch"
   """

    # remote configuration
    MONGODB_HOST = "ds039717.mongolab.com"
    MONGODB_PORT = 39717
    DB_NAME = "heroku_app8550378"
    db = Connection(MONGODB_HOST, MONGODB_PORT)

    db.heroku_app8550378.authenticate("heroku_app8550378",
                                      "2v3kconmt7hbc7pdf32gj3po3s")
    fp = open("test.json", "w")
    fp.write(outputHierarchyInfoJSON("friends_3", db[DB_NAME]))
    fp.close()
    def connect(self):
        """Connect to the MongoDB server and register the documents from
        :attr:`registered_documents`. If you set ``MONGODB_USERNAME`` and
        ``MONGODB_PASSWORD`` then you will be authenticated at the
        ``MONGODB_DATABASE``. You can also enable timezone awareness if
        you set to True ``MONGODB_TZ_AWARE`.
        """
        if self.app is None:
            raise RuntimeError('The flask-mongokit extension was not init to '
                               'the current application.  Please make sure '
                               'to call init_app() first.')

        ctx = ctx_stack.top
        mongokit_connection = getattr(ctx, 'mongokit_connection', None)
        if mongokit_connection is None:
            ctx.mongokit_connection = Connection(
                host=ctx.app.config.get('MONGODB_HOST'),
                port=ctx.app.config.get('MONGODB_PORT'),
                slave_okay=ctx.app.config.get('MONGODB_SLAVE_OKAY'),
                tz_aware=ctx.app.config.get('MONGODB_TZ_AWARE', False)
            )

            ctx.mongokit_connection.register(self.registered_documents)

        mongokit_database = getattr(ctx, 'mongokit_database', None)
        if mongokit_database is None:
            ctx.mongokit_database = Database(
                ctx.mongokit_connection,
                ctx.app.config.get('MONGODB_DATABASE')
            )

        if ctx.app.config.get('MONGODB_USERNAME') is not None:
            try:
                auth_success = ctx.mongokit_database.authenticate(
                    ctx.app.config.get('MONGODB_USERNAME'),
                    ctx.app.config.get('MONGODB_PASSWORD')
                )
            except OperationFailure:
                auth_success = False

            if not auth_success:
                raise AuthenticationIncorrect('Server authentication failed')
Example #4
0
def details(request):
    if request.method == 'POST':
        if request.POST.get("computer_details"):
            print "You have successfully submitted the foem"
            conn = Connection()
            conn.register([Computer])
            database = conn.mydb
            collection = database.mycollection
            computer = collection.Computer()
            computer.make = unicode(request.POST['make'])
            computer.model = unicode(request.POST['model'])
            computer.purchase_date = unicode(request.POST['pur_date'])
            computer.cpu_ghz = unicode(request.POST['cpu_speed'])
            computer.save()
            print "Computer is:::" + str(computer.model)
            computer_list = collection.Computer.find()
            context = {'computer_list': computer_list}
            return render(request, 'computers/details.html', context)
            #return HttpResponse("Your response has been recorded")
        else:
            print "You havent clicked submit as yet"

    return HttpResponse("You response was not successfully recd")
Example #5
0
def init_db(host=None, port=None, database=None):
    con = Connection(host, port)
    con.drop_database(database)
    con.register([Admin, AdminRole])
    db = Database(con, database)

    generate_index(host, port, database)

    role = db.AdminRole()
    role['name'] = u'管理员'
    role['auth_list'] = get_auth_list()
    role.save()

    user = db.Admin()
    user['email'] = u'*****@*****.**'
    user['name'] = u'admin'
    user['password'] = db.Admin.encode_pwd('123456')
    user['login_time'] = datetime.now()
    user['status'] = True
    user['role'] = role
    user.save()

    return 'success'
Example #6
0
    def test_create_test_database(self):
        from django.conf import settings
        try:
            assert 'mongodb' in settings.DATABASES
        except AttributeError:
            # Django <1.2
            return  # :(
        old_database_name = settings.DATABASES['mongodb']['NAME']
        assert 'test_' not in old_database_name
        # pretend we're the Django 'test' command

        from django.db import connections
        connection = connections['mongodb']

        connection.creation.create_test_db()
        test_database_name = settings.DATABASES['mongodb']['NAME']
        self.assertTrue('test_' in test_database_name)

        from mongokit import Connection
        con = Connection()
        # the test database isn't created till it's needed
        self.assertTrue(test_database_name not in con.database_names())

        # creates it
        db = con[settings.DATABASES['mongodb']['NAME']]
        coll = db.test_collection_name
        # do a query on the collection to force the database to be created
        list(coll.find())
        test_database_name = settings.DATABASES['mongodb']['NAME']
        self.assertTrue(test_database_name in con.database_names())

        connection.creation.destroy_test_db(old_database_name)
        self.assertTrue('test_' not in settings.DATABASES['mongodb']['NAME'])
        self.assertTrue(test_database_name not in con.database_names())

        # this should work even though it doesn't need to do anything
        connection.close()
Example #7
0
    def setUp(self):
        app.config.update({
            'DATABASE': 'test',
            'MONGODB_HOST': 'localhost',
            'MONGODB_PORT': 27017,
            'TESTING': True,
            'SECRET_KEY': 'testing key',
        })

        self.generic_entry = {
            'content': 'test',
            'rows': '1',
            'date': datetime.datetime.now()
        }
        self.generic_user = {'name': 'test_user', 'pw': 'default'}

        self.app = app.test_client()
        self.conn = Connection(app.config['MONGODB_HOST'],
                               app.config['MONGODB_PORT'])
        #self.conn.register([Entry, User])
        self.conn.register([Entry])
        #reset the collections
        self.conn[app.config['DATABASE']].entries.drop()
        self.conn[app.config['DATABASE']].users.drop()
Example #8
0
def get_mongoconn():
    global _mongo_conn
    if _mongo_conn is None:
        _mongo_conn = Connection(config.MONGO_URI, **config.MONGO_KWARGS)
        _mongo_conn.register([User, Log])
    return _mongo_conn
Example #9
0
class ReportInterface(object):

    db = Connection(MONGO_CONFIG.get('host')).leadhug

    @classmethod
    def get_range_data(cls, start, end, fields=None, limit=30, skip=0, **kw):
        '''
            args: fields-->[]
                  start--->'2016-03-12'
                  end--->'2016-03-15'
                  kw---> {...}
        '''
        if fields:
            fields_dict = {}
            [
                fields_dict.update({field: 1})
                for field, value in fields.items() if value
            ]

        condition = cls.create_condition(**kw)
        offers = kw.get('offers_id')
        api_offer_name = fields.get('api_offer_name')
        offer_name = fields.get('offer_name')
        if offer_name:
            condition.update(dict(offer_id={"$lt": 1000000}))
            if offers:
                condition.update(
                    dict(offer_id={"$in": [int(i) for i in offers]}))
        elif api_offer_name:
            condition.update(dict(offer_id={"$gte": 1000000}))

        condition.update({'day': {'$gte': start.strip(), '$lte': end.strip()}})

        collection = cls.db.reportHour if cls.__name__ == 'ReportHour' else cls.db.reportDay

        # total_count, results = cls.get_map_reduce_result(collection, fields_dict, condition)
        # total_count, results = cls.get_group_result(collection, fields_dict, condition)
        total_count, results = cls.get_aggregate_result(
            collection, fields_dict, condition, limit, skip)

        calculate_result = cls.create_calculate_result(
            fields_dict, results) if results != False else False
        return calculate_result, total_count

    # map_reduce start
    @classmethod
    def get_map_reduce_result(cls, collection, fields_dict, condition):
        initial = cls.create_initial(fields_dict)
        groups = cls.create_group(fields_dict)
        _map = cls.map_fun(groups, initial)
        _reduce = cls.reduce_fun(initial)
        results = cls.result(collection, _map, _reduce, condition)
        return len(results), results

    @classmethod
    def map_fun(cls, groups, initial):
        '''
            function(){
                emit(keys, values);
            }
        '''

        keys = '{'
        for key in groups.keys():
            keys += '{key}: this.{key},'.format(key=key)
        keys += '}'

        values = '{'
        for v in initial.keys():
            values += '{v}: this.{v},'.format(v=v)
        values += '}'

        _map = '''function(){
            emit(%s, %s)
        }''' % (keys, values)
        return Code(_map)

    @classmethod
    def reduce_fun(cls, initial):
        '''
            function(key, values){
                for(var i=0; i<values.length; i++){
                    each(initial, function(key, value){
                        initial[key] += values[i][key]
                    })
                }
                return initial
            }
        '''
        res = dict()
        for k in initial.keys():
            res[k] = 0

        _reduce = '''
            function(key, values){
                var res = %s;
                var res_list = %s;
                for(var i=0; i<values.length; i++){
                    for(var j=0; j<res_list.length; j++){
                        res[res_list[j]] += values[i][res_list[j]]
                    }
                }
                return res
            }
        ''' % (res, res.keys())
        return Code(_reduce)

    @classmethod
    def result(cls, collection, _map, _reduce, condition):
        res = collection.inline_map_reduce(_map,
                                           _reduce,
                                           full_response=False,
                                           query=condition)
        result = []
        for doc in res:
            group_fields = doc.get('_id')
            values = doc.get('value')
            values.update(group_fields)
            result.append(values)
        return result

    # map_reduce end

    # group method start
    @classmethod
    def get_group_result(cls, collection, fields_dict, condition):
        initial = cls.create_initial(fields_dict)
        groups = cls.create_group(fields_dict)
        reducer = cls.create_reducer(initial)
        results = collection.group(key=groups,
                                   condition=condition,
                                   initial=initial,
                                   reduce=reducer,
                                   finalize=None)
        total_count = len(results)
        return total_count, results

    @classmethod
    def create_reducer(cls, initial):
        prev_str = ''
        for k, v in initial.items():
            prev_str += 'prev.{k} += obj.{k};'.format(k=k)

        reduce_code = 'function(obj, prev){%s}' % prev_str
        return Code(reduce_code)

    # group method end

    @classmethod
    def create_initial(cls, field_dict):
        fields = [
            'gross_clicks', 'unique_clicks', 'profit', 'sales', 'revenue'
        ]

        initial = dict(impressions=0, clicks=0, cost=0, conversions=0)
        [initial.update({fd: 0}) for fd in fields if fd in field_dict]
        return initial

    @classmethod
    def create_condition(cls, **kw):
        spec = OrderedDict()
        affiliates = kw.get('affiliates_name')
        if affiliates:
            spec.update(
                dict(affiliate_id={"$in": [int(aff) for aff in affiliates]}))

        advertisers = kw.get('advertisers_name')
        if advertisers:
            spec.update(
                dict(advertiser_id={"$in": [int(a) for a in advertisers]}))

        countries = kw.get('countries')
        if countries:
            spec.update(dict(country={"$in": countries}))

        categories = kw.get('categories_name')
        if categories:
            spec.update(dict(category_name={"$in": categories}))

        return spec

    @classmethod
    def create_group(cls, fields_dict):
        group_fields = [
            'affiliate_id', 'affiliate_name', 'affiliate_sub_id_1',
            'affiliate_sub_id_2', 'affiliate_sub_id_3', 'affiliate_sub_id_4',
            'affiliate_sub_id_5', 'offer_id', 'offer_name', 'advertiser_id',
            'advertiser_name', 'category_name', 'country', 'hour', 'day',
            'week', 'month', 'year'
        ]
        groups = OrderedDict()
        [groups.update({fd: 1}) for fd in group_fields if fd in fields_dict]
        return groups

    # aggregate start
    @classmethod
    def get_aggregate_result(cls, collection, fields_dict, match, limit, skip):
        sum_field = cls.create_sum_field(fields_dict)
        groups2 = cls.create_aggregate_groups(fields_dict, sum_field)
        try:
            res = collection.aggregate([
                {
                    "$match": match
                },
                {
                    "$group": groups2
                },
                {
                    "$sort": SON([("_id", 1)])
                },
            ])
        except OperationFailure:
            return 0, False
        result = res.get('result')
        for r in result:
            fields = r.get('_id')
            r.pop('_id')
            r.update(fields)
        return len(result), result

    @classmethod
    def create_sum_field(cls, field_dict):
        fields = [
            'gross_clicks', 'unique_clicks', 'revenue', 'profit', 'sales'
        ]

        sum_field = dict(
            impressions={"$sum": "$impressions"},
            clicks={"$sum": "$clicks"},
            cost={"$sum": "$cost"},
            conversions={"$sum": "$conversions"},
        )
        [
            sum_field.update({fd: {
                "$sum": "${}".format(fd)
            }}) for fd in fields if fd in field_dict
        ]
        return sum_field

    @classmethod
    def create_aggregate_groups(cls, fields_dict, sum_field):
        group_fields = [
            'affiliate_id', 'affiliate_name', 'affiliate_sub_id_1',
            'affiliate_sub_id_2', 'affiliate_sub_id_3', 'affiliate_sub_id_4',
            'affiliate_sub_id_5', 'offer_id', 'offer_name', 'advertiser_id',
            'advertiser_name', 'category_name', 'country', 'category_status',
            'hour', 'day', 'week', 'month', 'year'
        ]
        groups = {'_id': {}}
        [
            groups['_id'].update({fd: "${}".format(fd)}) for fd in group_fields
            if fd in fields_dict
        ]
        if 'offer_name' in fields_dict:
            groups['_id'].update({'offer_id': "$offer_id"})
        if 'affiliate_name' in fields_dict:
            groups['_id'].update({'affiliate_id': "$affiliate_id"})
        if 'advertiser_name' in fields_dict:
            groups['_id'].update({'advertiser_id': "$advertiser_id"})
        if 'api_offer_name' in fields_dict:
            groups['_id'].update({'offer_name': "$offer_name"})
        groups.update(sum_field)
        return groups

    # aggregate end

    @classmethod
    def create_calculate_result(cls, fields_dict, results):
        calculate_fields = [
            'CPC', 'CPM', 'CTR', 'CR', 'CPA', 'RPM', 'RPC', 'RPA', 'EPC'
        ]
        calculate = [fd for fd in calculate_fields if fd in fields_dict]

        def result_update(r):
            for fd in calculate:
                res = getattr(cls, fd)(r)
                r.update({fd: res})
            return r

        results = map(result_update, results)

        return results

    @classmethod
    def total(cls, datas, fields):
        total = defaultdict(lambda: 0)
        count_fields = [
            'impressions', 'clicks', 'gross_clicks', 'unique_clicks',
            'conversions', 'revenue', 'profit', 'cost', 'sales'
        ]

        if fields:
            fields_dict = dict(
                impressions=1,
                clicks=1,
                conversions=1,
                # revenue=1,
                cost=1)
            [
                fields_dict.update({field: 1})
                for field, value in fields.items() if value
            ]

        count_fields = [fd for fd in count_fields if fd in fields_dict]

        for doc in datas:
            for fd in count_fields:
                total[fd] += doc.get(fd, 0)

        result = cls.create_calculate_result(fields_dict, [total])
        return result

    @classmethod
    def CTR(cls, result):
        """
            Click-Through-Rate
        """
        clicks, impressions = result.get('clicks',
                                         0), result.get('impressions')
        return '%0.2f' % (clicks / impressions * 100) if impressions else '*'

    @classmethod
    def CPC(cls, result):
        """
            Cost Per Click
        """
        cost, clicks = result.get('cost', 0), result.get('clicks')
        return '%0.2f' % (cost / clicks) if clicks else '*'

    @classmethod
    def CPM(cls, result):
        """
            Cost Per Thousand Impression
        """
        cost, impressions = result.get('cost', 0), result.get('impressions')
        return '%0.2f' % (cost / impressions * 1000) if impressions else '*'

    @classmethod
    def CR(cls, result):
        """
            Cost Per Thousand Impression
        """
        conversions, clicks = result.get('conversions',
                                         0), result.get('clicks')
        return '%0.2f' % (conversions / clicks * 100) if clicks else '*'

    @classmethod
    def CPA(cls, result):
        """
            Cost Per conversion
        """
        cost, conversions = result.get('cost', 0), result.get('conversions')
        return '%0.2f' % (cost / conversions) if conversions else '*'

    @classmethod
    def RPM(cls, result):
        """
            Revenue Per Thousand Impression
        """
        revenue, impressions = result.get('revenue',
                                          0), result.get('impressions')
        return '%0.2f' % (revenue / impressions * 1000) if impressions else '*'

    @classmethod
    def RPC(cls, result):
        """
            Revenue Per Thousand Impression
        """
        revenue, clicks = result.get('revenue', 0), result.get('clicks')
        return '%0.2f' % (revenue / clicks * 100) if clicks else '*'

    @classmethod
    def RPA(cls, result):
        """
            Revenue Per conversion
        """
        revenue, conversions = result.get('revenue',
                                          0), result.get('conversions')
        return '%0.2f' % (revenue / conversions) if conversions else '*'

    @classmethod
    def EPC(cls, result):
        """
            cost Per hundred conversion
        """
        cost, clicks = result.get('cost', 0), result.get('clicks')
        return '%0.2f' % (cost / clicks * 100) if clicks else '*'
Example #10
0
def test_delete_video_duplicate_documents_one_file(app, ACCOUNTS, ASSETS):
    from uuid import uuid4
    from shutil import copyfile
    from os.path import isfile
    from bson.objectid import ObjectId
    from mongokit import Document, Connection
    from hummedia import config
    from hummedia.models import Video, AssetGroup
    from bson.objectid import ObjectId

    connection = Connection(host=config.MONGODB_HOST, port=config.MONGODB_PORT)
    video = connection[Video.__database__][Video.__collection__]

    pid = str(ObjectId())
    filename = str(ObjectId())
    filepath = config.MEDIA_DIRECTORY + filename + '.mp4'
    copyfile(ASSETS + 'fire.mp4', filepath)
    video.insert({
        "_id": pid,
        "@context": {
            "dc:type": "@type",
            "hummedia": "http://humanities.byu.edu/hummedia/",
            "ma": "http://www.w3.org/ns/ma-ont/",
            "dc": "http://purl.org/dc/elements/1.1/",
            "dc:identifier": "@id"
        },
        "@graph": {
            "dc:coverage":
            "private",
            "dc:creator":
            "testuser",
            "dc:date":
            "2013-08-29",
            "dc:identifier":
            "hummedia:id/video/" + pid,
            "dc:rights": {
                "read": [],
                "write": []
            },
            "dc:type":
            "hummedia:type/humvideo",
            "ma:averageBitRate":
            903903,
            "ma:date":
            1970,
            "ma:description":
            "None",
            "ma:duration":
            0,
            "ma:features": [],
            "ma:frameHeight":
            360,
            "ma:frameRate":
            25,
            "ma:frameSizeUnit":
            "px",
            "ma:frameWidth":
            720,
            "ma:hasContributor": [],
            "ma:hasGenre": {
                "@id": None,
                "name": None
            },
            "ma:hasKeyword": ["film", "German"],
            "ma:hasLanguage": ["en"],
            "ma:hasPolicy": [],
            "ma:hasRelatedResource": [],
            "ma:height":
            400,
            "ma:isCopyrightedBy": {
                "@id": None,
                "name": None
            },
            "ma:isMemberOf": [{
                "@id": "anothertest",
                "title": "Test Videos"
            }, {
                "@id": "testid",
                "title": "testtitle"
            }],
            "ma:isRelatedTo": [],
            "ma:locator": [{
                "@id": filename,
                "ma:hasFormat": "video/mp4",
                "ma:hasCompression": {
                    "@id": "http://www.freebase.com/view/en/h_264_mpeg_4_avc",
                    "name": "avc.42E01E"
                }
            }],
            "ma:title":
            "Test Video",
            "pid":
            pid
        }
    })

    pid2 = str(ObjectId())
    video.insert({
        "_id": pid2,
        "@context": {
            "dc:type": "@type",
            "hummedia": "http://humanities.byu.edu/hummedia/",
            "ma": "http://www.w3.org/ns/ma-ont/",
            "dc": "http://purl.org/dc/elements/1.1/",
            "dc:identifier": "@id"
        },
        "@graph": {
            "dc:coverage":
            "private",
            "dc:creator":
            "testuser",
            "dc:date":
            "2013-08-29",
            "dc:identifier":
            "hummedia:id/video/" + pid2,
            "dc:rights": {
                "read": [],
                "write": []
            },
            "dc:type":
            "hummedia:type/humvideo",
            "ma:averageBitRate":
            903903,
            "ma:date":
            1970,
            "ma:description":
            "None",
            "ma:duration":
            0,
            "ma:features": [],
            "ma:frameHeight":
            360,
            "ma:frameRate":
            25,
            "ma:frameSizeUnit":
            "px",
            "ma:frameWidth":
            720,
            "ma:hasContributor": [],
            "ma:hasGenre": {
                "@id": None,
                "name": None
            },
            "ma:hasKeyword": ["film", "German"],
            "ma:hasLanguage": ["en"],
            "ma:hasPolicy": [],
            "ma:hasRelatedResource": [],
            "ma:height":
            400,
            "ma:isCopyrightedBy": {
                "@id": None,
                "name": None
            },
            "ma:isMemberOf": [{
                "@id": "anothertest",
                "title": "Test Videos"
            }, {
                "@id": "testid",
                "title": "testtitle"
            }],
            "ma:isRelatedTo": [],
            "ma:locator": [{
                "@id": filename,
                "ma:hasFormat": "video/mp4",
                "ma:hasCompression": {
                    "@id": "http://www.freebase.com/view/en/h_264_mpeg_4_avc",
                    "name": "avc.42E01E"
                }
            }],
            "ma:title":
            "Test Video",
            "pid":
            pid2
        }
    })

    app.login(ACCOUNTS['SUPERUSER'])

    response = app.delete('/video/' + pid)
    assert isfile(filepath), "Video was deleted with two references."

    response = app.delete('/video/' + pid2)
    assert not isfile(
        filepath
    ), "Video was not deleted after all references to the video were removed."
Example #11
0
def get_conn(server, port):
    uri = "mongodb://{}:{}@{}:{}".format(DATA_SERVER_USERNAME,
                                         DATA_SERVER_PASSWORD,
                                         server, port)
    conn = Connection(uri)
    return conn
Example #12
0
class ReportInterface(object):

    db = Connection(MONGO_CONFIG.get('host')).leadhug

    @classmethod
    def get_range_data(cls, start, end, fields=None, limit=30, skip=0, **kw):
        '''
            args: fields-->[]
                  start--->'2016-03-12'
                  end--->'2016-03-15'
                  kw---> {...}
        '''
        if fields:
            fields_dict = {}
            [
                fields_dict.update({field: 1})
                for field, value in fields.items() if value
            ]

        condition = cls.create_condition(**kw)
        condition.update({'day': {'$gte': start.strip(), '$lte': end.strip()}})

        collection = cls.db.reportHour if cls.__name__ == 'ReportHour' else cls.db.reportDay

        total_count, results = cls.get_result(collection, fields_dict,
                                              condition)
        # total_count, results = cls.get_result2(collection, fields_dict, condition, limit, skip)

        calculate_result = cls.create_calculate_result(fields_dict, results)
        return calculate_result, total_count

    @classmethod
    def get_result2(cls, collection, fields_dict, match, limit, skip):
        sum_field = cls.create_sum_field(fields_dict)
        groups2 = cls.create_groups2(fields_dict, sum_field)
        total_count = collection.aggregate([
            {
                "$match": match
            },
            {
                "$group": groups2
            },
            {
                "$sort": SON([("_id", 1)])
            },
        ]).get('result').__len__() if not skip else 0
        res = collection.aggregate([{
            "$match": match
        }, {
            "$group": groups2
        }, {
            "$sort": {
                'day': 1
            }
        }, {
            "$skip": skip
        }, {
            "$limit": limit
        }])
        result = res.get('result')
        for r in result:
            fields = r.get('_id')
            r.pop('_id')
            r.update(fields)
        return total_count, result

    @classmethod
    def create_sum_field(cls, field_dict):
        fields = [
            'impressions', 'clicks', 'gross_clicks', 'unique_clicks',
            'conversions', 'cost', 'revenue', 'profit', 'sales'
        ]

        sum_field = {}
        [
            sum_field.update({fd: {
                "$sum": "${}".format(fd)
            }}) for fd in fields if fd in field_dict
        ]
        return sum_field

    @classmethod
    def create_groups2(cls, fields_dict, sum_field):
        group_fields = [
            'hour',
            'day',
            'week',
            'month',
            'year'
            'affiliate_id',
            'affiliate_name',
            'affiliate_sub_id_1',
            'affiliate_sub_id_2',
            'affiliate_sub_id_3',
            'affiliate_sub_id_4',
            'affiliate_sub_id_5',
            'offer_id',
            'offer_name',
            'advertiser_id',
            'advertiser_name',
            'category_name',
            'country',
            'category_status',
        ]
        groups = {'_id': {}}
        [
            groups['_id'].update({fd: "${}".format(fd)}) for fd in group_fields
            if fd in fields_dict
        ]
        groups.update(sum_field)
        return groups

    @classmethod
    def get_result(cls, collection, fields_dict, condition):
        initial = cls.create_initial(fields_dict)
        groups = cls.create_group(fields_dict)
        reducer = cls.create_reducer(initial)
        results = collection.group(key=groups,
                                   condition=condition,
                                   initial=initial,
                                   reduce=reducer,
                                   finalize=None)
        return len(results), results

    @classmethod
    def create_reducer(cls, initial):
        prev_str = ''
        for k, v in initial.items():
            prev_str += 'prev.{k} += obj.{k};'.format(k=k)

        reduce_code = 'function(obj, prev){%s}' % prev_str
        return Code(reduce_code)

    @classmethod
    def create_initial(cls, field_dict):
        fields = [
            'gross_clicks', 'unique_clicks', 'profit', 'sales', 'revenue'
        ]

        initial = dict(impressions=0, clicks=0, cost=0, conversions=0)
        [initial.update({fd: 0}) for fd in fields if fd in field_dict]
        return initial

    @classmethod
    def create_condition(cls, **kw):
        spec = OrderedDict()
        affiliates = kw.get('affiliates_name')
        if affiliates:
            spec.update(
                dict(affiliate_id={"$in": [int(aff) for aff in affiliates]}))

        advertisers = kw.get('advertisers_name')
        if advertisers:
            spec.update(
                dict(advertiser_id={"$in": [int(a) for a in advertisers]}))

        offers = kw.get('offers')
        if offers:
            spec.update(dict(offer_id={"$in": [int(i) for i in offers]}))

        countries = kw.get('countries')
        if countries:
            spec.update(dict(country={"$in": countries}))

        payout_types = kw.get('payout_types')
        if payout_types:
            spec.update(dict(payout_type={"$in": payout_types}))

        # payout_range = kw.get('payout_range')
        # if payout_range:
        #     spec.update(dict(cost={
        #         "$gte": float(payout_range.get('min', 0)),
        #         "$lte": float(payout_range.get('mix', 10000))
        #     }))

        categories = kw.get('categories_name')
        if categories:
            spec.update(dict(category_name={"$in": categories}))

        return spec

    @classmethod
    def create_group(cls, fields_dict):
        group_fields = [
            'affiliate_id', 'affiliate_name', 'affiliate_sub_id_1',
            'affiliate_sub_id_2', 'affiliate_sub_id_3', 'affiliate_sub_id_4',
            'affiliate_sub_id_5', 'offer_id', 'offer_name', 'advertiser_id',
            'advertiser_name', 'category_name', 'country', 'hour', 'day',
            'week', 'month', 'year'
        ]
        groups = {}
        [groups.update({fd: 1}) for fd in group_fields if fd in fields_dict]
        return groups

    @classmethod
    def create_calculate_result(cls, fields_dict, results):
        calculate_fields = [
            'CPC', 'CPM', 'CTR', 'CR', 'CPA', 'RPM', 'RPC', 'RPA', 'EPC'
        ]
        calculate = [fd for fd in calculate_fields if fd in fields_dict]

        def result_upate(r):
            for fd in calculate:
                res = getattr(cls, fd)(r)
                r.update({fd: res})
            return r

        results = map(result_upate, results)

        return results

    @classmethod
    def total(cls, datas, fields):
        total = defaultdict(lambda: 0)
        count_fields = [
            'impressions', 'clicks', 'gross_clicks', 'unique_clicks',
            'conversions', 'revenue', 'profit', 'cost', 'sales'
        ]

        if fields:
            fields_dict = dict(impressions=1,
                               clicks=1,
                               conversions=1,
                               revenue=1,
                               cost=1)
            [
                fields_dict.update({field: 1})
                for field, value in fields.items() if value
            ]

        count_fields = [fd for fd in count_fields if fd in fields_dict]

        for doc in datas:
            for fd in count_fields:
                total[fd] += doc.get(fd, 0)

        result = cls.create_calculate_result(fields_dict, [total])
        return result

    @classmethod
    def CTR(cls, result):
        """
            Click-Through-Rate
        """
        clicks, impressions = result.get('clicks',
                                         0), result.get('impressions')
        return '%0.2f' % (clicks / impressions * 100) if impressions else '*'

    @classmethod
    def CPC(cls, result):
        """
            Cost Per Click
        """
        cost, clicks = result.get('cost', 0), result.get('clicks')
        return '%0.2f' % (cost / clicks) if clicks else '*'

    @classmethod
    def CPM(cls, result):
        """
            Cost Per Thousand Impression
        """
        cost, impressions = result.get('cost', 0), result.get('impressions')
        return '%0.2f' % (cost / impressions * 1000) if impressions else '*'

    @classmethod
    def CR(cls, result):
        """
            Cost Per Thousand Impression
        """
        conversions, clicks = result.get('conversions',
                                         0), result.get('clicks')
        return '%0.2f' % (conversions / clicks * 100) if clicks else '*'

    @classmethod
    def CPA(cls, result):
        """
            Cost Per conversion
        """
        cost, conversions = result.get('cost', 0), result.get('conversions')
        return '%0.2f' % (cost / conversions) if conversions else '*'

    @classmethod
    def RPM(cls, result):
        """
            Revenue Per Thousand Impression
        """
        revenue, impressions = result.get('revenue',
                                          0), result.get('impressions')
        return '%0.2f' % (revenue / impressions * 1000) if impressions else '*'

    @classmethod
    def RPC(cls, result):
        """
            Revenue Per Thousand Impression
        """
        revenue, clicks = result.get('revenue', 0), result.get('clicks')
        return '%0.2f' % (revenue / clicks * 100) if clicks else '*'

    @classmethod
    def RPA(cls, result):
        """
            Revenue Per conversion
        """
        revenue, conversions = result.get('revenue',
                                          0), result.get('conversions')
        return '%0.2f' % (revenue / conversions) if conversions else '*'

    @classmethod
    def EPC(cls, result):
        """
            cost Per hundred conversion
        """
        cost, clicks = result.get('cost', 0), result.get('clicks')
        return '%0.2f' % (cost / clicks * 100) if clicks else '*'
Example #13
0
 def create(**kwargs):
     db = Mongo()
     db.connection = Connection(kwargs['host'], kwargs['port'])
     Mongo.db = db
     if 'debug' in kwargs:
         print Mongo.db.connection
Example #14
0
# -*- coding: utf-8 -*-
import os
from .mongodoc import Attention
from mongokit import Connection

# config
MONGODB_HOST = os.getenv("REST_MONGO_HOST")
MONGODB_PORT = int(os.getenv("REST_MONGO_PORT"))

connection = Connection(MONGODB_HOST, MONGODB_PORT)
connection.register([Attention])
Example #15
0
 def __new__(self, *arg):
     if not self.connection:
         host_port = list(arg)
         self.connection = Connection(host_port[0], int(host_port[1]))
     return self.connection
def get_db():
    con = Connection()
    con.register([User, Event])
    return con['worklog']
Example #17
0
 def __init__(self, handlers, database_name=None, **kwargs):
     tornado.web.Application.__init__(self, handlers, **kwargs)
     self.database_name = database_name and database_name or options.database_name
     self.con = Connection()
     self.con.register([User, ChatMessage])
Example #18
0
def test_patch_video_25_fps(app, ACCOUNTS):
    from mongokit import Document, Connection
    from hummedia import config
    from hummedia.models import Video, AssetGroup
    from bson.objectid import ObjectId

    connection = Connection(host=config.MONGODB_HOST, port=config.MONGODB_PORT)

    video = connection[Video.__database__][Video.__collection__]

    pid = str(ObjectId())
    video.insert({
        "_id": pid,
        "@context": {
            "dc:type": "@type",
            "hummedia": "http://humanities.byu.edu/hummedia/",
            "ma": "http://www.w3.org/ns/ma-ont/",
            "dc": "http://purl.org/dc/elements/1.1/",
            "dc:identifier": "@id"
        },
        "@graph": {
            "dc:coverage":
            "private",
            "dc:creator":
            "testuser",
            "dc:date":
            "2013-08-29",
            "dc:identifier":
            "hummedia:id/video/" + pid,
            "dc:rights": {
                "read": [],
                "write": []
            },
            "dc:type":
            "hummedia:type/humvideo",
            "ma:averageBitRate":
            903903,
            "ma:date":
            1970,
            "ma:description":
            "None",
            "ma:duration":
            0,
            "ma:features": [],
            "ma:frameHeight":
            360,
            "ma:frameRate":
            25,
            "ma:frameSizeUnit":
            "px",
            "ma:frameWidth":
            720,
            "ma:hasContributor": [],
            "ma:hasGenre": {
                "@id": None,
                "name": None
            },
            "ma:hasKeyword": ["film", "German"],
            "ma:hasLanguage": ["en"],
            "ma:hasPolicy": [],
            "ma:hasRelatedResource": [],
            "ma:height":
            400,
            "ma:isCopyrightedBy": {
                "@id": None,
                "name": None
            },
            "ma:isMemberOf": [{
                "@id": "anothertest",
                "title": "Test Videos"
            }, {
                "@id": "testid",
                "title": "testtitle"
            }],
            "ma:isRelatedTo": [],
            "ma:locator": [{
                "@id": "tommytutone",
                "ma:hasFormat": "video/mp4",
                "ma:hasCompression": {
                    "@id": "http://www.freebase.com/view/en/h_264_mpeg_4_avc",
                    "name": "avc.42E01E"
                }
            }, {
                "@id": "tommytutone",
                "ma:hasFormat": "video/webm",
                "ma:hasCompression": {
                    "@id": "http://www.freebase.com/m/0c02yk5",
                    "name": "vp8.0"
                }
            }],
            "ma:title":
            "Test Video",
            "pid":
            pid
        }
    })

    app.login(ACCOUNTS['SUPERUSER'])
    patch = {"ma:title": "Castaway on the Moon", "ma:isMemberOf": []}
    result = app.patch('/video/' + pid,
                       data=json.dumps(patch),
                       content_type='application/json')
    raw = result.data

    assert result.status_code is 200

    data = json.loads(result.data)
    assert data['ma:title'] == patch['ma:title']
Example #19
0
 def connect():        
     con = Connection()
     routes = con.amdoraft.routes
     stops = con.amdoraft.stops
     return
Example #20
0
    def __call__(self, doc=None, gen_skel=False,
                 lang='en', fallback_lang='en'):
        return self._obj_class(
            doc=doc,
            gen_skel=gen_skel,
            collection=self.collection,
            lang=lang,
            fallback_lang=fallback_lang
        )


mongokit.connection.CallableMixin = CallableMixin
_iterables = (list, tuple, set, frozenset)
MONGO_CONFIG = dict(host=os.getenv("MONGO_HOST", "mongodb://127.0.0.1:27017/"))
MONGO_DB = os.getenv("MONGO_DB", "gateway")
mongo = Connection(**MONGO_CONFIG)


class JsOb(object):

    def __init__(self, *args, **kwds):
        for i in args:
            self.__dict__.update(args)
        self.__dict__.update(kwds)

    def __getattr__(self, name):
        return self.__dict__.get(name, '')

    def __setattr__(self, name, value):
        self.__dict__[name] = value
Example #21
0
 def setUp(self):
     self.connection = Connection(safe=True)
     self.col = self.connection['test']['mongolite']
Example #22
0
def get_connection():
    conn = Connection("localhost", 27017)
    dbauth = conn.Magic2.authenticate(configParser.get("Mongo","username"),configParser.get("Mongo","password"))
    return conn
from mongokit import Connection
db = Connection()


def get_db():
    return db
Example #24
0
# -*- coding: utf-8 -*-
from mongokit import Document, Connection
from datetime import datetime
import os

MONGO_DBNAME = 'aerost8'
MONGO_COLLECTION = 'issues'
MONGO_URI = os.environ.get('MONGO_URI', 'mongodb://localhsot:27017/aerost8')
connection = Connection(MONGO_URI)


@connection.register
class Issue(Document):
    __collection__ = MONGO_COLLECTION
    __database__ = MONGO_DBNAME
    use_schemaless = True
    structure = {
        'issue':
        int,
        'content':
        basestring,
        'paragraphs': [basestring],
        'excerpt':
        basestring,
        'slug':
        basestring,
        'title':
        basestring,
        'date':
        datetime,
        'track':
Example #25
0
 def setUp(self):
     self.connection = Connection()
     self.col = self.connection['test']['mongokit']
Example #26
0
def generate_index(host=None, port=None, database=None):
    con = Connection(host, port)
    con.register([Article])
    db = Database(con, database)

    db.Article.generate_index(db.article)
Example #27
0
from mongokit import Connection
'''
Extension for Mongokit support

1. Auto Connection support
2. easy way to use Document Class

@with_conn
class BlogPost(Document):
    pass

You do not have to use conn.BlogPost(), just use BlogPost() to do this.

~~~~~~~~~~~~~
Inheritance:
class Comment(BlogPost.__clsobj__):
    pass
'''

conn = Connection()


def with_conn(cls):
    '''auto_register and replace cls with conn'''
    #print cls_obj.__name__
    conn.register([cls])
    cls.__clsobj__ = cls
    #save cls self to cls.__clsobj__, may be useful when Inheritance
    return getattr(conn, cls.__name__)
#
# Para los usuarios podríamos usar algo así:
# * `created_at`
# * `screen_name`
# * `name`
# * `description`
# * `favourites_count`
# * `followers_count`
# * `friends_count`
# * `profile_image_url`
#
# Algunos de ellos no existen como tal en los resultados de la API, pero para ello podremos programar funciones que nos transformen los resultados en datos válidos para el modelo.

# MongoDB configuration
# Creamos la conexión con MongoDB y recuperamos la BD que vamos a utilizar
connection = Connection(host='localhost', port=27017)
DB_NAME = 'jose_perez_yazquez'
db = connection[DB_NAME]

## Definición de los modelos, llamados Documentos por mongokit


# Creamos el modelo para los tweets
@connection.register
class Tweet(Document):
    __collection__ = 'tweets'
    __database__ = DB_NAME
    structure = {
        'created_at': datetime.datetime,
        'text': basestring,
        'retweet_count': int,
Example #29
0
    '/', 'index',
    (lambda: header_text + say_hello() + instructions + footer_text))

# add a rule when the page is accessed with a name appended to the site
# URL.
application.add_url_rule('/<username>', 'hello',
                         (lambda username: header_text + say_hello(username) +
                          home_link + footer_text))

MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017

app = Flask(__name__)
app.config.from_object(__name__)

connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])


def max_length(length):
    def validate(value):
        if len(value) <= length:
            return True
        raise ValidationError(
            '%s must be at most {} characters long'.format(length))

    return validate


class User(Document):
    structure = {
        'name': unicode,
Example #30
0
def create_app(config_name='development'):
    app = Flask(__name__)

    # config
    app.config.from_object(config[config_name])
    app.debug = app.config.get('DEBUG')
    app.json_encoder = Encoder  # bson.ObjectId, encode as string repr

    # database connections
    mongodb_database = Connection(host=app.config.get("MONGODB_HOST"),
                                  port=app.config.get("MONGODB_PORT"))
    mongodb_conn = mongodb_database[app.config.get("MONGODB_DATABASE")]

    # inject database connections to app object
    app.mongodb_database = mongodb_database
    app.mongodb_conn = mongodb_conn

    # register error handlers
    @app.errorhandler(404)
    def app_error_404(error):
        current_app.logger.warn("Error: 404\n{}".format(
            traceback.format_exc()))
        return make_json_response(NotFound())

    @app.errorhandler(405)
    def app_error_405(error):
        current_app.logger.warn("Error: 405\n{}".format(
            traceback.format_exc()))
        return make_json_response(MethodNotAllowed())

    @app.errorhandler(400)
    def app_error_400(error):
        current_app.logger.warn("Error: 400\n{}".format(
            traceback.format_exc()))
        return make_json_response(ErrUncaughtException(repr(error)))

    # register before request handlers
    @app.before_request
    def app_before_request():
        # cors response
        if request.method == "OPTIONS":
            resp = current_app.make_default_options_response()
            cors_headers = make_cors_headers()
            resp.headers.extend(cors_headers)
            return resp
        return

    # register blueprints
    from blueprints.user import blueprint as user_module
    app.register_blueprint(user_module, url_prefix="/user")

    from blueprints.group import blueprint as group_module
    app.register_blueprint(group_module, url_prefix="/group")

    from blueprints.order import blueprint as order_module
    app.register_blueprint(order_module, url_prefix="/order")

    from blueprints.menu import blueprint as menu_module
    app.register_blueprint(menu_module, url_prefix="/menu")

    return app