def get_counts(request):
    """
    Get total counts for the number of emails and phones blocked
    """
    try:
        ecount = db.blocked_email.count()
        pcount = db.blocked_phone.count()
        return jsonResponse({"success": True, "email": ecount, "phone": pcount})
    except Exception, e:
        return jsonResponse({"success": False, "error": "Exception: " + str(e)})
Beispiel #2
0
def cancel_job(request):
    """
    Cancel the given job
    json body: { id: Object id, t_id: tool id }
    """
    try:
        data = json.loads(request.body)
        oid = data.get('id', data.get('oid', data.get('_id')))
        t_id = int(data.get('t_id', 0))
        if oid is not None:
            search = {"_id": ObjectId(oid)}
        elif t_id != 0:
            search = {"job.t_id": t_id}
        else:
            return jsonResponse({
                "success":
                False,
                "error":
                "Cannot find job, please give either an id or a t_id"
            })

        search.update({"job": {"$exists": True}})
        job = db.jobs.find_one(search, {"job": True})

        worksheet = get_scheduler_sheet()

        if not job:
            return jsonResponse({"success": False, "error": "Cannot find job"})
        elif 'sheet_row' in job['job']:  # We know the exact row number
            worksheet.update_acell("J" + str(job['job']['sheet_row']),
                                   "Cancel")
        else:
            full = worksheet.get_all_records()
            if t_id == 0:
                t_id = job['job'].get('t_id', 0)

            if t_id == 0:
                return jsonResponse({"success": False, "error": "Need t_id"})

            t_id = str(t_id)
            for record in full:
                if t_id == str(record['ID']):
                    row = full.index(record) + 2
                    worksheet.update_acell("J" + str(row), "Cancel")
                    break
        db.jobs.update_one({"_id": job['_id']},
                           {"$set": {
                               "job.status": "Cancel"
                           }})
        return basic_success

    except Exception, e:
        return basic_error(e)
def job_update(request):
    """
    API endpoint for sms tool to update job status and all of that
    Refer docs/status_update_format.json
    """
    try:

        if request.method == 'GET':
            query_dict = request.GET
        else:
            query_dict = json.loads(request.body)

        update = {}
        p_update = {}

        for key in ['t_id', 'file_link']:
            if key in query_dict:
                update['job.' + key] = query_dict[key]
        if 'status' in query_dict:
            p_update['job.status'] = {
                'status': query_dict['status'],
                'time': datetime.now()
            }

        for key in ['customer_count', 'sms_sent', 'sms_failed', 'errors']:
            if key in query_dict:
                update['job.report.' + key] = query_dict[key]

        if 'id' not in query_dict or not (update or p_update):
            return jsonResponse({
                "success": False,
                "query": query_dict,
                "update": update,
                "p_update": p_update
            })
        else:
            oid = query_dict['id']
            if oid.endswith('_segment'):
                oid = oid.replace('_segment', '')
                collection = db.segment_jobs
            else:
                collection = db.jobs

            final_update = {}
            if update:
                final_update["$set"] = update
            if p_update:
                final_update["$push"] = p_update

            collection.update_one({"_id": ObjectId(oid)}, final_update)
            return jsonResponse({"success": True})
    except Exception, e:
        return basic_error(e)
def login(request):
    """
    Simple login protocol
    method: POST
    post data: { username: string, password: <md5 hash> String }
    """
    try:
        data = json.loads(request.body)
        if db.credentials.count({"username": data['username'], "password": data['password']}) > 0:
            return jsonResponse({"success": True})
        else:
            return jsonResponse({"success": False})
    except Exception, e:
        return basic_error(e)
def block(request):
    """
    GET/POST based method for blocking a phone and/or email
    Parameters: (?: optional, +: required)
        ? email: email address to block
        ? phone: phone number to block
        ? language: a list of one or more languages, separated by comma, to be blocked for given phone.
            Only makes sense when the phone key is given. Supports English, Arabic
        ? pretty (true or false): If set to true, then results in an http response (yet to be styled),
            useful for user links. If set to false or not given, then results in a json response

    tested on Wed, 26 Aug, 10:45 PM
    """
    if request.method == 'GET':
        main_data = request.GET
    else:
        main_data = json.loads(request.body)

    if 'data' in main_data:
        main_data = main_data['data']
    else:
        main_data = [main_data]

    res = []
    for data in main_data:
        d = {}
        # ----- Email ------ #
        if 'email' in data:
            d['email entry'] = _block_email(data['email'])

        # ----- Phone ------ #
        if 'phone' in data:
            d['phone entry'] = _block_phone(data['phone'],
                                            data.get('language'))

        res.append(d)

    if 'pretty' in data and data['pretty'] not in [False, 'false']:
        if not res:
            return HttpResponse(
                "There seems to be some problem. You seem to be already unsubscribed"
            )
        else:
            return HttpResponse("You have been successfully unsubscribed")
    else:
        if not res:
            return jsonResponse({"success": False})
        else:
            return jsonResponse({"success": True, "result": res})
Beispiel #6
0
def form_post(request):
    """ Form submission api """
    try:
        data = json.loads(request.body)
        # Do processing here
        if data.get('segmented', False):
            row = ['No Send', 'external', '_', '', '1', '1', '_', '_']
            data.pop('campaign_config', {})
        else:
            campaign = data['campaign_config']
            repeat = campaign.get('repeat', 'Once')
            start_date = datetime.fromtimestamp(campaign['start_date'] / 1000).strftime("%m/%d/%Y")
            if 'end_date' in campaign:
                end_date = datetime.fromtimestamp(campaign['end_date'] / 1000).strftime("%m/%d/%Y")
            else:
                end_date = ''

            time = datetime.fromtimestamp(campaign['time'] / 1000)
            hour = time.hour
            minute = time.minute

            english = campaign['text']['english']
            arabic = campaign['text']['arabic']

            if len(english.strip()) == 0:
                english = '_'
            if len(arabic.strip()) == 0:
                arabic = '_'

            row = [repeat, 'external', start_date, end_date, hour, minute, english, arabic]

        data['name'] = data.get('name', 'Untitled')  # Add name and description
        data['description'] = data.get('description', '')
        data['job'] = {'status': [{'status': 'Pending', 'time': datetime.now()}]}  # Add the job subdocument, will be used later
        debug = data.pop('debug', False)

        data['timestamp'] = datetime.now()
        result = db.jobs.insert_one(data)  # >> Insertion here
        row.append(str(result.inserted_id))

        if debug:
            # db.jobs.remove({"_id": result.inserted_id})
            return jsonResponse({'success': True, 'data_received': data, 'row created': row})
        else:
            append_to_sheet(row)
            return jsonResponse({'success': True})

    except Exception, e:
        return basic_error(e)
def job_update(request):
    """
    API endpoint for sms tool to update job status and all of that
    Refer docs/status_update_format.json
    """
    try:

        if request.method == 'GET':
            query_dict = request.GET
        else:
            query_dict = json.loads(request.body)

        update = {}
        p_update = {}

        for key in ['t_id', 'file_link']:
            if key in query_dict:
                update['job.' + key] = query_dict[key]
        if 'status' in query_dict:
            p_update['job.status'] = {
                'status': query_dict['status'],
                'time': datetime.now()
            }

        for key in ['customer_count', 'sms_sent', 'sms_failed', 'errors']:
            if key in query_dict:
                update['job.report.' + key] = query_dict[key]

        if 'id' not in query_dict or not (update or p_update):
            return jsonResponse({"success": False, "query": query_dict, "update": update, "p_update": p_update})
        else:
            oid = query_dict['id']
            if oid.endswith('_segment'):
                oid = oid.replace('_segment', '')
                collection = db.segment_jobs
            else:
                collection = db.jobs

            final_update = {}
            if update:
                final_update["$set"] = update
            if p_update:
                final_update["$push"] = p_update

            collection.update_one({"_id": ObjectId(oid)}, final_update)
            return jsonResponse({"success": True})
    except Exception, e:
        return basic_error(e)
Beispiel #8
0
def get_segment_jobs(request):
    master_cache = {}  # A cache of master jobs data

    lst = db.segment_jobs.aggregate([{
        "$group": {
            "_id": {
                "ref_job": "$ref_job",
                "timestamp": "$timestamp"
            },
            "segments": {
                "$push": {
                    "status": "$job.status",
                    "english": "$text.english",
                    "arabic": "$text.arabic",
                    "date": "$date",
                    "num": "$segment_number"
                }
            }
        }
    }, {
        "$project": {
            "_id": 0,
            "segments": 1,
            "ref_job": "$_id.ref_job",
            "timestamp": "$_id.timestamp"
        }
    }, {
        "$sort": {
            "timestamp": -1
        }
    }])
    final = []
    for job in lst:
        if str(job["ref_job"]) in master_cache:
            job.update(master_cache[str(job["ref_job"])])
        else:
            master = db.jobs.find_one({"_id": job["ref_job"]}, {
                "_id": False,
                "job": True,
                "name": True,
                "description": True
            })
            if not master:
                continue
            else:
                umaster = {
                    'name': master.get('name', 'Untitled'),
                    'description': master.get('description', '')
                }
                if 't_id' in master.get('job', {}):
                    umaster['t_id'] = master['job']['t_id']
                if 'customer_count' in master.get('job', {}).get('report', {}):
                    umaster['count'] = master['job']['report'][
                        'customer_count']

                master_cache[str(job["ref_job"])] = umaster
                job.update(umaster)
        final.append(job)

    return jsonResponse({"success": True, "data": final})
def query(request):
    """
    Get the pipeline and options for the wadi system
    """
    id = request.GET['id']
    obj = db.queries.find_one({"_id": ObjectId(id)})
    if obj:
        options = obj['target_config']
        # Customisation ----------------- #
        if 'customer' not in options:
            options['mode'] = 'all'
        else:
            cust = options.pop('customer')
            if len(cust) == 2:
                options['mode'] = 'all'
            else:
                options['mode'] = cust[0]

        if 'purchase_month' in options:
            options['purchase_month'] = [monthDict[a] for a in options['purchase_month']]

        if 'channel' in options:
            options['channel'] = map(lambda k: lasttouch_dict[k], options['channel'])
        # ------------------------------- #
        pipeline = [k for k, v in options.items() if k != 'mode']
        pipeline.append('customer')

        return jsonResponse({"pipeline": pipeline, "options": options})
    else:
        raise Http404
Beispiel #10
0
def get_jobs(request):
    jobs = db.jobs.aggregate([{
        "$match": {
            "job": {
                "$exists": True
            }
        }
    }, {
        "$sort": {
            "timestamp": -1
        }
    }, {
        "$project": {
            "name": 1,
            "description": 1,
            "timestamp": 1,
            "segmented": 1,
            "start_date": "$campaign_config.start_date",
            "end_date": "$campaign_config.end_date",
            "time": "$campaign_config.time",
            "repeat": "$campaign_config.repeat",
            "status": "$job.status",
            "file": "$job.file_link",
            "t_id": "$job.t_id",
            "count": "$job.report.customer_count"
        }
    }])

    final = []
    for job in jobs:
        if isinstance(job['status'], list):
            job['status'] = job['status'][-1]['status']
        final.append(job)
    return jsonResponse({"success": True, "data": final})
def get_blocked(request):
    """
    GET based method for retrieving blocked [phone, language] or email list
    Parameters: (?: optional, +: required)
        + type (email or phone): Get the block list type. If not present, defaults to email.
            Throws error for any other type

    Response:
        1. For type=email:
            success: true,
            data: [list of emails]
        2. For type=phone:
            success: true,
            data: [list of [phone, language]]
    Tested on Wed, 26 Aug, 11:05 PM
    """
    type = request.GET.get('type', 'email')

    if type == 'email':
        return jsonResponse({
            "success":
            True,
            "data": [
                x['email'] for x in db.blocked_email.find({}, {
                    "_id": False,
                    "timestamp": False
                })
            ]
        })
    elif type == 'phone':
        return jsonResponse({
            "success":
            True,
            "data":
            [[x['phone'], x['language']]
             for x in db.blocked_phone.aggregate([{
                 "$project": {
                     "_id": False,
                     "phone": True,
                     "language": True
                 }
             }, {
                 "$unwind": "$language"
             }])]
        })
    else:
        return jsonResponse({"success": False, "error": "Unknown type"})
def get_counts(request):
    """
    Get total counts for the number of emails and phones blocked
    """
    try:
        ecount = db.blocked_email.count()
        pcount = db.blocked_phone.count()
        return jsonResponse({
            "success": True,
            "email": ecount,
            "phone": pcount
        })
    except Exception, e:
        return jsonResponse({
            "success": False,
            "error": "Exception: " + str(e)
        })
Beispiel #13
0
def login(request):
    """
    Simple login protocol
    method: POST
    post data: { username: string, password: <md5 hash> String }
    """
    try:
        data = json.loads(request.body)
        if db.credentials.count({
                "username": data['username'],
                "password": data['password']
        }) > 0:
            return jsonResponse({"success": True})
        else:
            return jsonResponse({"success": False})
    except Exception, e:
        return basic_error(e)
def block(request):
    """
    GET/POST based method for blocking a phone and/or email
    Parameters: (?: optional, +: required)
        ? email: email address to block
        ? phone: phone number to block
        ? language: a list of one or more languages, separated by comma, to be blocked for given phone.
            Only makes sense when the phone key is given. Supports English, Arabic
        ? pretty (true or false): If set to true, then results in an http response (yet to be styled),
            useful for user links. If set to false or not given, then results in a json response

    tested on Wed, 26 Aug, 10:45 PM
    """
    if request.method == 'GET':
        main_data = request.GET
    else:
        main_data = json.loads(request.body)

    if 'data' in main_data:
        main_data = main_data['data']
    else:
        main_data = [main_data]

    res = []
    for data in main_data:
        d = {}
        # ----- Email ------ #
        if 'email' in data:
            d['email entry'] = _block_email(data['email'])

        # ----- Phone ------ #
        if 'phone' in data:
            d['phone entry'] = _block_phone(data['phone'], data.get('language'))

        res.append(d)

    if 'pretty' in data and data['pretty'] not in [False, 'false']:
        if not res:
            return HttpResponse("There seems to be some problem. You seem to be already unsubscribed")
        else:
            return HttpResponse("You have been successfully unsubscribed")
    else:
        if not res:
            return jsonResponse({"success": False})
        else:
            return jsonResponse({"success": True, "result": res})
Beispiel #15
0
def post_form(request):
    try:
        data = json.loads(request.body)
        row = []
        selected = data.get('selected', {})
        excluded = data.get('excluded', {})

        for k in ['brand', 'season', 'category', 'brick']:
            row.append(json.dumps(selected.get(k, [])))
        for k in ['percent']:
            row.append(str(selected.get(k, '')) + '%')

        for k in ['brand', 'season', 'category', 'brick']:
            row.append(json.dumps(excluded.get(k, [])))
        append_row_to_content(row)
        return jsonResponse({"success": True, "data": data})
    except Exception, e:
        return jsonResponse({"success": False, "error": "Exception, "+str(e)})
def block_list_csv(request):
    """
    Block emails and phones using the uploaded csv file
    """
    if request.method == 'POST' and 'file' in request.FILES:
        reader = csv.reader(request.FILES['file'])
        ecount, pcount = _block_using_csv(list(reader)[1:])
        total_ecount = db.blocked_email.count()
        total_pcount = db.blocked_phone.count()
        return jsonResponse({"success": True, "blocked": {
            "email": ecount,
            "phone": pcount
        }, "total_blocked": {
            "email": total_ecount,
            "phone": total_pcount
        }})
    else:
        return jsonResponse({"success": False, "error": "No file"})
def cancel_job(request):
    """
    Cancel the given job
    json body: { id: Object id, t_id: tool id }
    """
    try:
        data = json.loads(request.body)
        oid = data.get('id', data.get('oid', data.get('_id')))
        t_id = int(data.get('t_id', 0))
        if oid is not None:
            search = {"_id": ObjectId(oid)}
        elif t_id != 0:
            search = {"job.t_id": t_id}
        else:
            return jsonResponse({"success": False, "error": "Cannot find job, please give either an id or a t_id"})

        search.update({"job": {"$exists": True}})
        job = db.jobs.find_one(search, {"job": True})

        worksheet = get_scheduler_sheet()

        if not job:
            return jsonResponse({"success": False, "error": "Cannot find job"})
        elif 'sheet_row' in job['job']:  # We know the exact row number
            worksheet.update_acell("J" + str(job['job']['sheet_row']), "Cancel")
        else:
            full = worksheet.get_all_records()
            if t_id == 0:
                t_id = job['job'].get('t_id', 0)

            if t_id == 0:
                return jsonResponse({"success": False, "error": "Need t_id"})

            t_id = str(t_id)
            for record in full:
                if t_id == str(record['ID']):
                    row = full.index(record) + 2
                    worksheet.update_acell("J" + str(row), "Cancel")
                    break
        db.jobs.update_one({"_id": job['_id']}, {"$set": {"job.status": "Cancel"}})
        return basic_success

    except Exception, e:
        return basic_error(e)
Beispiel #18
0
def test(request):
    if request.method == "GET":
        extra = {"method": "GET", "requestData": request.GET}
    else:
        extra = {"method": "POST", "requestData": request.body}
    return jsonResponse({
        "success": True,
        "Message": "Test api, ECHO",
        "extra": extra
    })
def get_blocked(request):
    """
    GET based method for retrieving blocked [phone, language] or email list
    Parameters: (?: optional, +: required)
        + type (email or phone): Get the block list type. If not present, defaults to email.
            Throws error for any other type

    Response:
        1. For type=email:
            success: true,
            data: [list of emails]
        2. For type=phone:
            success: true,
            data: [list of [phone, language]]
    Tested on Wed, 26 Aug, 11:05 PM
    """
    type = request.GET.get('type', 'email')

    if type == 'email':
        return jsonResponse({
            "success": True,
            "data": [
                x['email'] for x in
                db.blocked_email.find({}, {"_id": False, "timestamp": False})
                ]
        })
    elif type == 'phone':
        return jsonResponse({
            "success": True,
            "data": [
                [x['phone'], x['language']] for x in
                db.blocked_phone.aggregate([
                    {"$project": {"_id": False, "phone": True, "language": True}},
                    {"$unwind": "$language"}
                ])
                ]
        })
    else:
        return jsonResponse({
            "success": False,
            "error": "Unknown type"
        })
Beispiel #20
0
def get_conf(request, namespace, key):
    """ Get configuration values for the various wadi tools
    :param request:
    :param namespace:
    :param key:
    :return:
    """
    if request.method == 'GET':
        res = db.configuration.find_one({"namespace": namespace})

        if not res:
            return jsonResponse({"success": False, "error": "Wrong namespace: "+namespace})

        if len(key) == 0:
            return jsonResponse({"success": True, "namespace": namespace, "keys": res['conf'].keys()})
        if key not in res['conf']:
            return jsonResponse({"success": False, "error": "Wrong key: "+key})
        else:
            return jsonResponse({"success": True, "value": res['conf'][key]})
    else:   # Updation not support just yet
        raise Http404
Beispiel #21
0
def get_conf(request, namespace, key):
    """ Get configuration values for the various wadi tools
    :param request:
    :param namespace:
    :param key:
    :return:
    """
    if request.method == 'GET':
        res = db.configuration.find_one({"namespace": namespace})

        if not res:
            return jsonResponse({"success": False, "error": "Wrong namespace: " + namespace})

        if len(key) == 0:
            return jsonResponse({"success": True, "namespace": namespace, "keys": res['conf'].keys()})
        if key not in res['conf']:
            return jsonResponse({"success": False, "error": "Wrong key: " + key})
        else:
            return jsonResponse({"success": True, "value": res['conf'][key]})
    else:  # Updation not support just yet
        raise Http404
Beispiel #22
0
def schedule_testing_send(request):
    """ Create a testing campaign which schedules sms to be sent to the selected user in the other sheet """
    try:
        data = json.loads(request.body)
        english = data.get('english', '_')
        arabic = data.get('arabic', '_')
        row = ['Immediately', 'testing', '_', '', '_', '_', english, arabic]
        append_to_sheet(row)
        return jsonResponse({"success": True})

    except Exception, e:
        return basic_error(e)
def block_list_csv(request):
    """
    Block emails and phones using the uploaded csv file
    """
    if request.method == 'POST' and 'file' in request.FILES:
        reader = csv.reader(request.FILES['file'])
        ecount, pcount = _block_using_csv(list(reader)[1:])
        total_ecount = db.blocked_email.count()
        total_pcount = db.blocked_phone.count()
        return jsonResponse({
            "success": True,
            "blocked": {
                "email": ecount,
                "phone": pcount
            },
            "total_blocked": {
                "email": total_ecount,
                "phone": total_pcount
            }
        })
    else:
        return jsonResponse({"success": False, "error": "No file"})
def formPost(request):
    """
    Form submission api
    """
    try:
        data = json.loads(request.body)
        # Do processing here

        campaign = data['campaign_config']
        date = campaign['date']
        time = datetime.strptime(campaign['time'], "%H:%M")
        hour = time.hour
        minute = time.minute
        english = campaign['text']['english']
        arabic = campaign['text']['arabic']
        if len(english.strip()) == 0:
            english = '_'
        if len(arabic.strip()) == 0:
            arabic = '_'
        data['timestamp'] = datetime.now()

        result = db.queries.insert_one(data)

        url = 'http://45.55.72.208/wadi/query?id='+str(result.inserted_id)
        row = ['Once', 'external', date, hour, minute, english, arabic, url]

        if 'debug' in data and data['debug'] is True:
            db.queries.remove({"_id": result.inserted_id})
            return jsonResponse({'success': True, 'data_received': data, 'row created': row})
        else:
            wrk_sheet = get_scheduler_sheet()
            size = len(wrk_sheet.get_all_values())
            wrk_sheet.insert_row(row, size+1)
            return jsonResponse({'success': True})

    except Exception, e:
        return basic_error(e)
Beispiel #25
0
def test(request):
    if request.method == "GET":
        extra = {
            "method": "GET",
            "requestData": request.GET
        }
    else:
        extra = {
            "method": "POST",
            "requestData": request.body
        }
    return jsonResponse({
        "success": True,
        "Message": "Test api, ECHO",
        "extra": extra
    })
def get_segment_jobs(request):
    master_cache = {}           # A cache of master jobs data

    lst = db.segment_jobs.aggregate([
        {"$group": {
            "_id": {
                "ref_job": "$ref_job",
                "timestamp": "$timestamp"
            },
            "segments": {
                "$push": {
                    "status": "$job.status",
                    "english": "$text.english",
                    "arabic": "$text.arabic",
                    "date": "$date",
                    "num": "$segment_number"
                }
            }
        }},
        {"$project": {
            "_id": 0, "segments": 1,
            "ref_job": "$_id.ref_job",
            "timestamp": "$_id.timestamp"
        }},
        {"$sort": {"timestamp": -1}}
    ])
    final = []
    for job in lst:
        if str(job["ref_job"]) in master_cache:
            job.update(master_cache[str(job["ref_job"])])
        else:
            master = db.jobs.find_one({"_id": job["ref_job"]},
                                      {"_id": False, "job": True, "name": True, "description": True})
            if not master:
                continue
            else:
                umaster = {'name': master.get('name', 'Untitled'), 'description': master.get('description', '')}
                if 't_id' in master.get('job', {}):
                    umaster['t_id'] = master['job']['t_id']
                if 'customer_count' in master.get('job', {}).get('report', {}):
                    umaster['count'] = master['job']['report']['customer_count']

                master_cache[str(job["ref_job"])] = umaster
                job.update(umaster)
        final.append(job)

    return jsonResponse({"success": True, "data": final})
def get_jobs(request):
    jobs = db.jobs.aggregate([
        {"$match": {"job": {"$exists": True}}},
        {"$sort": {"timestamp": -1}},
        {"$project": {
            "name": 1, "description": 1, "timestamp": 1, "segmented": 1,
            "start_date": "$campaign_config.start_date",
            "end_date": "$campaign_config.end_date",
            "time": "$campaign_config.time",
            "repeat": "$campaign_config.repeat",
            "status": "$job.status",
            "file": "$job.file_link",
            "t_id": "$job.t_id",
            "count": "$job.report.customer_count"
        }}
    ])

    final = []
    for job in jobs:
        if isinstance(job['status'], list):
            job['status'] = job['status'][-1]['status']
        final.append(job)
    return jsonResponse({"success": True, "data": final})
def post_segment_form(request):
    try:
        data = json.loads(request.body)
        total = data['total']
        segments = data['segments']
        ref_job = data['ref_job']
        t_id = data['t_id']
        if db.jobs.count({"_id": ObjectId(ref_job)}) == 0:
            return basic_failure

        # Step 1: Setup limits
        slen = len(segments)
        sub_size = int(total) // slen

        limits = [
            [sub_size*i, sub_size*(i+1)] for i in range(0, slen)
            ]
        limits[-1][1] = total

        # Step 2, create db jobs for each segment
        result = []
        sheet_rows = []
        timestamp = datetime.now()
        for i, segment in enumerate(segments):
            date = segment['date']
            res = db.segment_jobs.insert_one({
                "ref_job": ObjectId(ref_job),
                "timestamp": timestamp,
                "segment_number": i+1,
                "limits": {
                    "lower": limits[i][0],
                    "upper": limits[i][1]
                },
                "text": {
                    "english": segment['english'],
                    "arabic": segment['arabic']
                },
                "date": date,
                "job": {
                    "status": "pending"
                }
            })
            oid_col = str(res.inserted_id) + ("_segment,%i,%i,%i" % (t_id, limits[i][0], limits[i][1]))     # Added _segment
            result.append(oid_col)

            # Creating the row
            date = datetime.fromtimestamp(date / 1000)
            start_date = date.strftime("%m/%d/%Y")
            hour = date.hour
            minute = date.minute

            row = ['Once', 'segment', start_date, '', hour, minute, segment['english'], segment['arabic'], oid_col]
            sheet_rows.append(row)

        if data.get('debug', False):
            return jsonResponse({"success": True, "result": sheet_rows})
        else:
            for row in sheet_rows:
                append_to_sheet(row)
            return basic_success
    except Exception, e:
        return basic_error(e)
def dummy_block_list_csv(request):
    if request.method == 'POST' and 'file' in request.FILES:
        reader = csv.reader(request.FILES['file'])
        return jsonResponse({"success": True, "data": list(reader)[1:]})
    else:
        return jsonResponse({"success": False, "error": "No file"})
Beispiel #30
0
def test_insert(request):
    append_row_to_content(['Testing', 'Row'])
    return jsonResponse({"success": True})
def dummy_block_list_csv(request):
    if request.method == 'POST' and 'file' in request.FILES:
        reader = csv.reader(request.FILES['file'])
        return jsonResponse({"success": True, "data": list(reader)[1:]})
    else:
        return jsonResponse({"success": False, "error": "No file"})
Beispiel #32
0
def get_form(request):
    data = list(db.jabong_form.find({}, {"_id": False}))
    return jsonResponse(data)
Beispiel #33
0
def post_segment_form(request):
    try:
        data = json.loads(request.body)
        total = data['total']
        segments = data['segments']
        ref_job = data['ref_job']
        t_id = data['t_id']
        if db.jobs.count({"_id": ObjectId(ref_job)}) == 0:
            return basic_failure

        # Step 1: Setup limits
        slen = len(segments)
        sub_size = int(total) // slen

        limits = [[sub_size * i, sub_size * (i + 1)] for i in range(0, slen)]
        limits[-1][1] = total

        # Step 2, create db jobs for each segment
        result = []
        sheet_rows = []
        timestamp = datetime.now()
        for i, segment in enumerate(segments):
            date = segment['date']
            res = db.segment_jobs.insert_one({
                "ref_job": ObjectId(ref_job),
                "timestamp": timestamp,
                "segment_number": i + 1,
                "limits": {
                    "lower": limits[i][0],
                    "upper": limits[i][1]
                },
                "text": {
                    "english": segment['english'],
                    "arabic": segment['arabic']
                },
                "date": date,
                "job": {
                    "status": "pending"
                }
            })
            oid_col = str(res.inserted_id) + (
                "_segment,%i,%i,%i" %
                (t_id, limits[i][0], limits[i][1]))  # Added _segment
            result.append(oid_col)

            # Creating the row
            date = datetime.fromtimestamp(date / 1000)
            start_date = date.strftime("%m/%d/%Y")
            hour = date.hour
            minute = date.minute

            row = [
                'Once', 'segment', start_date, '', hour, minute,
                segment['english'], segment['arabic'], oid_col
            ]
            sheet_rows.append(row)

        if data.get('debug', False):
            return jsonResponse({"success": True, "result": sheet_rows})
        else:
            for row in sheet_rows:
                append_to_sheet(row)
            return basic_success
    except Exception, e:
        return basic_error(e)
def get_pipeline(request):
    """
    Get the pipeline and options for the wadi system
    Refer docs/jobs_format.json

    Currently supports both the new and the old api. Future releases will deprecate the old
    pipeline method
    """
    id = request.GET['id']
    obj = db.jobs.find_one({"_id": ObjectId(id)})
    if obj:
        options = obj['target_config']
        if not options or not isinstance(options[options.keys()[0]], dict):
            new_api = False
        else:
            new_api = True
            complete = options.copy()
            options = dict(
                map(lambda kv: (kv[0], kv[1]['value']), options.items()))

        # Customisation ----------------- #
        if 'customer' not in options:
            options['mode'] = 'all'
        else:
            cust = options.pop('customer')
            if isinstance(cust, list):
                if len(cust) == 2:
                    options['mode'] = 'all'
                else:
                    options['mode'] = cust[0].lower()
            else:
                if cust.lower() in ['both', 'all']:
                    options['mode'] = 'all'
                else:
                    options['mode'] = cust.lower()

        if 'language' in options and options['language'].lower() in [
                'both', 'all'
        ]:
            options.pop('language')

        if 'purchase_month' in options:
            options['purchase_month'] = [
                monthDict[a] for a in options['purchase_month']
            ]

        if 'channel' in options:
            options['channel'] = map(lambda k: lasttouch_dict[k],
                                     options['channel'])
        # ------------------------------- #

        if new_api:
            pipeline = {'required': [], 'additional': []}
            # complete.pop('customer', '')
            for k, v in complete.items():
                if k in options:
                    pipeline[v['co_type']].append(k)
            pipeline['required'].append('customer')

        else:
            pipeline = [k for k, v in options.items() if k != 'mode']
            pipeline.append('customer')

        return jsonResponse({"pipeline": pipeline, "options": options})
    else:
        raise Http404
Beispiel #35
0
def get_form_data(request):
    """
    Get the form
    """
    data = db.form.find({"enabled": True}, {"_id": False, "regex": False, "enabled": False})
    return jsonResponse(data)
Beispiel #36
0
def get_sample_form_data(request):
    """ Just for testing """
    data = db.form.find({"operation": {"$in": ["item_status", "payment_method", "repeat_buyer"]}},
                        {"_id": False, "regex": False})
    return jsonResponse(data)
def get_form_data(request):
    """
    Get the form
    """
    data = db.form.find({}, {"_id": False, "regex": False})
    return jsonResponse(data)
def get_pipeline(request):
    """
    Get the pipeline and options for the wadi system
    Refer docs/jobs_format.json

    Currently supports both the new and the old api. Future releases will deprecate the old
    pipeline method
    """
    id = request.GET['id']
    obj = db.jobs.find_one({"_id": ObjectId(id)})
    if obj:
        options = obj['target_config']
        if not options or not isinstance(options[options.keys()[0]], dict):
            new_api = False
        else:
            new_api = True
            complete = options.copy()
            options = dict(map(
                lambda kv: (kv[0], kv[1]['value']),
                options.items()
            ))

        # Customisation ----------------- #
        if 'customer' not in options:
            options['mode'] = 'all'
        else:
            cust = options.pop('customer')
            if isinstance(cust, list):
                if len(cust) == 2:
                    options['mode'] = 'all'
                else:
                    options['mode'] = cust[0].lower()
            else:
                if cust.lower() in ['both', 'all']:
                    options['mode'] = 'all'
                else:
                    options['mode'] = cust.lower()

        if 'language' in options and options['language'].lower() in ['both', 'all']:
            options.pop('language')

        if 'purchase_month' in options:
            options['purchase_month'] = [monthDict[a] for a in options['purchase_month']]

        if 'channel' in options:
            options['channel'] = map(lambda k: lasttouch_dict[k], options['channel'])
        # ------------------------------- #

        if new_api:
            pipeline = {
                'required': [],
                'additional': []
            }
            # complete.pop('customer', '')
            for k, v in complete.items():
                if k in options:
                    pipeline[v['co_type']].append(k)
            pipeline['required'].append('customer')

        else:
            pipeline = [k for k, v in options.items() if k != 'mode']
            pipeline.append('customer')

        return jsonResponse({"pipeline": pipeline, "options": options})
    else:
        raise Http404