Example #1
0
def edit_host(request, host_id, template='config/edit_host.html'):
    "Edists Mail host"

    host = get_object_or_404(MailHost, id=host_id)

    if request.method == 'POST':
        form = EditMailHost(request.POST, instance=host)
        if form.is_valid():
            try:
                form.save()
                msg = _('Delivery SMTP server: %(server)s has '
                        'been updated successfully') % {'server': host.address}
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse('view-domain', 
                    args=[host.useraddress.id]))
            except IntegrityError:
                msg = _('Delivery SMTP server: %(server)s update failed') % {
                'server': host.address}
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
    else:
        form = EditMailHost(instance=host)
    return render_to_response(template, locals(), 
        context_instance=RequestContext(request))
Example #2
0
def delete_from_list(request, item_id):
    "delete filter"
    list_item = get_object_or_404(List, pk=item_id)
    if request.method == 'POST':
        form = ListDeleteForm(request.POST)
        if form.is_valid():
            if not list_item.can_access(request):
                return HttpResponseForbidden(_('You do not have authorization'))
            list_type = list_item.list_type
            list_item.delete()
            if request.is_ajax():
                response = anyjson.dumps({'success': True})
                return HttpResponse(response, 
                    content_type='application/javascript; charset=utf-8')
            return HttpResponseRedirect(reverse('lists-start', 
                args=[list_type]))
        else:
            if request.is_ajax():
                response = anyjson.dumps({'success': False})
                return HttpResponse(response, 
                    content_type='application/javascript; charset=utf-8')
    else:
        form = ListDeleteForm()
        form.fields['list_item'].widget.attrs['value'] = item_id
    return render_to_response('lists/delete.html', locals(), 
        context_instance=RequestContext(request))
Example #3
0
def delete_auth_host(request, host_id, template="config/delete_auth_host.html"):
    "Deletes an external auth host"
    host = get_object_or_404(MailAuthHost, id=host_id)

    if request.method == "POST":
        form = DeleteMailAuthHostForm(request.POST, instance=host)
        if form.is_valid():
            try:
                go_id = host.useraddress.id
                msg = _("External authentication %(auth)s: on host %(host)s" " for domain %(dom)s has been deleted") % {
                    "auth": AUTH_TYPES[host.protocol],
                    "host": host.address,
                    "dom": host.useraddress.address,
                }
                host.delete()
                if request.is_ajax():
                    response = anyjson.dumps({"success": True, "html": msg})
                    return HttpResponse(response, content_type="application/javascript; charset=utf-8")
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse("view-domain", args=[go_id]))
            except DatabaseError:
                msg = _(
                    "External authentication %(auth)s: on host %(host)s" " for domain %(dom)s could not be deleted"
                ) % {"auth": AUTH_TYPES[host.protocol], "host": host.address, "dom": host.useraddress.address}
                if request.is_ajax():
                    response = anyjson.dumps({"success": False, "html": msg})
                    return HttpResponse(response, content_type="application/javascript; charset=utf-8")
                djmessages.info(request, msg)
    else:
        form = DeleteMailAuthHostForm(instance=host)
    return render_to_response(template, locals(), context_instance=RequestContext(request))
Example #4
0
def add_host(request, domain_id, template='config/add_host.html'):
    "Adds Mail host"

    domain = get_object_or_404(UserAddresses, id=domain_id, address_type=1)

    if request.method == 'POST':
        form = MailHostForm(request.POST)
        if form.is_valid():
            try:
                host = form.save()
                msg = _('Delivery SMTP server: %(server)s was'
                        ' added successfully') % {'server': host.address}
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse('view-domain', 
                    args=[domain.id]))
            except IntegrityError:
                msg = _('Adding of Delivery SMTP server failed')
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
    else:
        form = MailHostForm(initial = {'useraddress': domain.id})
    return render_to_response(template, locals(), 
        context_instance=RequestContext(request))
Example #5
0
def weasyl_exception_processor():
    web.ctx.log_exc = web.ctx.env.get(
        'raven.captureException', lambda **kw: traceback.print_exc())
    try:
        return _handle()
    except ClientGoneAway:
        if 'raven.captureMessage' in web.ctx.env:
            web.ctx.env['raven.captureMessage']('HTTP client went away', level=logging.INFO)
        return ''
    except web.HTTPError:
        raise
    except Exception as e:
        userid = d.get_userid()
        errorpage_kwargs = {}
        if isinstance(e, WeasylError):
            if e.render_as_json:
                return json.dumps({'error': {'name': e.value}})
            errorpage_kwargs = e.errorpage_kwargs
            if e.value in errorcode.error_messages:
                web.ctx.status = errorcode.error_status_code.get(e.value, '200 OK')
                message = '%s %s' % (errorcode.error_messages[e.value], e.error_suffix)
                return d.errorpage(userid, message, **errorpage_kwargs)
        web.ctx.status = '500 Internal Server Error'
        request_id = None
        if 'raven.captureException' in web.ctx.env:
            request_id = base64.b64encode(os.urandom(6), '+-')
            event_id, = web.ctx.env['raven.captureException'](request_id=request_id)
            request_id = '%s-%s' % (event_id, request_id)
        print 'unhandled error (request id %s) in %r' % (request_id, web.ctx.env)
        traceback.print_exc()
        if getattr(e, '__render_as_json', False):
            return json.dumps({'error': {}})
        return d.errorpage(userid, request_id=request_id, **errorpage_kwargs)
Example #6
0
def report(request, report_kind):
    "displays a report"
    report_kind = int(report_kind)
    report_title = REPORT_DICT[report_kind]["title"]
    template = "web/reports/piereport.html"
    active_filters = []
    data = run_query(report_kind, request, active_filters)
    pie_data = pack_json_data(data, report_kind)
    filter_form = FilterForm()

    if request.is_ajax():
        response = anyjson.dumps({"items": list(data), "pie_data": pie_data})
        return HttpResponse(response, content_type="application/javascript; charset=utf-8")
    else:
        pie_data = anyjson.dumps(pie_data)
        return render_to_response(
            template,
            {
                "pie_data": pie_data,
                "top_items": data,
                "report_title": report_title,
                "report_kind": report_kind,
                "active_filters": active_filters,
                "form": filter_form,
            },
            context_instance=RequestContext(request),
        )
Example #7
0
def test_status(request, taskid):
    "Gets the task status"
    status = default_backend.get_status(taskid)
    results = default_backend.get_result(taskid)
    if status in ['SUCCESS', 'FAILURE']:
        if status == 'SUCCESS':
            if results['smtp']:
                msg = _('The server is contactable and accepting messages')
            else:
                if results['ping']:
                    msg = (_('The server is contactable via ICMP but'
                    ' is not accepting messages from me, Errors: %s') %
                    results['errors']['smtp'])
                else:
                    msg = (_('The server is not accepting messages, Errors: %s')
                    % str(results['errors']))
        else:
            msg = _('The tests failed to run try again later')
        if request.is_ajax():
            response = anyjson.dumps(dict(html=msg, completed=True))
            return HttpResponse(response,
                content_type='application/javascript; charset=utf-8')
        djmessages.info(request, msg)
        return HttpResponseRedirect(reverse('view-domain',
            args=[results['host']]))
    else:
        if request.is_ajax():
            response = anyjson.dumps(dict(html='', completed=False))
            return HttpResponse(response,
                content_type='application/javascript; charset=utf-8')
    return render_to_response('config/task_status.html', {'status': status},
    context_instance=RequestContext(request))
Example #8
0
def add_auth_host(request, domain_id, template='config/add_auth_host.html'):
    'Add an external auth host'
    domain = get_object_or_404(UserAddresses, id=domain_id, address_type=1)
    if request.method == 'POST':
        form = MailAuthHostForm(request.POST)
        if form.is_valid():
            try:
                host = form.save()
                msg = _('External authentication %(auth)s: on host %(host)s'
                        ' for domain %(dom)s was added successfully') % {
                    'auth': AUTH_TYPES[host.protocol], 'host': host.address, 
                    'dom': host.useraddress.address}
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse('view-domain', 
                    args=[domain.id]))
            except IntegrityError:
                msg = _('Addition of external authentication failed')
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
    else:
        form = MailAuthHostForm(initial = {'useraddress': domain.id})
    return render_to_response(template, locals(), 
        context_instance=RequestContext(request))
Example #9
0
File: api.py Project: 0x15/weasyl
    def GET(self):
        form = web.input(count=0, backtime=0, nexttime=0)
        try:
            count = int(form.count)
            backtime = int(form.backtime)
            nexttime = int(form.nexttime)
        except ValueError:
            web.ctx.status = '422 Unprocessable Entity'
            return json.dumps(_ERROR_UNEXPECTED)
        else:
            count = min(count or 100, 100)

        submissions = message.select_submissions(
            self.user_id, count + 1, backtime=backtime, nexttime=nexttime)
        backtime, nexttime = d.paginate(submissions, backtime, nexttime, count, 'unixtime')

        ret = []
        for sub in submissions:
            tidy_submission(sub)
            ret.append(sub)

        return json.dumps({
            'backtime': backtime, 'nexttime': nexttime,
            'submissions': ret,
        })
Example #10
0
def find_tracts_with_users():
	with open('data/' + my.DATA_FOLDER + 'user_homes.json', 'rb') as fp:
		homes = anyjson.loads(fp.read())
	homes = dict( ( int(h[0]), ogr.CreateGeometryFromWkt('POINT(%s %s)' \
						% (h[1][1], h[1][0])) )  for h in homes.items() )

	with open('data/' + my.DATA_FOLDER + 'city_tracts.json', 'rb') as fp:
		polygons = anyjson.loads(fp.read())

	user_counts = dict( (gid, 0) for gid in polygons.keys())
	users_in_tracts = []
	tracts_with_users = []
	tract_users = dict( (gid, []) for gid in polygons.keys())

	for gid, json_poly in polygons.iteritems():
		json_poly_str = anyjson.dumps(json_poly)
		poly = ogr.CreateGeometryFromJson(json_poly_str)
		
		for user_id, h in homes.items():
			if h.Within(poly):
				user_counts[gid] += 1
				users_in_tracts.append(user_id)
				tract_users[gid].append(user_id)
				del homes[user_id]

	tracts_with_users = [gid for gid, v in user_counts.items() if v != 0]

	with open('data/' + my.DATA_FOLDER + 'user_counts.json', 'wb') as fp:
		fp.write( jsb.beautify( anyjson.dumps(user_counts) ) )
	with open('data/' + my.DATA_FOLDER + 'tract_users.json', 'wb') as fp:
		fp.write( jsb.beautify( anyjson.dumps(tract_users) ) )
	with open('data/' + my.DATA_FOLDER + 'users_in_tracts.json', 'wb') as fp:
		fp.write( anyjson.dumps(users_in_tracts) )
	with open('data/' + my.DATA_FOLDER + 'tracts_with_users.json', 'wb') as fp:
		fp.write( anyjson.dumps(tracts_with_users) )
Example #11
0
def make_team_sets():
	''' '''
	vocab = []
	legend = {}
	sets = {} 
	with open('data/' + my.DATA_FOLDER + 'teams.txt', 'rb') as fp:
		cr = csv.reader(fp, delimiter=',')
		id = 0
		for row in cr:
			id += 1
			name = row.pop(0).strip().replace('.','')
			tw_id = row.pop().strip().replace('@','')
			htags = [h.strip().replace('#','') for h in row]
			print id, name, '\t', tw_id, htags
			legend[id] = name

			words = []
			words.append(tuple(n.lower() for n in name.split()))
			words.append(tw_id.lower())
			words.extend([h.lower() for h in htags])
			words = tuple(set(words))
			words = [[w] if type(w) != tuple else list(w) for w in words]
			print words, '\n'
			sets[id] = words

			vocab.extend(n.lower() for n in name.split())
			vocab.append(tw_id.lower())
			vocab.extend([h.lower() for h in htags])
	vocab = tuple(set(vocab))
	with open('data/' + my.DATA_FOLDER + 'vocab_teams.txt', 'wb') as fp:
		fp.write('\n'.join(vocab))
	with open('data/' + my.DATA_FOLDER + 'teams_legend.json', 'wb') as fp:
		fp.write(anyjson.dumps(legend))
	with open('data/' + my.DATA_FOLDER + 'team_sets.json', 'wb') as fp:
		fp.write(anyjson.dumps(sets))
Example #12
0
def edit_auth_host(request, host_id, template='config/edit_auth_host.html'):
    'Edits an external auth host'

    host = get_object_or_404(MailAuthHost, id=host_id)

    if request.method == 'POST':
        form = EditMailAuthHostForm(request.POST, instance=host)
        if form.is_valid():
            try:
                saved_host = form.save()
                msg = _('External authentication %(auth)s: on host %(host)s for'
                        ' domain %(dom)s has been updated successfully') % {
                    'auth': AUTH_TYPES[saved_host.protocol], 
                    'host': saved_host.address, 
                    'dom': saved_host.useraddress.address}
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse('view-domain', 
                    args=[saved_host.useraddress.id]))
            except IntegrityError:
                msg = _('Update of external authentication failed')
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
    else:
        form = EditMailAuthHostForm(instance=host)
    return render_to_response(template, locals(), 
        context_instance=RequestContext(request))
Example #13
0
def del_filter(request, index_num):
    "deletes a filter"
    try:
        filt = SavedFilter.objects.get(id=int(index_num))
    except SavedFilter.DoesNotExist:
        error_msg = _("This filter you attempted to delete does not exist")
        if request.is_ajax():
            response = anyjson.dumps(
                {"success": False, "data": [], "errors": error_msg, "active_filters": [], "saved_filters": []}
            )
            return HttpResponse(response, content_type="application/javascript; charset=utf-8")
        else:
            return HttpResponseRedirect(reverse("reports-index"))
    else:
        try:
            filt.delete()
        except DatabaseError:
            error_msg = _("Deletion of the filter failed, Try again")
            if request.is_ajax():
                response = anyjson.dumps(
                    {"success": False, "data": [], "errors": error_msg, "active_filters": [], "saved_filters": []}
                )
                return HttpResponse(response, content_type="application/javascript; charset=utf-8")
        if request.is_ajax():
            return index(request)
        else:
            return HttpResponseRedirect(reverse("reports-index"))
Example #14
0
def delete_account(request, user_id, template_name='accounts/delete_account.html'):
    """delete_account"""
    user_account = get_object_or_404(User, pk=user_id)
    if request.method == 'POST':
        form = DeleteUserForm(request.POST, instance=user_account)
        if form.is_valid():
            if user_account.id == request.user.id:
                return HttpResponseRedirect(reverse('user-profile',
                    args=[user_id]))
            else:
                try:
                    user_account.delete()
                    msg = _('The user account %(account)s has been deleted') % {
                        'account': user_account.username}
                    if request.is_ajax():
                        response = anyjson.dumps({'success': True, 'html': msg})
                        return HttpResponse(response,
                            content_type='application/javascript; charset=utf-8')
                    djmessages.info(request, msg)
                    return HttpResponseRedirect(reverse('accounts'))
                except DatabaseError:
                    msg = _('The deletion of user account %(account)s failed') % {
                    'account': user_account.username}
                    if request.is_ajax():
                        response = anyjson.dumps({'success': True, 'html': msg})
                        return HttpResponse(response,
                            content_type='application/javascript; charset=utf-8')
                    djmessages.info(request, msg)
                    return HttpResponseRedirect(reverse('user-profile',
                        args=[user_id]))
    else:
        form = DeleteUserForm(instance=user_account)
    return render_to_response(template_name, locals(),
        context_instance=RequestContext(request))
Example #15
0
def make_artificial_tweets():
	with open('data/' + my.DATA_FOLDER + 'user_homes.json', 'rb') as fp:
		user_homes = anyjson.loads(fp.read())
	path = 'data/' + my.DATA_FOLDER + 'data/'
	with open(path + 'artificial_indexes.json', 'rb') as fp:
		indexes = anyjson.loads(fp.read())

	with open('data/' + my.DATA_FOLDER + 'artificial/' + \
				'artificial_points.json', 'rb') as fp:
		artificial_points = anyjson.loads(fp.read())
	shuffle(artificial_points)

	tweets = {}
	for user_id, index in indexes.iteritems():
		tweets[user_id] = []
		home = user_homes[str(user_id)]
		home = ( round(home[0], 4), round(home[1], 4))
		for i in index:
			tweets[user_id].append((home[0] + artificial_points[i][0],
									home[1] + artificial_points[i][1]))
		print user_id, len(tweets[user_id])

	with open(path + 'artificial_tweets.json', 'wb') as fp:
		fp.write( anyjson.dumps(tweets) )

	tweets = list(itertools.chain(*tweets.values()))

	with open(path + 'artificial_all_tweets.json', 'wb') as fp:
		fp.write( anyjson.dumps(tweets) )
Example #16
0
    def test_client(self):

        cid = new_cid()
        headers = {'x-zato-cid':cid}
        ok = True
        status_code = rand_int()
        
        service_name = rand_string()
        service_response_name = '{}_response'.format(service_name)
        service_response_payload = {'service_id':5207, 'has_wsdl':True}
        service_response_dict = {'zato_service_has_wsdl_response':service_response_payload}
        service_response = dumps(service_response_dict).encode('base64')
        
        text = dumps({
            'zato_env':{'result':ZATO_OK, 'details':''},
            service_response_name: {
                'response':service_response
            }
        })
        
        client = self.get_client(FakeInnerResponse(headers, ok, text, status_code))
        response = client.invoke(service_name, '')
        
        eq_(response.ok, ok)
        eq_(response.inner.text, text)
        eq_(response.data.items(), service_response_payload.items())
        eq_(response.has_data, True)
        eq_(response.cid, cid)
Example #17
0
 def test_scheduled_task(self):
     task_dict = {
         'request': {'url': 'http://httpbin.org/get'},
         'schedule': '1234567',
         'cname': 'haha'
     }
     rv = self.client.post(
         '/apps/test/taskqueues/default/tasks',
         data=anyjson.dumps(task_dict))
     self.assertEqual(rv.status_code, 422)
     task_dict['schedule'] = 'every 30 second'
     task_dict.pop('cname')
     rv = self.client.post(
         '/apps/test/taskqueues/default/tasks',
         data=anyjson.dumps(task_dict))
     self.assertEqual(rv.status_code, 422)
     task_dict['cname'] = 'test schedule'
     rv = self.client.post(
         '/apps/test/taskqueues/default/tasks',
         data=anyjson.dumps(task_dict))
     self.assertEqual(rv.status_code, 201)
     task = anyjson.loads(rv.data)
     self.assertEqual(task['id'], 1)
     self.assertEqual(task['schedule'], 'every 30.0 seconds')
     task_dict['schedule'] = '*/1 1-5,8 * * *'
     task_dict['cname'] = 'test crontab'
     rv = self.client.post(
         '/apps/test/taskqueues/default/tasks',
         data=anyjson.dumps(task_dict))
     self.assertEqual(rv.status_code, 201)
     task = anyjson.loads(rv.data)
     self.assertEqual(task['id'], 2)
     self.assertEqual(task['schedule'], '*/1 1-5,8 * * *')
Example #18
0
    def test_do_restore_message(self):
        client = Mock(name='client')
        pl1 = {'body': 'BODY'}
        spl1 = dumps(pl1)
        lookup = self.channel._lookup = Mock(name='_lookup')
        lookup.return_value = ['george', 'elaine']
        self.channel._do_restore_message(
            pl1, 'ex', 'rkey', client,
        )
        client.rpush.assert_has_calls([
            call('george', spl1), call('elaine', spl1),
        ])

        pl2 = {'body': 'BODY2', 'headers': {'x-funny': 1}}
        headers_after = dict(pl2['headers'], redelivered=True)
        spl2 = dumps(dict(pl2, headers=headers_after))
        self.channel._do_restore_message(
            pl2, 'ex', 'rkey', client,
        )
        client.rpush.assert_has_calls([
            call('george', spl2), call('elaine', spl2),
        ])

        client.rpush.side_effect = KeyError()
        with patch('kombu.transport.redis.crit') as crit:
            self.channel._do_restore_message(
                pl2, 'ex', 'rkey', client,
            )
            self.assertTrue(crit.called)
Example #19
0
    def update(self, nodes, links=None):
        """
        Update json data of nodes and links.
        """

        # Add meta to visible nodes
        for index in range(len(nodes)):
            nodes[index]['meta'] = {'visible': 1}

        # Add hidden nodes
        nodes = sorted(nodes, key=lambda node: node['id'])
        nodes_hidden = self.nodes('hidden', meta=True)
        nodes += nodes_hidden

        # Recounting ID
        for index in range(len(nodes)):
            nodes[index]['id'] = index+1

        self.nodes_json = json.dumps(nodes)

        if links:
            # Add hidden links
            links_hidden = self.links('hidden')
            links += links_hidden

            # Filter links
            nodes_uid = [node['uid'] for node in nodes]
            links = filter(lambda link: (link['source'] in nodes_uid and link['target'] in nodes_uid), links)

            for idx, val in enumerate(links):
                links[idx]['weight'] = float(val['weight'])
                links[idx]['delay'] = float(val['delay'])

            self.links_json = json.dumps(links)
Example #20
0
 def test_insert_task(self):
     with self.app.app_context():
         tq = apis.TaskQueue('test')
         tq.add_task({'method': 'GET',
                      'url': 'http://httpbin.org/get'},
                     cname='testtask')
     task_dict = {
         'request': {'url': 'http://httpbin.org/get'},
         'cname': 'testtask',
         'eta': '10:42'
     }
     rv = self.client.post(
         '/apps/test/taskqueues/default/tasks',
         data=anyjson.dumps(task_dict))
     self.assertEqual(rv.status_code, 409)
     self.assertEqual(anyjson.loads(rv.data)['error_code'], 207203)
     task_dict['cname'] = 'testtask1'
     rv = self.client.post(
         '/apps/test/taskqueues/default/tasks',
         data=anyjson.dumps(task_dict))
     self.assertEqual(rv.status_code, 201)
     task = anyjson.loads(rv.data)
     self.assertEqual(task['id'], 2)
     self.assertEqual(task['request']['method'], 'GET')
     self.assertEqual(task['cname'], 'testtask1')
     now = datetime.now()
     eta_expect = utc.normalize(
         get_localzone().localize(
             datetime(now.year, now.month, now.day, 10, 42)
         )
     ).isoformat()
     self.assertEqual(task['eta'], eta_expect)
     self.assertTrue(isinstance(task['countdown'], float))
Example #21
0
def add_auth_host(request, domain_id, template="config/add_auth_host.html"):
    "Add an external auth host"
    domain = get_object_or_404(UserAddresses, id=domain_id, address_type=1)
    if request.method == "POST":
        form = MailAuthHostForm(request.POST)
        if form.is_valid():
            try:
                host = form.save()
                msg = _(
                    "External authentication %(auth)s: on host %(host)s" " for domain %(dom)s was added successfully"
                ) % {"auth": AUTH_TYPES[host.protocol], "host": host.address, "dom": host.useraddress.address}
                if request.is_ajax():
                    response = anyjson.dumps({"success": True, "html": msg})
                    return HttpResponse(response, content_type="application/javascript; charset=utf-8")
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse("view-domain", args=[domain.id]))
            except IntegrityError:
                msg = _("Addition of external authentication failed")
                if request.is_ajax():
                    response = anyjson.dumps({"success": True, "html": msg})
                    return HttpResponse(response, content_type="application/javascript; charset=utf-8")
                djmessages.info(request, msg)
    else:
        form = MailAuthHostForm(initial={"useraddress": domain.id})
    return render_to_response(template, locals(), context_instance=RequestContext(request))
Example #22
0
def edit_auth_host(request, host_id, template="config/edit_auth_host.html"):
    "Edits an external auth host"

    host = get_object_or_404(MailAuthHost, id=host_id)

    if request.method == "POST":
        form = EditMailAuthHostForm(request.POST, instance=host)
        if form.is_valid():
            try:
                saved_host = form.save()
                msg = _(
                    "External authentication %(auth)s: on host %(host)s for"
                    " domain %(dom)s has been updated successfully"
                ) % {
                    "auth": AUTH_TYPES[saved_host.protocol],
                    "host": saved_host.address,
                    "dom": saved_host.useraddress.address,
                }
                if request.is_ajax():
                    response = anyjson.dumps({"success": True, "html": msg})
                    return HttpResponse(response, content_type="application/javascript; charset=utf-8")
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse("view-domain", args=[saved_host.useraddress.id]))
            except IntegrityError:
                msg = _("Update of external authentication failed")
                if request.is_ajax():
                    response = anyjson.dumps({"success": True, "html": msg})
                    return HttpResponse(response, content_type="application/javascript; charset=utf-8")
                djmessages.info(request, msg)
    else:
        form = EditMailAuthHostForm(instance=host)
    return render_to_response(template, locals(), context_instance=RequestContext(request))
Example #23
0
    def render_GET(self, request):
        try:
            # log.msg("Handle request: %s" % request.path, logLevel=logging.DEBUG)
            request.setHeader("Content-Type", "application/json")

            if request.path == "/add/":
                params = json.loads(request.args["data"][0])
                response = self.__add_scrapers__(params)
                return json.dumps(response)

            elif request.path == "/list/":
                response = self.__list_scrapers__()
                return json.dumps(response)

            elif request.path == "/remove/":
                params = json.loads(request.args["data"][0])
                response = self.__remove_scrapers__(params)
                return json.dumps(response)
            elif request.path == "/ping/":
                return "pong"
            elif request.path == "/log/":
                logfile = open("log/daily-log.log")
                log_message = logfile.read()
                logfile.close()
                return log_message
            else:
                # log.msg("Wrong API path '%s'" % request.path,logLevel=logging.DEBUG)
                return json.dumps({"error": True, "message": "Wrong API path '%s'" % request.path})

        except Exception:
            # log.msg("Error: %s" % traceback.format_exc(),logLevel=logging.WARNING)
            return json.dumps({"error": True, "message": traceback.format_exc()})
Example #24
0
def test_status(request, taskid):
    "Gets the task status"
    status = default_backend.get_status(taskid)
    results = default_backend.get_result(taskid)
    if status in ["SUCCESS", "FAILURE"]:
        if status == "SUCCESS":
            if results["smtp"]:
                msg = _("The server is contactable and accepting messages")
            else:
                if results["ping"]:
                    msg = (
                        _("The server is contactable via ICMP but" " is not accepting messages from me, Errors: %s")
                        % results["errors"]["smtp"]
                    )
                else:
                    msg = _("The server is not accepting messages, Errors: %s") % str(results["errors"])
        else:
            msg = _("The tests failed to run try again later")
        if request.is_ajax():
            response = anyjson.dumps(dict(html=msg, completed=True))
            return HttpResponse(response, content_type="application/javascript; charset=utf-8")
        djmessages.info(request, msg)
        return HttpResponseRedirect(reverse("view-domain", args=[results["host"]]))
    else:
        if request.is_ajax():
            response = anyjson.dumps(dict(html="", completed=False))
            return HttpResponse(response, content_type="application/javascript; charset=utf-8")
    return render_to_response("config/task_status.html", {"status": status}, context_instance=RequestContext(request))
Example #25
0
def del_filter(request, index_num):
    "deletes a filter"
    try:
        filt = SavedFilter.objects.get(id=int(index_num))
    except SavedFilter.DoesNotExist:
        error_msg = _('This filter you attempted to delete does not exist')
        if request.is_ajax():
            response = anyjson.dumps({'success': False,
                'data': [], 'errors': error_msg, 'active_filters': [],
                'saved_filters': []})
            return HttpResponse(response,
                content_type='application/javascript; charset=utf-8')
        else:
            return HttpResponseRedirect(reverse('reports-index'))
    else:
        try:
            filt.delete()
        except DatabaseError:
            error_msg = _('Deletion of the filter failed, Try again')
            if request.is_ajax():
                response = anyjson.dumps({'success': False, 'data': [],
                    'errors': error_msg, 'active_filters': [],
                    'saved_filters': []})
                return HttpResponse(response,
                    content_type='application/javascript; charset=utf-8')
        if request.is_ajax():
            return index(request)
        else:
            return HttpResponseRedirect(reverse('reports-index'))
Example #26
0
def parse_kml():
	geo_data = {}
	loc_data = []
	kml = 'data/' + my.DATA_FOLDER + 'kml/' + 'LosAngelesCountyGangs.kml'
	tree = ElementTree.parse(kml)
	items = tree.findall('.//{http://earth.google.com/kml/2.2}Placemark')
	id = 0
	for item in items:
		id += 1
		name = item.find('.//{http://earth.google.com/kml/2.2}name').text
		cord = item.find(
				'.//{http://earth.google.com/kml/2.2}coordinates').text
		cord = cord.strip().split('\n')
		pol = []
		for ll in cord:
			ll = ll.split(',')
			lng = float(ll[0])
			lat = float(ll[1])
			pol.append([lat, lng])
		
		geo = {
			"type" : "Polygon",
			"coordinates" : [pol]
			}
		geo_data[id] = geo
		loc = {
			"id" : id,
			"name" : name,
			"polygon" : pol,
			}
		loc_data.append(loc)
	with open('data/' + my.DATA_FOLDER + 'geo_data.json', 'wb') as fp:
		fp.write(anyjson.dumps(geo_data))
	with open('data/' + my.DATA_FOLDER + 'loc_data.json', 'wb') as fp:
		fp.write(anyjson.dumps(loc_data))
Example #27
0
def delete_host(request, host_id, template='config/delete_host.html'):
    'Deletes Mail host'

    host = get_object_or_404(MailHost, id=host_id)

    if request.method == 'POST':
        form = DeleteMailHost(request.POST, instance=host)
        if form.is_valid():
            try:
                go_id = host.useraddress.id
                msg = _('Delivery SMTP server: %(server)s has been deleted') % {
                'server': host.address}
                host.delete()
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse('view-domain', 
                                            args=[go_id]))
            except DatabaseError:
                msg = _('Delivery SMTP server: %(server)s could not be deleted') % {
                'server': host.address}
                if request.is_ajax():
                    response = anyjson.dumps({'success': False, 'html': msg})
                    return HttpResponse(response, 
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
    else:
        form = DeleteMailHost(instance=host)
    return render_to_response(template, locals(), 
        context_instance=RequestContext(request))
Example #28
0
def delete_from_list(request, item_id):
    "delete filter"
    list_item = get_object_or_404(List, pk=item_id)
    if request.method == "POST":
        form = ListDeleteForm(request.POST)
        if form.is_valid():
            if not list_item.can_access(request):
                return HttpResponseForbidden(_("You do not have authorization"))
            list_type = list_item.list_type
            list_item.delete()
            if request.is_ajax():
                response = anyjson.dumps({"success": True})
                return HttpResponse(response, content_type="application/javascript; charset=utf-8")
            msg = _("List item deleted")
            djmessages.info(request, msg)
            return HttpResponseRedirect(reverse("lists-start", args=[list_type]))
        else:
            if request.is_ajax():
                response = anyjson.dumps({"success": False})
                return HttpResponse(response, content_type="application/javascript; charset=utf-8")
            msg = _("List item could not be deleted")
            djmessages.info(request, msg)
    else:
        form = ListDeleteForm()
        form.fields["list_item"].widget.attrs["value"] = item_id
    return render_to_response("lists/delete.html", locals(), context_instance=RequestContext(request))
Example #29
0
def make_picks_sets():
	''''''
	legend = {}
	sets = {} 
	with open('data/' + my.DATA_FOLDER + 'nba_picks.txt', 'rb') as fp:
		cr = csv.reader(fp, delimiter=',')
		pick_players = [row[0] for row in cr]
	with open('data/' + my.DATA_FOLDER + 'player_legend.json', 'rb') as fp:
		player_legend = anyjson.loads(fp.read()).items()
	with open('data/' + my.DATA_FOLDER + 'player_sets.json', 'rb') as fp:
		player_sets = anyjson.loads(fp.read())

	id = 0
	for name in pick_players:
		id += 1
		screen_name = None
		pid = 0
		for id2, n2 in player_legend:
			if _match_names(name, n2):
				player_legend.remove((id2, n2))
				screen_name = n2
				pid = id2
		#print name, '\t', screen_name, pid
		legend[id] = screen_name
		sets[id] = player_sets[pid]

	with open('data/' + my.DATA_FOLDER + 'picks_legend.json', 'wb') as fp:
		fp.write(anyjson.dumps(legend))
	with open('data/' + my.DATA_FOLDER + 'picks_sets.json', 'wb') as fp:
		fp.write(anyjson.dumps(sets))
Example #30
0
    def test_send_email_message_reply(self, get_message_info_mock, send_email_message_mock, get_label_info_mock):
        """
        Test the GmailManager on replying on an email message.
        """

        message_id = '15b3e2894fa3648d'
        thread_id = '15a6008a4baa65f3'

        # Mock the responses of the API calls.
        with open('lily/messaging/email/tests/data/send_email_message_reply.json') as infile:
            json_obj = json.load(infile)
            send_email_message_mock.return_value = json_obj

        with open('lily/messaging/email/tests/data/get_message_info_{0}.json'.format(message_id)) as infile:
            json_obj = json.load(infile)
            get_message_info_mock.return_value = json_obj

        with open('lily/messaging/email/tests/data/get_label_info_SENT.json') as infile:
            json_obj = json.load(infile)
            get_label_info_mock.return_value = json_obj

        email_account = EmailAccount.objects.first()
        manager = GmailManager(email_account)

        # Prepare an email message that will be send out.
        email_outbox_message = EmailOutboxMessage.objects.create(
            subject="Mauris ex tortor, hendrerit non sem eu, mollis varius purus.",
            send_from=email_account,
            to=anyjson.dumps("*****@*****.**"),
            cc=anyjson.dumps(None),
            bcc=anyjson.dumps(None),
            body="<html><body>Maecenas metus turpis, eleifend at dignissim ac, feugiat vel erat. Aenean at urna "
                 "convallis, egestas massa sed, rhoncus est.<br><br>Firstname Lastname ([email protected]) wrote on "
                 "22 March 2017 13:14:<hr><div dir=\"ltr\">Aliquam eleifend pharetra ligula, id feugiat ipsum laoreet "
                 "a. Aenean sed volutpat magna, ut viverra turpis. Morbi suscipit, urna in pellentesque venenatis, "
                 "mauris elit placerat justo, sit amet vestibulum purus dui id massa. In vitae libero et nunc "
                 "facilisis imperdiet. Sed pharetra aliquet luctus.</div></body></html>",
            headers={},
            mapped_attachments=0,
            template_attachment_ids='',
            original_message_id=None,
            tenant=self.user_obj.tenant
        )

        # Send the email message, it will be a reply because a thead id is passed.
        manager.send_email_message(email_outbox_message.message(), thread_id=thread_id)

        # Verify that is stored in the database as an email message.
        self.assertTrue(EmailMessage.objects.filter(account=email_account, message_id=message_id).exists(),
                        "Send reply message missing from the database.")

        # Verify that the email message has the correct labels.
        email_message = EmailMessage.objects.get(account=email_account, message_id=message_id)
        email_message_labels = set(email_message.labels.all().values_list('label_id', flat=True))
        self.assertEqual(email_message_labels, set([settings.GMAIL_LABEL_SENT]), "Send message mssing the SEND label.")

        # Verify that the email emssage has the correct thread id.
        self.assertEqual(email_message.thread_id, thread_id,
                         "Message {0} should have thread_id {1}.".format(email_message.message_id, thread_id))
Example #31
0
    def get(self, request, *args, **kwargs):
        number = kwargs.get('number', None)

        if number:
            # For now we'll always convert the phone number to a certain format.
            # In the future we might change how we handle phone numbers.
            number = parse_phone_number(number)

        results = self._search_number(number)

        return HttpResponse(anyjson.dumps(results),
                            content_type='application/json; charset=utf-8')
Example #32
0
def _edit_create_response(id, verb, transport, connection, name):

    return_data = {'id': id,
                   'transport': transport,
                   'message': 'Successfully {0} the {1} {2} [{3}]'.format(
                       verb,
                       TRANSPORT[transport],
                       CONNECTION[connection],
                       name),
                }

    return HttpResponse(dumps(return_data), mimetype='application/javascript')
Example #33
0
def slow_response_details(req, cid, service_name):

    item = None
    service = _get_service(req, service_name)
    pretty_print = asbool(req.GET.get('pretty_print'))

    input_dict = {
        'cid': cid,
        'name': service_name,
    }
    response = req.zato.client.invoke('zato.service.slow-response.get',
                                      input_dict)

    if response.has_data:
        cid = response.data.cid
        if cid != ZATO_NONE:
            item = SlowResponse()
            item.cid = response.data.cid
            item.req_ts = from_utc_to_user(response.data.req_ts + '+00:00',
                                           req.zato.user_profile)
            item.resp_ts = from_utc_to_user(response.data.resp_ts + '+00:00',
                                            req.zato.user_profile)
            item.proc_time = response.data.proc_time
            item.service_name = service_name
            item.threshold = service.slow_threshold

            for name in ('req', 'resp'):
                value = getattr(response.data, name)
                if value:
                    if isinstance(value, dict):
                        value = dumps(value)
                        data_format = 'json'
                    else:
                        data_format = known_data_format(value)

                    if data_format:
                        if pretty_print:
                            value = get_pretty_print(value, data_format)
                        attr_name = name + '_html'
                        setattr(
                            item, attr_name,
                            highlight(value, data_format_lexer[data_format](),
                                      HtmlFormatter(linenos='table')))

    return_data = {
        'cluster_id': req.zato.cluster_id,
        'service': service,
        'item': item,
        'pretty_print': not pretty_print,
    }

    return TemplateResponse(req, 'zato/service/slow-response-details.html',
                            return_data)
Example #34
0
File: amqp_.py Project: znavy/zato
def _edit_create_response(client, verb, id, name, def_id, cluster_id):
    response = client.invoke('zato.definition.amqp.get-by-id', {
        'id': def_id,
        'cluster_id': cluster_id
    })
    return_data = {
        'id': id,
        'message': 'Successfully {} the AMQP channel `{}`'.format(verb, name),
        'def_name': response.data.name
    }
    return HttpResponse(dumps(return_data),
                        content_type='application/javascript')
Example #35
0
    def invoke_async(self, name, payload='', channel=CHANNEL.INVOKE_ASYNC, data_format=DATA_FORMAT.DICT,
                     transport=None, expiration=BROKER.DEFAULT_EXPIRATION, to_json_string=False, cid=None, callback=None,
                     zato_ctx={}, environ={}):
        """ Invokes a service asynchronously by its name.
        """
        if self.component_enabled_target_matcher:
            name, target = self.extract_target(name)
            zato_ctx['zato.request_ctx.target'] = target

        # Let's first find out if the service can be invoked at all
        impl_name = self.server.service_store.name_to_impl_name[name]

        if self.component_enabled_invoke_matcher:
            if not self._worker_store.invoke_matcher.is_allowed(impl_name):
                raise ZatoException(self.cid, 'Service `{}` (impl_name) cannot be invoked'.format(impl_name))

        if to_json_string:
            payload = dumps(payload)

        cid = cid or new_cid()

        # If there is any callback at all, we need to figure out its name because that's how it will be invoked by.
        if callback:

            # The same service
            if callback is self:
                callback = self.name

        else:
            sink = '{}-async-callback'.format(self.name)
            if sink in self.server.service_store.name_to_impl_name:
                callback = sink

            # Otherwise the callback must be a string pointing to the actual service to reply to so we don't need to do anything.

        msg = {}
        msg['action'] = SERVICE.PUBLISH.value
        msg['service'] = name
        msg['payload'] = payload
        msg['cid'] = cid
        msg['channel'] = channel
        msg['data_format'] = data_format
        msg['transport'] = transport
        msg['is_async'] = True
        msg['callback'] = callback
        msg['zato_ctx'] = zato_ctx
        msg['environ'] = environ

        # If we have a target we need to invoke all the servers
        # and these which are not able to handle the target will drop the message.
        (self.broker_client.publish if target else self.broker_client.invoke_async)(msg, expiration=expiration)

        return cid
Example #36
0
    def test_run(self, send_task):
        a = apply(app=self.app, stderr=WhateverIO(), stdout=WhateverIO())
        a.run("tasks.add")
        self.assertTrue(send_task.called)

        a.run("tasks.add",
              args=dumps([4, 4]),
              kwargs=dumps({"x": 2, "y": 2}))
        self.assertEqual(send_task.call_args[1]["args"], [4, 4])
        self.assertEqual(send_task.call_args[1]["kwargs"], {"x": 2, "y": 2})

        a.run("tasks.add", expires=10, countdown=10)
        self.assertEqual(send_task.call_args[1]["expires"], 10)
        self.assertEqual(send_task.call_args[1]["countdown"], 10)

        now = datetime.now()
        iso = now.isoformat()
        a.run("tasks.add", expires=iso)
        self.assertEqual(send_task.call_args[1]["expires"], now)
        with self.assertRaises(ValueError):
            a.run("tasks.add", expires="foobaribazibar")
Example #37
0
 def post_study(self, nexson, study_id=None, commit_msg=None):
     assert nexson is not None
     if study_id is None:
         uri = '{d}/study'.format(d=self._prefix)
     else:
         uri = '{d}/study/{i}'.format(d=self._prefix, i=study_id)
     params = {'auth_token': self.auth_token}
     if commit_msg:
         params['commit_msg'] = commit_msg
     return self.json_http_post(uri,
                                params=params,
                                data=anyjson.dumps({'nexson': nexson}))
Example #38
0
 def _do_query(self, url, query_dict, exact, verbose, valid_keys, kwargs):
     data = self._prepare_query_data(query_dict=query_dict,
                                     exact=exact,
                                     verbose=verbose,
                                     valid_keys=valid_keys,
                                     kwargs=kwargs)
     response = self.json_http_post(url, data=anyjson.dumps(data))
     if 'error' in response:
         raise RuntimeError('Error reported by oti "{}"'.format(
             response['error']))
     assert len(response) == 1
     return response['matched_studies']
Example #39
0
    def test_run(self, send_task):
        a = call(app=self.app, stderr=WhateverIO(), stdout=WhateverIO())
        a.run('tasks.add')
        self.assertTrue(send_task.called)

        a.run('tasks.add',
              args=dumps([4, 4]),
              kwargs=dumps({'x': 2, 'y': 2}))
        self.assertEqual(send_task.call_args[1]['args'], [4, 4])
        self.assertEqual(send_task.call_args[1]['kwargs'], {'x': 2, 'y': 2})

        a.run('tasks.add', expires=10, countdown=10)
        self.assertEqual(send_task.call_args[1]['expires'], 10)
        self.assertEqual(send_task.call_args[1]['countdown'], 10)

        now = datetime.now()
        iso = now.isoformat()
        a.run('tasks.add', expires=iso)
        self.assertEqual(send_task.call_args[1]['expires'], now)
        with self.assertRaises(ValueError):
            a.run('tasks.add', expires='foobaribazibar')
Example #40
0
    def create_PeriodicTask(self):
        """
        创建定时任务

            在 PeriodicTask表里创建新的定时任务,并在表的args字段里保存批处理作业表的batch_job_id。这样,
            就可以把批处理作业和定时调度关联起来。
        :return:
        """
        obj = PeriodicTask.objects.create(**self.schedule_dict)
        obj.args = dumps([self.batch_job_id])
        obj.save()
        return obj
Example #41
0
 def _do_restore_message(self, payload, exchange, routing_key):
     try:
         try:
             payload['headers']['redelivered'] = True
         except KeyError:
             pass
         for queue in self._lookup(exchange, routing_key):
             self._avail_client.lpush(queue, dumps(payload))
     except Exception:
         logger.critical('Could not restore message: %r',
                         payload,
                         exc_info=True)
Example #42
0
    def on_message(self, incoming):
        print 'message received %s' % incoming

        text = json.loads(incoming).get('text', None)
        task_id = text if text else 'Sorry could you repeat?'

        wi = Deployer(task_id, websocket=self)
        current_app.logger.info(current_user.id)
        ret = wi.walle_deploy()

        response = json.dumps(dict(output='receive: {0}'.format(task_id)))
        self.write_message(response)
Example #43
0
        def invoke_async(self,
                         msg,
                         msg_type=MESSAGE_TYPE.TO_PARALLEL_ANY,
                         expiration=BROKER.DEFAULT_EXPIRATION):
            msg['msg_type'] = msg_type

            try:
                msg = dumps(msg)
            except Exception, e:
                error_msg = 'JSON serialization failed for msg:[%r], e:[%s]'
                logger.error(error_msg, msg, format_exc(e))
                raise
Example #44
0
def store(kvdb, name, **data):
    """ Stores information regarding an invocation that came later than it was allowed.
    """
    key = '{}{}'.format(KVDB.RESP_SLOW, name)
    data = dumps(data)

    if logger.isEnabledFor(TRACE1):
        msg = 'key:[{}], name:[{}], data:[{}]'.format(key, name, data)
        logger.log(TRACE1, msg)

    kvdb.conn.lpush(key, data)
    kvdb.conn.ltrim(key, 0, 99) # TODO: This should be configurable
Example #45
0
    def set_payload(self, response, data_format, transport, service_instance):
        """ Sets the actual payload to represent the service's response out of
        whatever the service produced. This includes converting dictionaries into
        JSON, adding Zato metadata and wrapping the mesasge in SOAP if need be.
        """
        if isinstance(service_instance, AdminService):
            if data_format == SIMPLE_IO.FORMAT.JSON:
                zato_env = {'zato_env':{'result':response.result, 'cid':service_instance.cid, 'details':response.result_details}}
                if response.payload:
                    payload = response.payload.getvalue(False)
                    payload.update(zato_env)
                else:
                    payload = zato_env

                response.payload = dumps(payload)

            else:
                if transport == URL_TYPE.SOAP:
                    zato_message_template = zato_message_soap
                else:
                    zato_message_template = zato_message_declaration_uni

                if response.payload:
                    if not isinstance(response.payload, basestring):
                        response.payload = self._get_xml_admin_payload(service_instance, zato_message_template, response.payload)
                else:
                    response.payload = self._get_xml_admin_payload(service_instance, zato_message_template, None)
        else:
            if not isinstance(response.payload, basestring):
                if isinstance(response.payload, dict) and data_format in (DATA_FORMAT.JSON, DATA_FORMAT.DICT):
                    response.payload = dumps(response.payload)
                elif isinstance(response.payload, dict):
                    response.payload = dumps(response.payload)
                else:
                    response.payload = response.payload.getvalue() if response.payload else ''

        if transport == URL_TYPE.SOAP:
            if not isinstance(service_instance, AdminService):
                if self.use_soap_envelope:
                    response.payload = soap_doc.format(body=response.payload)
Example #46
0
    def setUpTestData(cls):
        super(EmailOutboxMessageTests, cls).setUpTestData()

        # Create an email account for the user.
        cls.email_account = EmailAccountFactory.create(
            owner=cls.user_obj,
            tenant=cls.user_obj.tenant
        )

        cls.body_content = (
            'In hac habitasse platea dictumst. Class '
            'aptent taciti sociosqu ad litora torquent per conubia nostra'
            ', per inceptos himenaeos. Ut aliquet elit sed augue bibendum '
            'malesuada.'
        )

        to = '*****@*****.**'
        kwargs = dict(
            subject='Mauris ex tortor, hendrerit non sem eu, varius purus.',
            send_from=cls.email_account,
            to=anyjson.dumps([to]),
            cc=anyjson.dumps(None),
            bcc=anyjson.dumps(None),
            body='<html><body><br/>{}</body></html>'.format(cls.body_content),
            headers={},
            mapped_attachments=0,
            template_attachment_ids='',
            original_message_id='',
            tenant=cls.user_obj.tenant
        )

        cls.email_outbox_message = EmailOutboxMessage.objects.create(**kwargs)

        # Some fields are different types in EmailDraft
        kwargs['to'] = [to]
        kwargs['cc'] = []
        kwargs['bcc'] = []
        kwargs['send_from'] = cls.email_account

        cls.email_draft = EmailDraft.objects.create(**kwargs)
Example #47
0
def edit_auth_host(request,
                   host_id,
                   template='mail/config/edit_auth_host.html'):
    'Edits an external auth host'

    host = get_object_or_404(MailAuthHost, id=host_id)

    if request.method == 'POST':
        form = EditMailAuthHostForm(request.POST, instance=host)
        if form.is_valid():
            try:
                saved_host = form.save()
                msg = _(
                    'External authentication %(auth)s: on host %(host)s for'
                    ' domain %(dom)s has been updated successfully') % {
                        'auth': AUTH_TYPES[saved_host.protocol],
                        'host': saved_host.address,
                        'dom': saved_host.useraddress.address
                    }
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(
                        response,
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
                return HttpResponseRedirect(
                    reverse('view-domain', args=[saved_host.useraddress.id]))
            except IntegrityError:
                msg = _('Update of external authentication failed')
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(
                        response,
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
    else:
        form = EditMailAuthHostForm(instance=host)
    return render_to_response(template,
                              locals(),
                              context_instance=RequestContext(request))
Example #48
0
    def test_do_restore_message(self):
        client = Mock(name='client')
        pl1 = {'body': 'BODY'}
        spl1 = dumps(pl1)
        lookup = self.channel._lookup = Mock(name='_lookup')
        lookup.return_value = ['george', 'elaine']
        self.channel._do_restore_message(
            pl1,
            'ex',
            'rkey',
            client,
        )
        client.rpush.assert_has_calls([
            call('george', spl1),
            call('elaine', spl1),
        ])

        pl2 = {'body': 'BODY2', 'headers': {'x-funny': 1}}
        headers_after = dict(pl2['headers'], redelivered=True)
        spl2 = dumps(dict(pl2, headers=headers_after))
        self.channel._do_restore_message(
            pl2,
            'ex',
            'rkey',
            client,
        )
        client.rpush.assert_has_calls([
            call('george', spl2),
            call('elaine', spl2),
        ])

        client.rpush.side_effect = KeyError()
        with patch('kombu.transport.redis.logger') as logger:
            self.channel._do_restore_message(
                pl2,
                'ex',
                'rkey',
                client,
            )
            self.assertTrue(logger.critical.called)
Example #49
0
    def render_GET(self, request):
        try:
            log.msg("Handle request: %s" % request.path, logLevel=logging.DEBUG)
            request.setHeader("Content-Type", "application/json")

            if request.path == "/add/":
                params = json.loads(request.args["data"][0])
                response = self.__add_scrapers__(params)
                return json.dumps(response)

            elif request.path == "/list/":
                response = self.__list_scrapers__()
                return json.dumps(response)

            elif request.path == "/remove/":
                params = json.loads(request.args["data"][0])
                response = self.__remove_scrapers__(params)
                return json.dumps(response)
            elif request.path == "/ping/":
                return "pong"
            elif request.path == "/log/":
                logfile = open("log/daily-log.log")
                log_message = logfile.read()
                logfile.close()
                return log_message
            else:
                log.msg("Wrong API path '%s'" % request.path,
                        logLevel=logging.DEBUG)
                return json.dumps({
                    "error": True,
                    "message": "Wrong API path '%s'" % request.path,
                })

        except Exception:
            log.msg("Error: %s" % traceback.format_exc(),
                    logLevel=logging.WARNING)
            return json.dumps({
                "error": True,
                "message": traceback.format_exc(),
            })
Example #50
0
    def get(self, request, *args, **kwargs):

        email_address = kwargs.get('email_address', None)

        # 1: Search for Contact with given email address
        results = self._search_contact(email_address)

        # 2: Search for Account with given email address
        if not results:
            results = self._search_account(email_address)

        return HttpResponse(anyjson.dumps(results),
                            content_type='application/json; charset=utf-8')
Example #51
0
 def _do_restore_message(self, payload, exchange, routing_key, client=None):
     with self.conn_or_acquire(client) as client:
         try:
             try:
                 payload['headers']['redelivered'] = True
             except KeyError:
                 pass
             for queue in self._lookup(exchange, routing_key):
                 client.lpush(queue, dumps(payload))
         except Exception:
             logger.critical('Could not restore message: %r',
                             payload,
                             exc_info=True)
Example #52
0
    def from_entry(cls, name, skip_fields=('relative', 'options'), **entry):
        options = entry.get('options') or {}
        fields = dict(entry)
        for skip_field in skip_fields:
            fields.pop(skip_field, None)
        schedule = fields.pop('schedule')
        model_schedule, model_field = cls.to_model_schedule(schedule)

        # reset schedule
        for t in cls.model_schedules:
            fields[t[2]] = None

        fields[model_field] = model_schedule
        fields['args'] = dumps(fields.get('args') or [])
        fields['kwargs'] = dumps(fields.get('kwargs') or {})
        fields['queue'] = options.get('queue')
        fields['exchange'] = options.get('exchange')
        fields['routing_key'] = options.get('routing_key')
        obj, _ = PeriodicTask._default_manager.update_or_create(
            name=name, defaults=fields,
        )
        return cls(obj)
Example #53
0
def delete_auth_host(request, host_id,
                    template='config/delete_auth_host.html'):
    'Deletes an external auth host'
    host = get_object_or_404(MailAuthHost, id=host_id)

    if request.method == 'POST':
        form = DeleteMailAuthHostForm(request.POST, instance=host)
        if form.is_valid():
            try:
                go_id = host.useraddress.id
                msg = _('External authentication %(auth)s: on host %(host)s'
                        ' for domain %(dom)s has been deleted') % {
                            'auth': AUTH_TYPES[host.protocol],
                            'host': host.address,
                            'dom': host.useraddress.address
                        }
                host.delete()
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(response,
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
                return HttpResponseRedirect(reverse('view-domain', args=[go_id]))
            except DatabaseError:
                msg = _('External authentication %(auth)s: on host %(host)s'
                        ' for domain %(dom)s could not be deleted') % {
                        'auth': AUTH_TYPES[host.protocol],
                        'host': host.address,
                        'dom': host.useraddress.address
                        }
                if request.is_ajax():
                    response = anyjson.dumps({'success': False, 'html': msg})
                    return HttpResponse(response,
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
    else:
        form = DeleteMailAuthHostForm(instance=host)
    return render_to_response(template, locals(),
        context_instance=RequestContext(request))
Example #54
0
 def get_source_tree(self, tree_id=None, format='newick', node_id=None, max_depth=None, **kwargs):
     if self.use_v1:
         uri = '{p}/getSourceTree'.format(p=self.prefix)
         return self._get_tree(uri, tree_id, format=format, node_id=node_id, max_depth=max_depth)
     else:
         uri = '{p}/source_tree'.format(p=self.graph_prefix)
         study_id = kwargs.get('study_id', '')
         if len(study_id) < 3 or study_id[2] != '_':
             study_id = 'pg_' + study_id
         data = {'git_sha': kwargs.get('git_sha', ''),
                 'study_id': study_id,
                 'tree_id': tree_id}
         return self.json_http_post_raise(uri, data=anyjson.dumps(data))
Example #55
0
    def handle(self):
        lotteryService = self.outgoing.plain_http.get('Lottery')
        response = lotteryService.conn.post(self.cid)
        root = etree.XML(
            response.text.encode('utf-8'))  #.decode('utf-8').encode('ascii')
        find_text = etree.XPath("//text()")
        text = find_text(root)

        payload = {}
        payload['today'] = text[17]
        payload['description'] = text[19]

        self.response.payload = dumps(payload)
Example #56
0
    def form_valid(self, form):
        email_message = super(EmailMessageDraftView, self).form_valid(form)

        if form.data.get('send_draft', False) == 'true':
            task = self.send_message(email_message)
        else:
            task = self.draft_message(email_message)

        if is_ajax(self.request):
            return HttpResponse(anyjson.dumps({'task_id': task.id}),
                                content_type='application/json')
        else:
            return HttpResponseRedirect(self.get_success_url())
Example #57
0
def delete_host(request, host_id, template='mail/config/delete_host.html'):
    'Deletes Mail host'

    host = get_object_or_404(MailHost, id=host_id)

    if request.method == 'POST':
        form = DeleteMailHost(request.POST, instance=host)
        if form.is_valid():
            try:
                go_id = host.useraddress.id
                msg = _(
                    'Delivery SMTP server: %(server)s has been deleted') % {
                        'server': host.address
                    }
                host.delete()
                if request.is_ajax():
                    response = anyjson.dumps({'success': True, 'html': msg})
                    return HttpResponse(
                        response,
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
                return HttpResponseRedirect(
                    reverse('view-domain', args=[go_id]))
            except DatabaseError:
                msg = _('Delivery SMTP server: %(server)s could not be deleted'
                        ) % {
                            'server': host.address
                        }
                if request.is_ajax():
                    response = anyjson.dumps({'success': False, 'html': msg})
                    return HttpResponse(
                        response,
                        content_type='application/javascript; charset=utf-8')
                djmessages.info(request, msg)
    else:
        form = DeleteMailHost(instance=host)
    return render_to_response(template,
                              locals(),
                              context_instance=RequestContext(request))
Example #58
0
 def __call__(self, request, *args, **kwargs):
     try:
         api = getapi(request)
         data = json.loads(request.REQUEST["data"]
                           ) if request.REQUEST.has_key("data") else {}
         data.update(kwargs)
         try:
             res = self.fun(api, request, *args, **data)
             return HttpResponse(
                 json.dumps({
                     "success": True,
                     "result": res
                 }))
         except Exception, e:
             return ajaxFault(e)
     except xmlrpclib.ProtocolError, e:
         return HttpResponse(
             json.dumps({
                 "success": False,
                 "error": 'Error %s: %s' % (e.errcode, e.errmsg)
             }),
             status=e.errcode if e.errcode in [401, 403] else 200)
Example #59
0
    def POST(self):
        # Retrieve form data
        form = web.input(username="")
        userid = profile.resolve_by_login(form.username)

        # Return JSON response
        if userid:
            media_items = media.get_user_media(userid)
            return json.dumps({
                "avatar": d.absolutify_url(media_items['avatar'][0]['display_url']),
            })

        raise WeasylError('userRecordMissing')
Example #60
0
    def get(self, request, *args, **kwargs):

        query = self.get_query(request, *args, **kwargs)

        if query:
            cursor = connection.cursor()
            cursor.execute(query)

            results = self.parse_results(dictfetchall(cursor))
        else:
            results = []

        return HttpResponse(anyjson.dumps(results))