File: default_fixture.py

package info (click to toggle)
python-django-dynamic-fixture 4.0.1-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 616 kB
  • sloc: python: 3,909; makefile: 237; sh: 6
file content (92 lines) | stat: -rw-r--r-- 2,749 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
from datetime import datetime, date, timedelta
from decimal import Decimal
import random
import string
import uuid

from django.core.exceptions import ImproperlyConfigured


try:
    from django.utils.timezone import now
except ImportError:
    now = datetime.now

try:
    from django.contrib.gis.geos import *
except ImproperlyConfigured:
    pass  # environment without geo libs
except Exception:
    pass # Avoid errors like GDALException


from django_dynamic_fixture.ddf import DataFixture


class BaseDataFixture(DataFixture):
    # Django >= 1.6
    def binaryfield_config(self, field, key):
        return b'\x00\x46\xFE'

    # Django >= 1.8
    def uuidfield_config(self, field, key):
        return uuid.uuid4()

    # Django >= 1.4
    def genericipaddressfield_config(self, field, key):
        return self.ipaddressfield_config(field, key)

    # POSTGRES
    def  jsonfield_config(self, field, key):
        return {}


# GIS/GeoDjango
class GeoDjangoFixtureMixin:
    def create_point(self, x=None, y=None):
        # latitude: [-90,90], longitude: [-180,180]
        latitude = x or random.randint(-90, 90)
        longitude = y or random.randint(-180, 180)
        return Point(longitude, latitude)

    def create_points(self, n=3, closed=True):
        points = [self.create_point() for i in range(n)]
        if closed: # LinearRing
            points.append(points[0])
        return points

    def geometryfield_config(self, field, key):
        return GEOSGeometry('POINT(%s %s)' % self.create_point().coords)

    def pointfield_config(self, field, key):
        return self.create_point()

    def linestringfield_config(self, field, key, n=3):
        return LineString(self.create_points(n))

    def polygonfield_config(self, field, key, n=3):
        return Polygon(self.create_points(n))

    def multipointfield_config(self, field, key, n=3):
        return MultiPoint(self.create_points(n))

    def multilinestringfield_config(self, field, key, n=3):
        lines = [self.linestringfield_config(field, key, n) for i in range(n)]
        return MultiLineString(lines)

    def multipolygonfield_config(self, field, key, n=3):
        polygons = [self.polygonfield_config(field, key, n) for i in range(n)]
        return MultiPolygon(polygons)

    def geometrycollectionfield_config(self, field, key, n=3):
        polygons = [self.polygonfield_config(field, key, n) for i in range(n)]
        return GeometryCollection(polygons)


# Postgres fields
# https://docs.djangoproject.com/en/1.8/ref/contrib/postgres/fields/
class PostgresFixtureMixin:
    def arrayfield_config(self, field, key, n=1):
        data = [self.generate_data(field.base_field) for i in range(n)]
        return data