django-restit 4.2.123__py3-none-any.whl → 4.2.125__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: django-restit
3
- Version: 4.2.123
3
+ Version: 4.2.125
4
4
  Summary: A Rest Framework for DJANGO
5
5
  License: MIT
6
6
  Author: Ian Starnes
@@ -91,7 +91,7 @@ inbox/models/message.py,sha256=acPm2z0QsucoFFmfzPekehElZm8EMzkcGChX_BpzGLU,3758
91
91
  inbox/models/template.py,sha256=i5vf0vsM1U0251UmVsF61MDCV_c7xt-zdCdx1SiKOG0,1013
92
92
  inbox/rpc.py,sha256=7JXvpXlEGKG7by_NkANPGYLCzagyCnTIGM4rme_htpk,1534
93
93
  inbox/utils/__init__.py,sha256=P_UR2rGK3L0tZNlTN-mf99tpeYM-tLkA18iDKXSSLDM,89
94
- inbox/utils/parsing.py,sha256=ae8JKm10qg6Q3dGhC29oDKKycN3yeDxI6e9SryPKxcY,4615
94
+ inbox/utils/parsing.py,sha256=cU5qHaVNkje21b3ApeCixYQEuCsQl0gM8FeLz9DPgs0,5340
95
95
  inbox/utils/render.py,sha256=CU_F2qUBQE7mjb9Q6Dn9ro5CS_O_zEY-wDMHEClKkIA,4331
96
96
  inbox/utils/sending.py,sha256=BKelTZnbkdSLGpjOY6IRTrzj-Hnw2pPZ7RYQGwe-tqk,2179
97
97
  incident/README.md,sha256=4vbZTJj7uUmq8rogYngxqNYjFTlBOujfWUGheLoFKMc,1114
@@ -161,6 +161,7 @@ location/rpc/location.py,sha256=D5R54nnmmp8NaHQRkP_TF921Ej6j9nmTUNzX1K5WVPc,779
161
161
  location/rpc/track.py,sha256=jSS9_eiKayM_8iW_4Ldojg9a2_trjfXhnLe_a261x5E,3735
162
162
  medialib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
163
163
  medialib/admin.py,sha256=befSpVP7TpyWGsh47E1CiFMUSwNYT7bxMgzgyDK07yI,2261
164
+ medialib/caching.py,sha256=K5dE8fpsC2mpFB5WLJQKx9OtjuhvYeTtQM5vBA5IBEE,2292
164
165
  medialib/cvutil/__init__.py,sha256=ADxS2MfnLqzd7NI2QgWeQSNcgJCNBP2Wgy3hBZV9w4s,27
165
166
  medialib/cvutil/contours.py,sha256=B6_PGOIg_ORX9i3K8PE35H_c2H0LejLSRE1sQMxPaIc,4269
166
167
  medialib/cvutil/images.py,sha256=9qQDd1rE3HJZ0MBn2v39snBXatI9fLqYPQXgdCuaQs0,12649
@@ -272,6 +273,7 @@ medialib/stores/oauth2client/tools.py,sha256=OLSW5Iu7rtjGankFpjXlHBw8aAFOcIsWdDh
272
273
  medialib/stores/oauth2client/util.py,sha256=1Uc6qwqNhI3b507VtFnklX4sEZNt0MZbLAME2o6kCgg,5706
273
274
  medialib/stores/oauth2client/xsrfutil.py,sha256=4Plq0y5xEkDwvrveVA32gBRP0A3FkJ_36dIsyGHeJeA,3367
274
275
  medialib/stores/rtmpstore.py,sha256=CFRP6Ss4hbLKngbdaLvA8_oKCSe18B29-oWVgvTXMPA,500
276
+ medialib/stores/s3.py,sha256=gAFiVHQkbYHZQ_2LU5nl2EXyoSs1mW4VdVCjsImisuE,5836
275
277
  medialib/stores/s3store.py,sha256=uKO6I-X83sD9xefCQytLyrV-7t9TSX2p476n_ZBkSHw,4583
276
278
  medialib/stores/uritemplate/__init__.py,sha256=ONWR_KRz9au0O-XUUTrO_UN7GHTmZCTKyvflUQb8wxM,4996
277
279
  medialib/stores/youtubestore.py,sha256=swXOJYfbhgzAv1NjOehjKtfYRZYk2wiPK7a7_boAulc,406
@@ -374,7 +376,7 @@ pushit/utils.py,sha256=IeTCGa-164nmB1jIsK1lu1O1QzUhS3BKfuXHGjCW-ck,2121
374
376
  rest/.gitignore,sha256=TbEvWRMnAiajCTOdhiNrd9eeCAaIjRp9PRjE_VkMM5g,118
375
377
  rest/README.md,sha256=V3ETc-cJu8PZIbKr9xSe_pA4JEUpC8Dhw4bQeVCDJPw,5460
376
378
  rest/RemoteEvents.py,sha256=nL46U7AuxIrlw2JunphR1tsXyqi-ep_gD9CYGpYbNgE,72
377
- rest/__init__.py,sha256=9U4bbRpojdF2iTaNZzy6i2wcFFL4PyJAXf243H2cR_w,122
379
+ rest/__init__.py,sha256=NBdgyzD7Sr_EusbqnuFbRRRqTOZ83u4uzw9o1hGOOnw,122
378
380
  rest/arc4.py,sha256=y644IbF1ec--e4cUJ3KEYsewTCITK0gmlwa5mJruFC0,1967
379
381
  rest/cache.py,sha256=1Qg0rkaCJCaVP0-l5hZg2CIblTdeBSlj_0fP6vlKUpU,83
380
382
  rest/crypto/__init__.py,sha256=Tl0U11rgj1eBYqd6OXJ2_XSdNLumW_JkBZnaJqI6Ldw,72
@@ -509,7 +511,7 @@ ws4redis/servers/uwsgi.py,sha256=VyhoCI1DnVFqBiJYHoxqn5Idlf6uJPHvfBKgkjs34mo,172
509
511
  ws4redis/settings.py,sha256=K0yBiLUuY81iDM4Yr-k8hbvjn5VVHu5zQhmMK8Dtz0s,1536
510
512
  ws4redis/utf8validator.py,sha256=S0OlfjeGRP75aO6CzZsF4oTjRQAgR17OWE9rgZdMBZA,5122
511
513
  ws4redis/websocket.py,sha256=R0TUyPsoVRD7Y_oU7w2I6NL4fPwiz5Vl94-fUkZgLHA,14848
512
- django_restit-4.2.123.dist-info/LICENSE.md,sha256=VHN4hhEeVOoFjtG-5fVv4jesA4SWi0Z-KgOzzN6a1ps,1068
513
- django_restit-4.2.123.dist-info/METADATA,sha256=HGtIB4-fKEK2kSVmhVJ0a3uo7qH0DTy1r_P1xskGYG4,7663
514
- django_restit-4.2.123.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
515
- django_restit-4.2.123.dist-info/RECORD,,
514
+ django_restit-4.2.125.dist-info/LICENSE.md,sha256=VHN4hhEeVOoFjtG-5fVv4jesA4SWi0Z-KgOzzN6a1ps,1068
515
+ django_restit-4.2.125.dist-info/METADATA,sha256=1MGxqma8EG_Bsb6xAzGNoEcvCsFIOwCbnkUjFh3SNQY,7663
516
+ django_restit-4.2.125.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
517
+ django_restit-4.2.125.dist-info/RECORD,,
inbox/utils/parsing.py CHANGED
@@ -1,5 +1,7 @@
1
1
  from io import StringIO
2
2
  import email
3
+ import re
4
+
3
5
  from email.utils import parseaddr, parsedate_to_datetime, getaddresses
4
6
  from email.header import decode_header
5
7
 
@@ -129,6 +131,24 @@ def toFileObject(attachment):
129
131
  return obj
130
132
 
131
133
 
134
+ def parseAddresses(input_string):
135
+ # Define a regex pattern to capture email addresses
136
+ email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
137
+ # Find all matches of the pattern in the input string
138
+ emails = re.findall(email_pattern, input_string)
139
+ # Create a list to hold parsed email data
140
+ parsed_emails = []
141
+ for addr in emails:
142
+ # Extract name if it exists
143
+ name_match = re.search(r'([a-zA-Z\s]+)?\s*<{}>'.format(re.escape(addr)), input_string)
144
+ if name_match and name_match.group(1):
145
+ name = name_match.group(1).strip()
146
+ else:
147
+ name = None
148
+ parsed_emails.append(objict(name=name, email=addr))
149
+ return parsed_emails
150
+
151
+
132
152
  def toString(value):
133
153
  if isinstance(value, bytes):
134
154
  value = value.decode()
medialib/caching.py ADDED
@@ -0,0 +1,63 @@
1
+ from rest import settings
2
+ from medialib.stores import s3
3
+ from django.http import HttpResponse, StreamingHttpResponse
4
+ from datetime import datetime
5
+ AWS_CACHING_BUCKET = settings.get("AWS_CACHING_BUCKET", settings.AWS_S3_BUCKET)
6
+
7
+
8
+ def on_cached_rest_list(Model, request):
9
+ cache_guid = request.DATA.get("format_filename")
10
+ file_format = request.DATA.get("format", None)
11
+ if not file_format:
12
+ file_format = "csv"
13
+ request.DATA.set("format", file_format)
14
+ content_type = "text/csv"
15
+ if file_format[:3] == "csv":
16
+ file_format = "csv"
17
+ else:
18
+ file_format = "json"
19
+ content_type = "application/json"
20
+ if cache_guid:
21
+ resp = getCachedResponse(cache_guid)
22
+ if resp is not None:
23
+ return resp
24
+ resp = Model.on_rest_list(request)
25
+ # TODO only cache if dr_end exists and is less then today
26
+ dr_end = request.DATA.get("dr_end", field_type=datetime)
27
+ if dr_end < datetime.now():
28
+ cacheResponse(resp, cache_guid, content_type=content_type)
29
+ return resp
30
+ return Model.on_rest_list(request)
31
+
32
+
33
+ def getCachedResponse(cache_guid, content_type="application/json"):
34
+ item = getCachedItem(cache_guid, content_type)
35
+ if not item.exists:
36
+ return None
37
+ return _createResponse(item, cache_guid)
38
+
39
+
40
+ def getCachedItem(cache_guid, content_type="application/json"):
41
+ url = f"s3://{settings.AWS_S3_BUCKET}/caching/{cache_guid}"
42
+ return s3.S3Item(url, content_type)
43
+
44
+
45
+ def cacheResponse(response, cache_guid, content_type="application/json"):
46
+ url = f"s3://{settings.AWS_S3_BUCKET}/caching/{cache_guid}"
47
+ item = s3.S3Item(url, content_type)
48
+ item.upload(response.content)
49
+ return item
50
+
51
+
52
+ def _createResponse(item, cache_guid):
53
+ # Create a StreamingHttpResponse object with the file iterator
54
+ fp = item.download()
55
+ fp.seek(0)
56
+ fp.name = cache_guid
57
+ response = StreamingHttpResponse(fp)
58
+ # Add the appropriate content type header to the response
59
+ # You might want to dynamically determine the content type based on the file
60
+ response['Content-Type'] = item.content_type
61
+ # Set the Content-Disposition header to prompt download with a filename
62
+ response['Content-Disposition'] = f'attachment; filename="{cache_guid}"'
63
+ return response
medialib/stores/s3.py ADDED
@@ -0,0 +1,208 @@
1
+ from rest import settings
2
+ from objict import objict
3
+ # import boto
4
+ # from boto.s3.connection import S3Connection
5
+ import boto3
6
+ import botocore
7
+
8
+ from urllib.parse import urlparse
9
+ import io
10
+ import sys
11
+ from medialib import utils
12
+ import threading
13
+ import tempfile
14
+
15
+ S3 = objict(KEY=settings.AWS_KEY, SECRET=settings.AWS_SECRET, BUCKET=settings.AWS_S3_BUCKET)
16
+
17
+
18
+ class S3Item(object):
19
+ def __init__(self, url, content_type="application/json", metadata=None):
20
+ self.url = url
21
+ u = urlparse(url)
22
+ self.bucket_name = u.netloc
23
+ self.key = u.path.lstrip('/')
24
+ self.content_type = content_type
25
+ self.s3 = getS3()
26
+ self.host = "https://s3.amazonaws.com"
27
+ self.metadata = metadata
28
+ self.object = getObject(self.bucket_name, self.key)
29
+ self.exists = self.checkExists()
30
+
31
+ def checkExists(self):
32
+ try:
33
+ self.object.load()
34
+ except botocore.exceptions.ClientError as e:
35
+ if e.response['Error']['Code'] == "404":
36
+ # The object does not exist.
37
+ return False
38
+ else:
39
+ # Something else has gone wrong.
40
+ raise
41
+ return True
42
+
43
+ def upload(self, fp, background=False):
44
+ if self.metadata:
45
+ return self.object.upload_fileobj(openFile(fp), ExtraArgs=self.metadata)
46
+ self.object.upload_fileobj(openFile(fp))
47
+
48
+ def createUpload(self):
49
+ self.part_num = 0
50
+ self.parts = []
51
+ self.client = getS3(False)
52
+ resp = self.client.create_multipart_upload(Bucket=self.bucket_name, Key=self.key)
53
+ self.upload_id = resp["UploadId"]
54
+ return True
55
+
56
+ def uploadPart(self, chunk):
57
+ self.part_num += 1
58
+ resp = self.client.upload_part(
59
+ Bucket=self.bucket_name,
60
+ Key=self.key,
61
+ PartNumber=self.part_num,
62
+ UploadId=self.upload_id,
63
+ Body=chunk)
64
+ self.parts.append(dict(PartNumber=self.part_num, ETag=resp["ETag"]))
65
+
66
+ def completeUpload(self):
67
+ resp = self.client.complete_multipart_upload(
68
+ Bucket=self.bucket_name,
69
+ Key=self.key,
70
+ UploadId=self.upload_id,
71
+ MultipartUpload=dict(Parts=self.parts))
72
+ return resp
73
+
74
+ @property
75
+ def public_url(self):
76
+ return "{}/{}/{}".format(self.host, self.bucket_name, self.key)
77
+
78
+ def generateURL(self, expires=600):
79
+ client = getS3(False)
80
+ return client.generate_presigned_url(
81
+ 'get_object',
82
+ ExpiresIn=expires,
83
+ Params={'Bucket': self.bucket_name, 'Key': self.key})
84
+
85
+ def download(self, fp=None):
86
+ if fp is None:
87
+ fp = tempfile.NamedTemporaryFile()
88
+ self.object.download_fileobj(fp)
89
+ return fp
90
+
91
+ def delete(self):
92
+ self.object.delete()
93
+
94
+
95
+ def _getS3(as_resource=True, key=S3.KEY, secret=S3.SECRET):
96
+ if as_resource:
97
+ return boto3.resource('s3', aws_access_key_id=key, aws_secret_access_key=secret)
98
+ return boto3.client('s3', aws_access_key_id=key, aws_secret_access_key=secret)
99
+
100
+
101
+ def getS3(as_resource=True):
102
+ if as_resource:
103
+ if S3.resource is None:
104
+ S3.resource = _getS3(True)
105
+ return S3.resource
106
+ if S3.client is None:
107
+ S3.client = _getS3(False)
108
+ return S3.client
109
+
110
+
111
+ def getBucket(name):
112
+ s3r = getS3()
113
+ return s3r.Bucket(name)
114
+
115
+
116
+ def getObject(bucket_name, key):
117
+ s3r = getS3()
118
+ return s3r.Object(bucket_name, key)
119
+
120
+
121
+ def getObjectContent(bucket_name, key):
122
+ s3r = getS3(False)
123
+ obj = s3r.get_object(Bucket=bucket_name, Key=key)
124
+ return obj['Body'].read().decode('utf-8')
125
+
126
+
127
+ class ProgressPercentage(object):
128
+ def __init__(self, size):
129
+ self._size = size
130
+ self._seen_so_far = 0
131
+ self._lock = threading.Lock()
132
+
133
+ def __call__(self, bytes_amount):
134
+ # To simplify we'll assume this is hooked up
135
+ # to a single filename.
136
+ with self._lock:
137
+ self._seen_so_far += bytes_amount
138
+ percentage = (self._seen_so_far / self._size) * 100
139
+ sys.stdout.write(
140
+ "\r%s %s / %s (%.2f%%)" % (
141
+ self._filename, self._seen_so_far, self._size,
142
+ percentage))
143
+ sys.stdout.flush()
144
+
145
+
146
+ def upload(url, fp, background=False):
147
+ obj = S3Item(url)
148
+ obj.upload(fp)
149
+
150
+
151
+ def view_url_noexpire(url, is_secure=False):
152
+ obj = S3Item(url)
153
+ return obj.public_url
154
+
155
+
156
+ def view_url(url, expires=600, is_secure=True):
157
+ if expires is None:
158
+ return view_url_noexpire(url, is_secure)
159
+ obj = S3Item(url)
160
+ return obj.generateURL(expires)
161
+
162
+
163
+ def exists(url):
164
+ obj = S3Item(url)
165
+ return obj.exists
166
+
167
+
168
+ def get_file(url, fp=None):
169
+ obj = S3Item(url)
170
+ return obj.download(fp)
171
+
172
+
173
+ def delete(url):
174
+ if url[-1] == "/":
175
+ prefix = url.path.lstrip("/")
176
+ bucket_name = url.netloc
177
+ s3 = getS3()
178
+ response = s3.list_objects_v2(
179
+ Bucket=bucket_name,
180
+ Prefix =prefix,
181
+ MaxKeys=100)
182
+ for obj in response['Contents']:
183
+ s3.delete_object(Bucket=bucket_name, Key=object['Key'])
184
+ else:
185
+ obj = S3Item(url)
186
+ obj.delete()
187
+ # _getkey(url, key=settings.AWS_ADMIN_KEY, secret=settings.AWS_ADMIN_SECRET).delete()
188
+
189
+
190
+ def openFile(fp):
191
+ # this should fail if already opened.. if not iwll open
192
+ # even if wrapped file-like object exists. To avoid Django-specific
193
+ # logic, pass a copy of internal file-like object if `content` is
194
+ # `File` class instance.
195
+ if isinstance(fp, (str, bytes)):
196
+ return io.BytesIO(utils.toBytes(fp))
197
+ if hasattr(fp, "read"):
198
+ return io.BytesIO(utils.toBytes(fp.read()))
199
+ try:
200
+ return open(fp.name, "r")
201
+ except IOError:
202
+ pass
203
+ return fp
204
+
205
+
206
+ def path(url):
207
+ u = urlparse(url)
208
+ return u.path
rest/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  from .uberdict import UberDict # noqa: F401
2
2
  from .settings_helper import settings # noqa: F401
3
3
 
4
- __version__ = "4.2.123"
4
+ __version__ = "4.2.125"