def setup_imagestore_permissions(instance, created, **kwargs):
    if not created:
        return
    try:
        Album = swapper.load_model('imagestore', 'Album')
        Image = swapper.load_model('imagestore', 'Image')

        image_applabel, image_classname = Image._meta.app_label, Image.__name__.lower()
        album_applabel, album_classname = Album._meta.app_label, Album.__name__.lower()

        image_ctype = ContentType.objects.get(app_label=image_applabel, model=image_classname)
        album_ctype = ContentType.objects.get(app_label=album_applabel, model=album_classname)

        add_image_permission = Permission.objects.get(codename='add_%s' % image_classname, content_type=image_ctype)
        add_album_permission = Permission.objects.get(codename='add_%s' % album_classname, content_type=album_ctype)
        change_image_permission = Permission.objects.get(codename='change_%s' % image_classname, content_type=image_ctype)
        change_album_permission = Permission.objects.get(codename='change_%s' % album_classname, content_type=album_ctype)
        delete_image_permission = Permission.objects.get(codename='delete_%s' % image_classname, content_type=image_ctype)
        delete_album_permission = Permission.objects.get(codename='delete_%s' % album_classname, content_type=album_ctype)
        instance.user_permissions.add(add_image_permission, add_album_permission,)
        instance.user_permissions.add(change_image_permission, change_album_permission,)
        instance.user_permissions.add(delete_image_permission, delete_album_permission,)
    except ObjectDoesNotExist:
        # Permissions are not yet installed or content does not created yet
        # probaly this is first
        pass
Пример #2
0
def process_zipfile(uploaded_album):
    Image = swapper.load_model('imagestore', 'Image')
    Album = swapper.load_model('imagestore', 'Album')

    if default_storage.exists(uploaded_album.zip_file.name):
        # TODO: implement try-except here
        zip = zipfile.ZipFile(uploaded_album.zip_file)
        bad_file = zip.testzip()
        if bad_file:
            raise Exception('"%s" in the .zip archive is corrupt.' % bad_file)

        if not uploaded_album.album:
            uploaded_album.album = \
                Album.objects.create(name=uploaded_album.new_album_name)

        for filename in sorted(zip.namelist()):
            try:
                encoding = chardet.detect(filename)['encoding']
                unicode_filename = filename.decode(encoding)
            except ValueError:  # if detect takes unicode string
                unicode_filename = filename

            # do not process meta files
            if unicode_filename.startswith('__'):
                continue

            logger.info('Processing file {}.'.format(unicode_filename))

            data = zip.read(filename)
            if len(data):
                try:
                    # the following is taken from django.forms.fields.ImageField:
                    # load() could spot a truncated JPEG, but it loads the entire
                    # image in memory, which is a DoS vector. See #3848 and #18520.
                    # verify() must be called immediately after the constructor.
                    PILImage.open(BytesIO(data)).verify()
                except Exception as e:
                    # if a "bad" file is found we just skip it.
                    logger.info('Error verify image: %s' % e)
                    continue

                if hasattr(data, 'seek') and \
                        isinstance(data.seek, collections.Callable):
                    logger.info('Seeked')
                    data.seek(0)

                try:
                    img = Image(album=uploaded_album.album)
                    img.image.save(filename, ContentFile(data))
                    img.save()
                except Exception as e:
                    logger.info('Error create Image: %s' % e)
                    continue
        zip.close()
        uploaded_album.delete()
Пример #3
0
    def test_create(self):
        Type = swapper.load_model('default_app', 'Type')
        Item = swapper.load_model('default_app', 'Item')

        Item.objects.create(
            type=Type.objects.create(name="Type 1"),
            name="Item 1",
        )

        self.assertEqual(Item.objects.count(), 1)

        item = Item.objects.all()[0]
        self.assertEqual(item.type.name, "Type 1")
Пример #4
0
 def test_swap_fields(self):
     Type = swapper.load_model('default_app', 'Type')
     fields = dict(
         (field.name, field)
         for field in Type._meta.fields
     )
     self.assertIn('code', fields)
Пример #5
0
    def set_for_events(self, delete=True, **event_filter):
        """
        Update EventResult cache for the given events.  The event query should
        be specified as query keyword arguments, rather than a queryset, so
        that a JOIN can be used instead of retrieving the results for each
        event separately.
        """

        # Delete existing EventResults (using denormalized Event fields)
        if delete:
            er_filter = {
                'event_' + key: val for key, val in event_filter.items()
            }
            self.filter(**er_filter).delete()

        # Filter results (using JOIN through Report to Event)
        Result = swapper.load_model('vera', 'Result')
        result_filter = {
            'report__event__' + key: val for key, val in event_filter.items()
        }
        ers = []
        results = Result.objects.valid_results(
            **result_filter
        ).select_related('report__event')
        for result in results:
            er = self.model(
                event=result.report.event,
                result=result
            )
            er.denormalize()
            ers.append(er)
        self.bulk_create(ers)
 def test_fields(self):
     Type = swapper.load_model('default_app', 'Type')
     fields = {
         field.name: field
         for field in Type._meta.fields
     }
     self.assertIn('name', fields)
Пример #7
0
def test_page_fails_with_nonexistant_template(client):
    Page = swapper.load_model('varlet', 'Page')
    homepage = Page(url='', template='home.html')
    with pytest.raises(ValidationError) as e:
        homepage.full_clean()
    homepage.save()
    with pytest.raises(TemplateDoesNotExist):
        client.get(homepage.get_absolute_url())
    def process_tweet_queue(self):
        """
        Inserts any queued tweets into the database.

        It is ok for this to be called on a thread other than the streaming thread.
        """

        # this is for calculating the tps rate
        now = time.time()
        diff = now - self.time
        self.time = now

        try:
            batch = self.queue.get_all_nowait()
        except Queue.Empty:
            return 0

        if len(batch) == 0:
            return 0

        Tweet = load_model("twitter_stream", "Tweet")

        tweets = []
        for status in batch:
            if settings.CAPTURE_EMBEDDED and 'retweeted_status' in status:
                if self.to_file:
                    tweets.append(json.dumps(status['retweeted_status']))
                else:
                    tweets.append(Tweet.create_from_json(status['retweeted_status']))

            if self.to_file:
                if 'retweeted_status' in status:
                    del status['retweeted_status']

                tweets.append(json.dumps(status))
            else:
                tweets.append(Tweet.create_from_json(status))

        if tweets:
            if self.to_file:
                if not self._output_file or self._output_file.closed:
                    self._output_file = open(self.to_file, 'ab')
                self._output_file.write("\n".join(tweets) + "\n")
                self._output_file.flush()
                logger.info("Dumped %s tweets at %s tps to %s" % (len(tweets), len(tweets) / diff, self.to_file))
            else:
                Tweet.objects.bulk_create(tweets, settings.INSERT_BATCH_SIZE)
                logger.info("Inserted %s tweets at %s tps" % (len(tweets), len(tweets) / diff))
        else:
            logger.info("Saved 0 tweets")

        if settings.DEBUG:
            # Prevent apparent memory leaks
            # https://docs.djangoproject.com/en/dev/faq/models/#why-is-django-leaking-memory
            from django import db
            db.reset_queries()

        return len(tweets) / diff
Пример #9
0
def test_rendering_sitemaps(rf):
    Page = swapper.load_model('varlet', 'Page')
    pages = [Page.objects.create(url=str(x), template="varlet/pages/layouts/test_template.html") for x in range(1, 5)]
    sm = PageSitemap()
    response = sitemap(rf.get('/'), sitemaps={'pages': sm})
    response.render()
    assert '<loc>http://example.com/3/</loc>' in force_text(response.content)
    assert '<loc>http://example.com/2/</loc>' in force_text(response.content)
    assert '<loc>http://example.com/1/</loc>' in force_text(response.content)
Пример #10
0
def test_page_renders_html_ok(client):
    Page = swapper.load_model('varlet', 'Page')
    homepage = Page(url='', template='varlet/pages/layouts/test_template.html')
    homepage.full_clean()
    homepage.save()
    response = client.get(homepage.get_absolute_url())
    assert response.status_code == 200
    response.render()
    assert '<b>Test template</b>' in force_text(response.content)
Пример #11
0
def test_page_renders_json_detailview_ok(client):
    Page = swapper.load_model('varlet', 'Page')
    page = Page(url='/test/', template='varlet/pages/layouts/test_template.html')
    page.full_clean()
    page.save()
    response = client.get(page.get_absolute_url(), HTTP_ACCEPT="application/json")
    assert response.status_code == 200
    data = json.loads(force_text(response.content))
    assert data['get_absolute_url'] == '/test/'
Пример #12
0
 def get_admin_url(self):
     model = swapper.load_model('varlet', 'Page')
     if not admin.site.is_registered(model):
         return None
     try:
         url = reverse('admin:{}_{}_autocomplete'.format(
             model._meta.app_label, model._meta.model_name))
     except NoReverseMatch:
         return None
     return url
Пример #13
0
def get_page(path):
    Page = swapper.load_model('varlet', 'Page')
    field = Page._meta.get_field('url')
    try:
        cleaned_path = field.to_python(path)
    except ValidationError:
        return None
    try:
        return Page.objects.get(url=cleaned_path)
    except (Page.DoesNotExist, MultipleObjectsReturned):
        return None
def process_zipfile(uploaded_album):
    Image = swapper.load_model('imagestore', 'Image')
    Album = swapper.load_model('imagestore', 'Album')

    if default_storage.exists(uploaded_album.zip_file.name):
        # TODO: implement try-except here
        zip = zipfile.ZipFile(uploaded_album.zip_file)
        bad_file = zip.testzip()
        if bad_file:
            raise Exception('"%s" in the .zip archive is corrupt.' % bad_file)

        if not uploaded_album.album:
            uploaded_album.album = Album.objects.create(name=uploaded_album.new_album_name)

        for filename in sorted(zip.namelist()):
            if filename.startswith('__'):  # do not process meta files
                continue
            print(filename.encode('ascii', errors='replace'))
            data = zip.read(filename)
            if len(data):
                try:
                    # the following is taken from django.forms.fields.ImageField:
                    # load() could spot a truncated JPEG, but it loads the entire
                    # image in memory, which is a DoS vector. See #3848 and #18520.
                    # verify() must be called immediately after the constructor.
                    PILImage.open(six.moves.cStringIO(data)).verify()
                except Exception as ex:
                    # if a "bad" file is found we just skip it.
                    print('Error verify image: %s' % ex.message)
                    continue
                if hasattr(data, 'seek') and isinstance(data.seek, collections.Callable):
                    print('seeked')
                    data.seek(0)
                try:
                    img = Image(album=uploaded_album.album)
                    img.image.save(filename, ContentFile(data))
                    img.save()
                except Exception as ex:
                    print('error create Image: %s' % ex.message)
        zip.close()
        uploaded_album.delete()
Пример #15
0
def setup_imagestore_permissions(instance, created, **kwargs):
    if not created:
        return
    try:
        Album = swapper.load_model('imagestore', 'Album')
        Image = swapper.load_model('imagestore', 'Image')

        perms = []

        for model_class in [Album, Image]:
            for perm_name in ['add', 'change', 'delete']:
                app_label, model_name = model_class._meta.app_label, model_class.__name__.lower()
                perm = Permission.objects.get_by_natural_key('%s_%s' % (perm_name, model_name), app_label, model_name)
                perms.append(perm)

        instance.user_permissions.add(*perms)

    except ObjectDoesNotExist:
        # Permissions are not yet installed or content does not created yet
        # probaly this is first
        pass
Пример #16
0
def stream_status():
    terms = FilterTerm.objects.filter(enabled=True)
    processes = StreamProcess.get_current_stream_processes()
    running = False
    for p in processes:
        if p.status == StreamProcess.STREAM_STATUS_RUNNING:
            running = True
            break

    Tweet = load_model("twitter_stream", "Tweet")
    tweet_count = Tweet.count_approx()
    earliest_time = Tweet.get_earliest_created_at()
    latest_time = Tweet.get_latest_created_at()

    avg_rate = None
    if earliest_time is not None and latest_time is not None:
        avg_rate = float(tweet_count) / (latest_time - earliest_time).total_seconds()

    # Get the tweets / minute over the past 10 minutes
    tweet_counts = []
    if latest_time is not None:
        latest_time_minute = latest_time.replace(second=0, microsecond=0)

        if settings.DATABASES['default']['ENGINE'].endswith('mysql'):
            drop_seconds = "created_at - INTERVAL SECOND(created_at) SECOND"
        elif settings.DATABASES['default']['ENGINE'].endswith('postgresql_psycopg2'):
            drop_seconds = "date_trunc('minute', created_at)"
        else:
            drop_seconds = "created_at"

        tweet_counts = Tweet.objects.extra(select={
            'time': drop_seconds
        }) \
            .filter(created_at__gt=latest_time_minute - timedelta(minutes=20)) \
            .values('time') \
            .order_by('time') \
            .annotate(tweets=models.Count('id'))

        tweet_counts = list(tweet_counts)

    for row in tweet_counts:
        row['time'] = row['time'].isoformat()

    return {
        'running': running,
        'terms': [t.term for t in terms],
        'processes': processes,
        'tweet_count': tweet_count,
        'earliest': earliest_time,
        'latest': latest_time,
        'avg_rate': avg_rate,
        'timeline': tweet_counts
    }
Пример #17
0
def test_page_renders_json_listview_ok(client):
    Page = swapper.load_model('varlet', 'Page')
    homepage = Page(url='', template='varlet/pages/layouts/test_template.html')
    homepage.full_clean()
    homepage.save()
    for x in range(1, 3):
        y = Page.objects.create(url=str(x), template='varlet/pages/layouts/test_template.html')
    response = client.get(homepage.get_absolute_url(), HTTP_ACCEPT="application/json")
    assert response.status_code == 200
    data = json.loads(force_text(response.content))
    assert len(data) == 3
    assert {x['get_absolute_url'] for x in data} == {"/", "/1/", "/2/"}
Пример #18
0
def dictionary_get(request):
    if request.method != "GET":
        return False
    # print('start view')
    model_name = request.GET["model_name"]
    # print(model_name)
    my_model = swapper.load_model("chemical", model_name)
    model_dict = {}

    for mdl in my_model.objects.all():
        model_dict[mdl.pk] = mdl.name

    xml_bytes = json.dumps(model_dict)
    # print(xml_bytes)
    # print('end eiw')
    return HttpResponse(xml_bytes, "application/json")
Пример #19
0
    def vals(self, vals):
        Parameter = swapper.load_model('vera', 'Parameter')
        keys = [(key,) for key in vals.keys()]
        params, success = Parameter.objects.resolve_keys(keys)
        if not success:
            missing = [
                name for name, params in params.items() if params is None
            ]
            raise TypeError(
                "Could not identify one or more parameters: %s!"
                % missing
            )

        for key, param in params.items():
            result, is_new = self.results.get_or_create(type=param)
            result.value = vals[key[0]]
            result.save()
Пример #20
0
    def valid_results(self, **filter):
        Parameter = swapper.load_model('vera', 'Parameter')

        filter['report__status__is_valid'] = True
        filter['empty'] = False

        # DISTINCT ON event, then parameter, collapsing results from different
        # reports into one
        distinct = ['report__event__id'] + nest_ordering(
            'type', Parameter._meta.ordering
        ) + ['type__id']

        # ORDER BY distinct fields, then valid report order
        order = distinct + nest_ordering('report', VALID_REPORT_ORDER)

        return self.filter(
            **filter
        ).order_by(*order).distinct(*distinct).select_related('report')
Пример #21
0
def pages():
    PAGES = (
        '/this/is/a/test/',
        '/this/is/a',
        '/this/is/',
        '/this/',
        '/test/',
        '/unrelated/',
        '/contains/test/in/it/'
    )
    Page = swapper.load_model('varlet', 'Page')
    pages = []
    for page in PAGES:
        page = Page(url=page, template='varlet/pages/layouts/test_template.html')
        page.full_clean()
        page.save()
        pages.append(page)
    yield pages
Пример #22
0
def test_sitemap_includes_urls(client):
    Page = swapper.load_model('varlet', 'Page')
    pages = []
    for x in range(1, 5):
        page = Page(url=str(x), template='varlet/pages/layouts/test_template.html')
        page.full_clean()
        page.save()
        pages.append(page)
    response = client.get(reverse('xml_sitemap'))
    assert response.status_code == 200

    response.render()
    etree = ET.fromstring(response.content)
    children = etree.getchildren()
    locs = [x.getchildren()[0] for x in children]
    urls = [x.text for x in locs]
    xml_paths = set(urlsplit(x).path for x in urls)

    page_paths = set(x.get_absolute_url() for x in pages)
    assert page_paths == xml_paths
Пример #23
0
def stream_status():
    terms = FilterTerm.objects.filter(enabled=True)
    processes = StreamProcess.get_current_stream_processes()
    running = False
    for p in processes:
        if p.status == StreamProcess.STREAM_STATUS_RUNNING:
            running = True
            break

    stream_class_memory_cutoffs = get_stream_cutoff_times()

    Tweet = load_model("twitter_stream", "Tweet")
    tweet_count = Tweet.count_approx()
    analyzed_count = 'a lot'

    if tweet_count < 10000000:
        for stream_class, cutoff_time in stream_class_memory_cutoffs.iteritems():
            if stream_class == TweetStream:
                analyzed_count = TweetStream().count_before(cutoff_time)

    stream = TweetStream()
    earliest_time = stream.get_earliest_stream_time()
    latest_time = stream.get_latest_stream_time()

    avg_rate = None
    if earliest_time is not None and latest_time is not None:
        avg_rate = float(tweet_count) / (latest_time - earliest_time).total_seconds()

    return {
        'running': running,
        'terms': [t.term for t in terms],
        'processes': processes,
        'tweet_count': tweet_count,
        'analyzed_count': analyzed_count,
        'earliest': earliest_time,
        'latest': latest_time,
        'avg_rate': avg_rate
    }
Пример #24
0
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.db.models import Q
from django.urls import reverse
from swapper import load_model

Organization = load_model('openwisp_users', 'Organization')
OrganizationOwner = load_model('openwisp_users', 'OrganizationOwner')
OrganizationUser = load_model('openwisp_users', 'OrganizationUser')
User = get_user_model()


class TestUserAdditionalFieldsMixin(object):
    _additional_user_fields = []

    def _additional_params_pop(self, params):
        fields = self._additional_user_fields
        for field in fields:
            params.pop(field[0])
        return params

    def _additional_params_add(self):
        params = dict()
        fields = self._additional_user_fields
        for field in fields:
            params.update({field[0]: field[1]})
        return params


class TestMultitenantAdminMixin(object):
    def setUp(self):
Пример #25
0
"""
base/admin.py and admin.py are not merged
to keep backward compatibility; otherwise,
there is no reason for the existence of the
base/admin.py file.
"""

from django.contrib import admin
from swapper import load_model

from .base.admin import AbstractCaAdmin, AbstractCertAdmin

Ca = load_model('django_x509', 'Ca')
Cert = load_model('django_x509', 'Cert')


class CertAdmin(AbstractCertAdmin):
    pass


class CaAdmin(AbstractCaAdmin):
    pass


admin.site.register(Ca, CaAdmin)
admin.site.register(Cert, CertAdmin)
from datetime import timedelta

from django.utils import timezone
from freezegun import freeze_time
from swapper import load_model

from ...device.tests import (
    DeviceMonitoringTestCase,
    DeviceMonitoringTransactionTestcase,
)

Metric = load_model('monitoring', 'Metric')
Notification = load_model('openwisp_notifications', 'Notification')
Device = load_model('config', 'Device')
Config = load_model('config', 'Config')
OrganizationUser = load_model('openwisp_users', 'OrganizationUser')

notification_queryset = Notification.objects.order_by('timestamp')
start_time = timezone.now()
ten_minutes_ago = start_time - timedelta(minutes=10)
ten_minutes_after = start_time + timedelta(minutes=10)


class TestMonitoringNotifications(DeviceMonitoringTestCase):
    device_model = Device
    config_model = Config

    def test_general_check_threshold_crossed_immediate(self):
        admin = self._create_admin()
        m = self._create_general_metric(name='load')
        self._create_alert_settings(metric=m,
Пример #27
0
import swapper
from django.test import TestCase

from .utils import CreateGraphObjectsMixin, CreateOrgMixin, LoadMixin, UnpublishMixin

Link = swapper.load_model('topology', 'Link')
Node = swapper.load_model('topology', 'Node')
Snapshot = swapper.load_model('topology', 'Snapshot')
Topology = swapper.load_model('topology', 'Topology')


class TestApi(
    CreateGraphObjectsMixin, CreateOrgMixin, UnpublishMixin, LoadMixin, TestCase
):
    list_url = '/api/v1/topology/'
    topology_model = Topology
    node_model = Node
    link_model = Link
    snapshot_model = Snapshot

    def setUp(self):
        org = self._create_org()
        t = self._create_topology(organization=org)
        self._create_node(
            label="node1", addresses=["192.168.0.1"], topology=t, organization=org
        )
        self._create_node(
            label="node2", addresses=["192.168.0.2"], topology=t, organization=org
        )

    @property
Пример #28
0
import swapper

from .generics import (
    BaseAvailableIpView,
    BaseExportSubnetView,
    BaseImportSubnetView,
    BaseIpAddressListCreateView,
    BaseIpAddressView,
    BaseRequestIPView,
    BaseSubnetListCreateView,
    BaseSubnetView,
)

IpAddress = swapper.load_model('django_ipam', 'IpAddress')
Subnet = swapper.load_model('django_ipam', 'Subnet')


class AvailableIpView(BaseAvailableIpView):
    """
    Get the next available IP address under a subnet
    """
    subnet_model = Subnet
    queryset = IpAddress.objects.none()


class RequestIPView(BaseRequestIPView):
    """
    Request and create a record for the next available IP address under a subnet
    """
    subnet_model = Subnet
    queryset = IpAddress.objects.none()
Пример #29
0
from django.utils.translation import gettext_lazy as _
from rest_framework.permissions import BasePermission
from swapper import load_model

Organization = load_model('openwisp_users', 'Organization')


class BaseOrganizationPermission(BasePermission):
    def has_object_permission(self, request, view, obj):
        organization = self.get_object_organization(view, obj)
        return self.validate_membership(request.user, organization)

    def has_permission(self, request, view):
        return request.user and request.user.is_authenticated

    def get_object_organization(self, view, obj):
        organization_field = getattr(view, 'organization_field',
                                     'organization')
        fields = organization_field.split('__')
        accessed_object = obj
        for field in fields:
            accessed_object = getattr(accessed_object, field, None)
            if not accessed_object:
                raise AttributeError(
                    _('Organization not found, `organization_field` '
                      'not implemented correctly.'))
        return accessed_object

    def validate_membership(self, user, org):
        raise NotImplementedError(
            _('View\'s permission_classes not implemented correctly.'
Пример #30
0
    AbstractFloorPlanAdmin,
    AbstractFloorPlanForm,
    AbstractFloorPlanInline,
    AbstractLocationAdmin,
    AbstractLocationForm,
    AbstractObjectLocationForm,
    ObjectLocationMixin,
)
from swapper import load_model

from openwisp_users.multitenancy import MultitenantOrgFilter

from ..admin import MultitenantAdminMixin
from ..config.admin import DeviceAdmin

DeviceLocation = load_model('geo', 'DeviceLocation')
FloorPlan = load_model('geo', 'FloorPlan')
Location = load_model('geo', 'Location')


class FloorPlanForm(AbstractFloorPlanForm):
    class Meta(AbstractFloorPlanForm.Meta):
        model = FloorPlan
        exclude = ('organization', )  # automatically managed


class FloorPlanInline(AbstractFloorPlanInline):
    form = FloorPlanForm
    model = FloorPlan

Пример #31
0
from contextlib import redirect_stderr
from io import StringIO
from unittest.mock import patch

from swapper import load_model

from openwisp_utils.tests import catch_signal

from ... import settings as monitoring_settings
from ..signals import device_metrics_received
from . import DeviceMonitoringTestCase

Chart = load_model('monitoring', 'Chart')
Metric = load_model('monitoring', 'Metric')
DeviceData = load_model('device_monitoring', 'DeviceData')


class TestDeviceApi(DeviceMonitoringTestCase):
    """
    Tests API (device metric collection)
    """
    def test_404(self):
        r = self._post_data(self.device_model().pk, '123', self._data())
        self.assertEqual(r.status_code, 404)

    def test_403(self):
        o = self._create_org()
        d = self._create_device(organization=o)
        r = self.client.post(self._url(d.pk))
        self.assertEqual(r.status_code, 403)
        r = self._post_data(d.id, 'WRONG', self._data())
Пример #32
0
from rest_framework.test import APITestCase
from rest_pandas.test import parse_csv
from django.conf import settings
import unittest
try:
    from matplotlib.cbook import boxplot_stats
except ImportError:
    boxplot_stats = None

import swapper
Report = swapper.load_model('vera', 'Report')
ReportStatus = swapper.load_model('vera', 'ReportStatus')
Parameter = swapper.load_model('vera', 'Parameter')


class SwapTestCase(APITestCase):
    def setUp(self):
        data = (
            ('2014-01-01', 'Site1', 'Temp', 0.5),
            ('2014-01-02', 'Site1', 'Temp', 0.4),
            ('2014-01-03', 'Site1', 'Temp', 0.6),
            ('2014-01-04', 'Site1', 'Temp', 0.2),
            ('2014-01-05', 'Site1', 'Temp', 0.1),

            ('2014-01-01', 'Site2', 'Temp', 0.4),
            ('2014-01-02', 'Site2', 'Temp', 0.3),
            ('2014-01-03', 'Site2', 'Temp', 0.6),
            ('2014-01-04', 'Site2', 'Temp', 0.7),
            ('2014-01-05', 'Site2', 'Temp', 0.2),

            ('2014-01-01', 'Site2', 'Snow', 0.1),
Пример #33
0
 def device_post_save_receiver(cls, instance, created, **kwargs):
     if created:
         DeviceMonitoring = load_model('device_monitoring',
                                       'DeviceMonitoring')
         DeviceMonitoring.objects.create(device=instance)
Пример #34
0
 def connect_is_working_changed(cls):
     is_working_changed.connect(
         cls.is_working_changed_receiver,
         sender=load_model('connection', 'DeviceConnection'),
         dispatch_uid='is_working_changed_monitoring',
     )
Пример #35
0
import os
from unittest import skipIf

import swapper
from django.contrib.auth import get_user_model
from django.test import TestCase

from . import CreateRadiusObjectsMixin, PostParamsMixin
from .. import settings as app_settings
from .base.test_api import BaseTestApi, BaseTestApiReject

RadiusPostAuth = swapper.load_model('django_freeradius', 'RadiusPostAuth')
RadiusAccounting = swapper.load_model('django_freeradius', 'RadiusAccounting')
RadiusBatch = swapper.load_model('django_freeradius', 'RadiusBatch')


class ApiTestCase(PostParamsMixin, CreateRadiusObjectsMixin, TestCase):
    pass


@skipIf(os.environ.get('SAMPLE_APP', False), 'Running tests on SAMPLE_APP')
class TestApi(BaseTestApi, ApiTestCase):
    radius_postauth_model = RadiusPostAuth
    radius_accounting_model = RadiusAccounting
    radius_batch_model = RadiusBatch
    user_model = get_user_model()
    auth_header = 'Bearer {}'.format(app_settings.API_TOKEN)
    token_querystring = '?token={}'.format(app_settings.API_TOKEN)


@skipIf(os.environ.get('SAMPLE_APP', False), 'Running tests on SAMPLE_APP')
Пример #36
0
from allauth.account.models import EmailAddress
from django.contrib import auth
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.test import TestCase
from django.urls import reverse
from openwisp_utils.tests import AssertNumQueriesSubTestMixin
from swapper import load_model

from ..utils import TestOrganizationMixin

Organization = load_model('openwisp_users', 'Organization')
User = get_user_model()
Group = load_model('openwisp_users', 'Group')
OrganizationUser = load_model('openwisp_users', 'OrganizationUser')


class TestUsersApi(
        AssertNumQueriesSubTestMixin,
        TestOrganizationMixin,
        TestCase,
):
    def setUp(self):
        user = get_user_model().objects.create_superuser(
            username='******', password='******', email='*****@*****.**')
        self.client.force_login(user)

    # Tests for Organization Model API endpoints
    def test_organization_list_api(self):
        path = reverse('users:organization_list')
        with self.assertNumQueries(3):
Пример #37
0
import swapper

from formula_one.serializers.base import ModelSerializer

BiologicalInformation = swapper.load_model('kernel', 'BiologicalInformation')


class BiologicalInformationSerializer(ModelSerializer):
    """
    Serializer for BiologicalInformation objects
    """
    class Meta:
        """
        Meta class for BiologicalInformationSerializer
        """

        model = BiologicalInformation
        exclude = [
            'person',
            'id',
            'datetime_created',
            'datetime_modified',
        ]
        read_only_fields = [
            'date_of_birth',
        ]
Пример #38
0
from django.test import TestCase
from django.urls import reverse
from django.utils.timezone import now, timedelta
from freezegun import freeze_time
from swapper import get_model_name, load_model

from openwisp_controller.config.tests.test_admin import TestImportExportMixin
from openwisp_controller.config.tests.utils import CreateDeviceGroupMixin
from openwisp_controller.geo.tests.utils import TestGeoMixin
from openwisp_users.tests.utils import TestMultitenantAdminMixin

from ...check.settings import CHECK_CLASSES
from ..admin import CheckInline, CheckInlineFormSet
from . import DeviceMonitoringTestCase, TestWifiClientSessionMixin

Chart = load_model('monitoring', 'Chart')
Metric = load_model('monitoring', 'Metric')
DeviceData = load_model('device_monitoring', 'DeviceData')
WifiClient = load_model('device_monitoring', 'WifiClient')
WifiSession = load_model('device_monitoring', 'WifiSession')
User = get_user_model()
Check = load_model('check', 'Check')
# needed for config.geo
Device = load_model('config', 'Device')
DeviceLocation = load_model('geo', 'DeviceLocation')
Location = load_model('geo', 'Location')


class TestAdmin(
    TestWifiClientSessionMixin, TestImportExportMixin, DeviceMonitoringTestCase
):
Пример #39
0
import swapper
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from rest_framework import generics
from rest_framework.decorators import api_view
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response

from .serializers import RadiusAccountingSerializer, RadiusPostAuthSerializer

RadiusPostAuth = swapper.load_model("django_freeradius", "RadiusPostAuth")
RadiusAccounting = swapper.load_model("django_freeradius", "RadiusAccounting")
User = get_user_model()


@api_view(['POST'])
def authorize(request):
    username = request.data.get('username')
    password = request.data.get('password')
    try:
        user = User.objects.get(username=username, is_active=True)
    except User.DoesNotExist:
        user = None
    if user and user.check_password(password):
        return Response({'control:Auth-Type': 'Accept'}, status=200)
    return Response({'control:Auth-Type': 'Reject'}, status=401)


class PostAuthView(generics.CreateAPIView):
    serializer_class = RadiusPostAuthSerializer
Пример #40
0
import re

import responses
import swapper
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.test import TestCase
from django.urls import reverse

from openwisp_users.tests.utils import TestMultitenantAdminMixin, TestOrganizationMixin

from ..admin import TopologyAdmin
from .utils import CreateGraphObjectsMixin, CreateOrgMixin, LoadMixin

Link = swapper.load_model('topology', 'Link')
Node = swapper.load_model('topology', 'Node')
Topology = swapper.load_model('topology', 'Topology')


class TestAdmin(CreateGraphObjectsMixin, CreateOrgMixin, LoadMixin, TestCase):
    module = 'openwisp_network_topology'
    app_label = 'topology'
    topology_model = Topology
    link_model = Link
    node_model = Node
    user_model = get_user_model()
    fixtures = ['test_users.json']
    api_urls_path = 'api.urls'

    @property
    def prefix(self):
def migrate_influxdb_data(
    configuration,
    new_measurement,
    read_query=READ_QUERY,
    delete_query=DELETE_QUERY,
):
    Metric = load_model('monitoring', 'Metric')
    metric_qs = Metric.objects.filter(configuration=configuration, key=new_measurement)
    for metric in metric_qs.iterator(chunk_size=CHUNK_SIZE):
        old_measurement = metric.main_tags.get('ifname')
        fields = ','.join(['time', metric.field_name, *metric.related_fields])
        query = (f"{read_query} ORDER BY time DESC LIMIT {SELECT_QUERY_LIMIT}").format(
            fields=fields,
            measurement=old_measurement,
            content_type_key=metric.content_type_key,
            object_id=metric.object_id,
        )
        # Read and write data in batches to avoid loading
        # all data in memory at once.
        offset = 0
        migrated_rows = 0
        read_data = retry_until_success(timeseries_db.query, query, epoch='s')
        while read_data:
            write_data, write_data_count = get_writable_data(
                read_data, metric.tags, old_measurement, new_measurement=new_measurement
            )
            start = offset
            end = offset + min(write_data_count, SELECT_QUERY_LIMIT)
            response = retry_until_success(
                timeseries_db.db.write_points,
                write_data,
                tags=metric.tags,
                batch_size=WRITE_BATCH_SIZE,
            )
            if response is True:
                logger.info(
                    f'Successfully written rows for "{metric} (id:{metric.id})"'
                    f' from {start} to {end}.'
                )
                migrated_rows += write_data_count
                offset += SELECT_QUERY_LIMIT
            else:
                logger.warning(
                    f'Error encountered in writing data for "{metric} (id:{metric.id})"'
                    f' from {start} to {end}: {response["error"]}. It will be retried.'
                )
            read_data = retry_until_success(
                timeseries_db.query, f'{query} OFFSET {offset}', epoch='s'
            )
        logger.info(f'Migrated {migrated_rows} row(s) for "{metric} (id:{metric.id})".')

        # Delete data that has been migrated
        if delete_query:
            retry_until_success(
                timeseries_db.query,
                delete_query.format(
                    old_measurement=old_measurement,
                    content_type_key=metric.content_type_key,
                    object_id=metric.object_id,
                ),
            )
            logger.info(f'Deleted old measurements for "{metric} (id:{metric.id})".')
Пример #42
0
Файл: views.py Проект: wq/vera
from rest_pandas import (
    PandasView, PandasUnstackedSerializer, PandasScatterSerializer,
    PandasBoxplotSerializer,
)
from .serializers import EventResultSerializer
from .filters import ChartFilterBackend


import swapper
EventResult = swapper.load_model('results', 'EventResult')


class ChartView(PandasView):
    queryset = EventResult.objects.all()
    serializer_class = EventResultSerializer
    filter_backends = [ChartFilterBackend]

    def get_queryset(self):
        qs = super(ChartView, self).get_queryset()
        qs = qs.select_related('event_site', 'result_type')
        return qs


class TimeSeriesView(ChartView):
    pandas_serializer_class = PandasUnstackedSerializer


class ScatterView(ChartView):
    pandas_serializer_class = PandasScatterSerializer

Пример #43
0
from openwisp_utils.api.serializers import ValidatedModelSerializer
from rest_framework import serializers
from swapper import load_model

IpAddress = load_model('openwisp_ipam', 'IpAddress')
Subnet = load_model('openwisp_ipam', 'Subnet')


class IpRequestSerializer(ValidatedModelSerializer):
    class Meta:
        model = IpAddress
        fields = ('subnet', 'description')
        read_only_fields = ('created', 'modified')


class IpAddressSerializer(ValidatedModelSerializer):
    class Meta:
        model = IpAddress
        fields = '__all__'
        read_only_fields = ('created', 'modified')


class SubnetSerializer(ValidatedModelSerializer):
    class Meta:
        model = Subnet
        fields = '__all__'
        read_only_fields = ('created', 'modified')


class ImportSubnetSerializer(serializers.Serializer):
    csvfile = serializers.FileField()
Пример #44
0
import swapper

from rest_framework import serializers

from bhawan_app.models import Visitor

Person = swapper.load_model('Kernel', 'Person')


class VisitorSerializer(serializers.ModelSerializer):
    """
    Serializer for Visitor objects
    """

    full_name = serializers.CharField(source='person.full_name',
                                      read_only=True)

    class Meta:
        """
        Meta class for ProfileSerializer
        """

        model = Visitor
        fields = [
            'id',
            'full_name',
            'relation',
            'photo_identification',
        ]
Пример #45
0
    list_display = ('name', 'units')
    list_filter = ('units', 'is_numeric')


class ReportAdmin(admin.ModelAdmin):
    inlines = [ResultInline]


class EventAdmin(admin.ModelAdmin):
    inlines = [ReportInline]

# Register models with admin, but only if they haven't been swapped
default_admin = {
    'Site': admin.IdentifiedRelatedModelAdmin,
    'Event': EventAdmin,
    'Report': ReportAdmin,
    'ReportStatus': admin.ModelAdmin,
    'Parameter': ParameterAdmin,
}
for model in default_admin:
    if swapper.is_swapped('vera', model):
        continue
    admin.site.register(
        swapper.load_model('vera', model),
        default_admin[model]
    )

# Auto-register relevant patterns' type models
admin.site.register(Authority, admin.AuthorityAdmin)
admin.site.register(RelationshipType)
Пример #46
0
from openwisp_utils.admin import (
    AlwaysHasChangedMixin,
    TimeReadonlyAdminMixin,
    UUIDAdmin,
)

from ..admin import MultitenantAdminMixin
from ..pki.base import PkiReversionTemplatesMixin
from . import settings as app_settings
from .utils import send_file
from .views import schema
from .widgets import JsonSchemaWidget

logger = logging.getLogger(__name__)
prefix = 'config/'
Config = load_model('config', 'Config')
Device = load_model('config', 'Device')
Template = load_model('config', 'Template')
Vpn = load_model('config', 'Vpn')
OrganizationConfigSettings = load_model('config', 'OrganizationConfigSettings')

if 'reversion' in settings.INSTALLED_APPS:
    from reversion.admin import VersionAdmin as ModelAdmin
else:  # pragma: nocover
    from django.contrib.admin import ModelAdmin


class BaseAdmin(TimeReadonlyAdminMixin, PkiReversionTemplatesMixin,
                ModelAdmin):
    history_latest_first = True
Пример #47
0
class TestModel(BaseTestModel, CreateModelsMixin, TestCase):
    ipaddress_model = swapper.load_model('django_ipam', 'IPAddress')
    subnet_model = swapper.load_model('django_ipam', 'Subnet')
from import_export import resources
import swapper

Signature = swapper.load_model("petition", "Signature")


class SignatureResource(resources.ModelResource):
    class Meta:
        model = Signature
Пример #49
0
 def __setmodels__(self):
     self.location_model = swapper.load_model('geo', 'Location')
Пример #50
0
import swapper
from django.contrib import admin
from django.contrib.auth import get_user_model

from django_freeradius.base.admin import (
    AbstractNasAdmin, AbstractRadiusAccountingAdmin, AbstractRadiusBatchAdmin, AbstractRadiusCheckAdmin,
    AbstractRadiusGroupAdmin, AbstractRadiusGroupCheckAdmin, AbstractRadiusGroupReplyAdmin,
    AbstractRadiusPostAuthAdmin, AbstractRadiusReplyAdmin, AbstractRadiusUserGroupAdmin, AbstractUserAdmin,
)

RadiusCheck = swapper.load_model('django_freeradius', 'RadiusCheck')
RadiusReply = swapper.load_model('django_freeradius', 'RadiusReply')
RadiusGroup = swapper.load_model('django_freeradius', 'RadiusGroup')
RadiusGroupCheck = swapper.load_model('django_freeradius', 'RadiusGroupCheck')
RadiusGroupReply = swapper.load_model('django_freeradius', 'RadiusGroupReply')
RadiusUserGroup = swapper.load_model('django_freeradius', 'RadiusUserGroup')
RadiusAccounting = swapper.load_model('django_freeradius', 'RadiusAccounting')
RadiusPostAuth = swapper.load_model('django_freeradius', 'RadiusPostAuth')
Nas = swapper.load_model('django_freeradius', 'Nas')
RadiusBatch = swapper.load_model('django_freeradius', 'RadiusBatch')


@admin.register(RadiusCheck)
class RadiusCheckAdmin(AbstractRadiusCheckAdmin):
    pass


@admin.register(RadiusReply)
class RadiusReplyAdmin(AbstractRadiusReplyAdmin):
    pass
Пример #51
0
Файл: wizard.py Проект: wq/vera
from data_wizard import registry
from wq.db.patterns import serializers as patterns
import swapper

Site = swapper.load_model('params', 'Site')
Parameter = swapper.load_model('params', 'Parameter')


class SiteSerializer(patterns.IdentifiedModelSerializer):
    class Meta:
        model = Site
        fields = "__all__"


class ParameterSerializer(patterns.IdentifiedModelSerializer):
    class Meta:
        model = Parameter
        fields = "__all__"


registry.register('Site Metadata', SiteSerializer)
registry.register('Parameter Metadata', ParameterSerializer)
Пример #52
0
import swapper
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline

from formula_one.admin.model_admins.base import ModelAdmin
from formula_one.models import (ContactInformation, LocationInformation,
                                SocialInformation)
from omniport.admin.site import omnipotence

Person = swapper.load_model('kernel', 'Person')
BiologicalInformation = swapper.load_model('kernel', 'BiologicalInformation')
FinancialInformation = swapper.load_model('kernel', 'FinancialInformation')
PoliticalInformation = swapper.load_model('kernel', 'PoliticalInformation')
ResidentialInformation = swapper.load_model('kernel', 'ResidentialInformation')


class BaseGenericRelationsInline(GenericTabularInline):
    """
    This is the base class inherited by all classes implementing
    inline generic relation in admin page
    """

    ct_field = 'entity_content_type'
    ct_fk_field = 'entity_object_id'
    fk_name = 'entity'
    max_num = 1


class ContactInformationInline(BaseGenericRelationsInline):
    """
    This class implements ContactInformation inline for Person in Omnipotence
Пример #53
0
from rest_pandas import (
    PandasView, PandasUnstackedSerializer, PandasScatterSerializer,
    PandasBoxplotSerializer,
)
from .serializers import EventResultSerializer
from .filters import ChartFilterBackend


import swapper
EventResult = swapper.load_model('vera', 'EventResult')


class ChartView(PandasView):
    queryset = EventResult.objects.all()
    serializer_class = EventResultSerializer
    filter_backends = [ChartFilterBackend]

    def get_queryset(self):
        qs = super(ChartView, self).get_queryset()
        qs = qs.select_related('event_site', 'result_type')
        return qs


class TimeSeriesView(ChartView):
    pandas_serializer_class = PandasUnstackedSerializer


class ScatterView(ChartView):
    pandas_serializer_class = PandasScatterSerializer

Пример #54
0
import swapper
from django.urls import reverse
from django.utils.timezone import localtime
from freezegun import freeze_time
from rest_framework.authtoken.models import Token

from openwisp_radius.api import views as api_views
from openwisp_utils.tests import capture_any_output

from ...utils import load_model
from .. import _TEST_DATE
from ..mixins import ApiTokenMixin, BaseTestCase

RadiusToken = load_model('RadiusToken')
OrganizationUser = swapper.load_model('openwisp_users', 'OrganizationUser')


class TestApiUserToken(ApiTokenMixin, BaseTestCase):
    def _get_url(self):
        return reverse('radius:user_auth_token', args=[self.default_org.slug])

    def _user_auth_token_helper(self, username):
        url = self._get_url()
        response = self.client.post(url, {
            'username': '******',
            'password': '******'
        })
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['key'], Token.objects.first().key)
        self.assertEqual(
            response.data['radius_user_token'],
Пример #55
0
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db.transaction import atomic
from django.test import TestCase
from netjsonconfig import OpenWrt
from swapper import load_model

from openwisp_users.tests.utils import TestOrganizationMixin
from openwisp_utils.tests import catch_signal

from .. import settings as app_settings
from ..signals import config_modified, config_status_changed
from .utils import CreateConfigTemplateMixin, TestVpnX509Mixin

Config = load_model('config', 'Config')
Device = load_model('config', 'Device')
Template = load_model('config', 'Template')
Vpn = load_model('config', 'Vpn')
Ca = load_model('django_x509', 'Ca')


class TestConfig(CreateConfigTemplateMixin, TestOrganizationMixin,
                 TestVpnX509Mixin, TestCase):
    """
    tests for Config model
    """

    fixtures = ['test_templates']
    maxDiff = None
Пример #56
0
from django.db import transaction
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from swapper import load_model

from openwisp_users.api.mixins import FilterSerializerByOrgManaged
from openwisp_utils.api.serializers import ValidatedModelSerializer

Template = load_model('config', 'Template')
Vpn = load_model('config', 'Vpn')
Device = load_model('config', 'Device')
Config = load_model('config', 'Config')
Organization = load_model('openwisp_users', 'Organization')


class BaseMeta:
    read_only_fields = ['created', 'modified']


class BaseSerializer(FilterSerializerByOrgManaged, ValidatedModelSerializer):
    pass


class TemplateSerializer(BaseSerializer):
    config = serializers.JSONField(initial={}, required=False)
    tags = serializers.StringRelatedField(many=True, read_only=True)
    default_values = serializers.JSONField(required=False, initial={})
    include_shared = True

    class Meta(BaseMeta):
Пример #57
0
 def device_post_delete_receiver(cls, instance, **kwargs):
     DeviceData = load_model('device_monitoring', 'DeviceData')
     instance.__class__ = DeviceData
     instance.checks.all().delete()
     instance.metrics.all().delete()
from rest_framework import authentication
import swapper
Token = swapper.load_model('multitoken', 'Token')


class TokenAuthentication(authentication.TokenAuthentication):
    model = Token
Пример #59
0
#!/usr/bin/env python
# vim:fileencoding=utf-8
from __future__ import unicode_literals
from django.urls import reverse, NoReverseMatch
from django.conf import settings
import swapper

Image = swapper.load_model('imagestore', 'Image')
Album = swapper.load_model('imagestore', 'Album')


def imagestore_processor(request):
    template = getattr(settings, 'IMAGESTORE_TEMPLATE', False)
    ret = {
        'IMAGESTORE_SHOW_USER': getattr(settings, 'IMAGESTORE_SHOW_USER', True),
        'IMAGESTORE_SHOW_TAGS': getattr(settings, 'IMAGESTORE_SHOW_TAGS',
                                        not swapper.is_swapped('imagestore', 'Image')),
        'IMAGESTORE_LOAD_CSS': getattr(settings, 'IMAGESTORE_LOAD_CSS', True)
    }
    try:
        ret['imagestore_index_url'] = reverse('imagestore:index')
    except NoReverseMatch:  # Bastard django-cms from hell!!!!111
        pass
    if template:
        ret['IMAGESTORE_TEMPLATE'] = template
    image_applabel, image_classname = Image._meta.app_label, Image.__name__.lower()
    album_applabel, album_classname = Album._meta.app_label, Album.__name__.lower()
    ret['imagestore_perms'] = {
        'add_image': request.user.has_perm('%s.add_%s' % (image_applabel, image_classname)),
        'add_album': request.user.has_perm('%s.add_%s' % (album_applabel, album_classname)),
    }
Пример #60
0
import logging
import time

from celery import shared_task
from django.core.exceptions import ObjectDoesNotExist
from swapper import load_model

Device = load_model('config', 'Device')
logger = logging.getLogger(__name__)


@shared_task
def update_config(device_id):
    """
    Launches the ``update_config()`` operation
    of a specific device in the background
    """
    # wait for the saving operations of this device to complete
    # (there may be multiple ones happening at the same time)
    time.sleep(2)
    # avoid repeating the operation multiple times
    try:
        device = Device.objects.select_related('config').get(pk=device_id)
        if device.config.status == 'applied':
            return
    except ObjectDoesNotExist as e:
        logger.warning(f'update_config("{device_id}") failed: {e}')
        return
    qs = device.deviceconnection_set.filter(device_id=device_id, enabled=True)
    conn = qs.first()
    if conn: