ramifice 0.8.43__py3-none-any.whl → 0.8.45__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,11 +8,12 @@ import logging
8
8
  import uuid
9
9
  from base64 import b64decode
10
10
  from datetime import datetime
11
- from os import makedirs
12
- from os.path import exists, getsize
11
+ from os.path import getsize
13
12
  from shutil import copyfile
13
+ from typing import Any
14
14
 
15
15
  from anyio import Path, open_file, to_thread
16
+ from xloft.converters import to_human_size
16
17
 
17
18
  from ramifice.fields.general.field import Field
18
19
  from ramifice.fields.general.file_group import FileGroup
@@ -132,7 +133,7 @@ class FileField(Field, FileGroup, JsonMixin):
132
133
  """ # noqa: D205
133
134
  base64_str = base64_str or None
134
135
  filename = filename or None
135
- file_info: dict[str, str | int | bool] = {"save_as_is": False}
136
+ file_info: dict[str, Any] = {"save_as_is": False}
136
137
  file_info["is_new_file"] = True
137
138
  file_info["is_delete"] = is_delete
138
139
 
@@ -155,12 +156,17 @@ class FileField(Field, FileGroup, JsonMixin):
155
156
  # Create the current date for the directory name.
156
157
  date_str: str = str(datetime.now(UTC_TIMEZONE).date())
157
158
  # Create path to target directory.
158
- dir_target_path = f"{MEDIA_ROOT}/uploads/{self.target_dir}/{date_str}"
159
+ dir_target_path = Path(
160
+ MEDIA_ROOT,
161
+ "uploads",
162
+ self.target_dir,
163
+ date_str,
164
+ )
159
165
  # Create target directory if it does not exist.
160
- if not await to_thread.run_sync(exists, dir_target_path):
161
- await to_thread.run_sync(makedirs, dir_target_path)
166
+ if not await dir_target_path.exists():
167
+ await dir_target_path.mkdir(parents=True)
162
168
  # Create path to target file.
163
- f_target_path = f"{dir_target_path}/{f_uuid_name}"
169
+ f_target_path = f"{dir_target_path.as_posix()}/{f_uuid_name}"
164
170
  # Save file in target directory.
165
171
  async with await open_file(f_target_path, mode="wb") as open_f:
166
172
  f_content = b64decode(base64_str)
@@ -174,6 +180,9 @@ class FileField(Field, FileGroup, JsonMixin):
174
180
  file_info["extension"] = extension
175
181
  # Add file size (in bytes).
176
182
  file_info["size"] = await to_thread.run_sync(getsize, f_target_path)
183
+ # Convert the number of bytes into a human-readable format.
184
+ # Examples: 200 bytes | 1 KB | 1.5 MB.
185
+ file_info["human_size"] = to_human_size(file_info["size"])
177
186
  #
178
187
  # to value.
179
188
  self.value = file_info
@@ -185,7 +194,7 @@ class FileField(Field, FileGroup, JsonMixin):
185
194
  ) -> None:
186
195
  """Get file information and copy the file to the target directory."""
187
196
  src_path = src_path or None
188
- file_info: dict[str, str | int | bool] = {"save_as_is": False}
197
+ file_info: dict[str, Any] = {"save_as_is": False}
189
198
  file_info["is_new_file"] = True
190
199
  file_info["is_delete"] = is_delete
191
200
 
@@ -201,12 +210,17 @@ class FileField(Field, FileGroup, JsonMixin):
201
210
  # Create the current date for the directory name.
202
211
  date_str: str = str(datetime.now(UTC_TIMEZONE).date())
203
212
  # Create path to target directory.
204
- dir_target_path = f"{MEDIA_ROOT}/uploads/{self.target_dir}/{date_str}"
213
+ dir_target_path = Path(
214
+ MEDIA_ROOT,
215
+ "uploads",
216
+ self.target_dir,
217
+ date_str,
218
+ )
205
219
  # Create target directory if it does not exist.
206
- if not await to_thread.run_sync(exists, dir_target_path):
207
- await to_thread.run_sync(makedirs, dir_target_path)
220
+ if not await dir_target_path.exists():
221
+ await dir_target_path.mkdir(parents=True)
208
222
  # Create path to target file.
209
- f_target_path = f"{dir_target_path}/{f_uuid_name}"
223
+ f_target_path = f"{dir_target_path.as_posix()}/{f_uuid_name}"
210
224
  # Save file in target directory.
211
225
  await to_thread.run_sync(copyfile, src_path, f_target_path)
212
226
  # Add paths to target file.
@@ -218,6 +232,9 @@ class FileField(Field, FileGroup, JsonMixin):
218
232
  file_info["extension"] = extension
219
233
  # Add file size (in bytes).
220
234
  file_info["size"] = await to_thread.run_sync(getsize, f_target_path)
235
+ # Convert the number of bytes into a human-readable format.
236
+ # Examples: 200 bytes | 1 KB | 1.5 MB.
237
+ file_info["human_size"] = to_human_size(file_info["size"])
221
238
  #
222
239
  # to value.
223
240
  self.value = file_info
@@ -8,11 +8,12 @@ import logging
8
8
  import uuid
9
9
  from base64 import b64decode
10
10
  from datetime import datetime
11
- from os import makedirs
12
- from os.path import exists, getsize
11
+ from os.path import getsize
13
12
  from shutil import copyfile
13
+ from typing import Any
14
14
 
15
15
  from anyio import Path, open_file, to_thread
16
+ from xloft.converters import to_human_size
16
17
 
17
18
  from ramifice.fields.general.field import Field
18
19
  from ramifice.fields.general.file_group import FileGroup
@@ -160,7 +161,7 @@ class ImageField(Field, FileGroup, JsonMixin):
160
161
  """ # noqa: D205
161
162
  base64_str = base64_str or None
162
163
  filename = filename or None
163
- img_info: dict[str, str | int | bool] = {"save_as_is": False}
164
+ img_info: dict[str, Any] = {"save_as_is": False}
164
165
  img_info["is_new_img"] = True
165
166
  img_info["is_delete"] = is_delete
166
167
 
@@ -181,24 +182,30 @@ class ImageField(Field, FileGroup, JsonMixin):
181
182
  # Create the current date for the directory name.
182
183
  date_str: str = str(datetime.now(UTC_TIMEZONE).date())
183
184
  # Directory name for the original image and its thumbnails.
184
- general_dir = uuid.uuid4()
185
+ general_dir = str(uuid.uuid4())
185
186
  # Create path to target directory with images.
186
- imgs_dir_path = f"{MEDIA_ROOT}/uploads/{self.target_dir}/{date_str}/{general_dir}"
187
+ imgs_dir_path = Path(
188
+ MEDIA_ROOT,
189
+ "uploads",
190
+ self.target_dir,
191
+ date_str,
192
+ general_dir,
193
+ )
194
+ # Create target directory if it does not exist.
195
+ if not await imgs_dir_path.exists():
196
+ await imgs_dir_path.mkdir(parents=True)
187
197
  # Create url path to target directory with images.
188
198
  imgs_dir_url = f"{MEDIA_URL}/uploads/{self.target_dir}/{date_str}/{general_dir}"
189
199
  # Create a new name for the original image.
190
200
  new_original_name = f"original{extension}"
191
201
  # Create path to main image.
192
- main_img_path = f"{imgs_dir_path}/{new_original_name}"
193
- # Create target directory if it does not exist.
194
- if not await to_thread.run_sync(exists, imgs_dir_path):
195
- await to_thread.run_sync(makedirs, imgs_dir_path)
202
+ main_img_path = Path(imgs_dir_path, new_original_name)
196
203
  # Save main image in target directory.
197
204
  async with await open_file(main_img_path, mode="wb") as open_f:
198
205
  f_content = b64decode(base64_str)
199
206
  await open_f.write(f_content)
200
207
  # Add paths for main image.
201
- img_info["path"] = main_img_path
208
+ img_info["path"] = main_img_path.as_posix()
202
209
  img_info["url"] = f"{imgs_dir_url}/{new_original_name}"
203
210
  # Add original image name.
204
211
  img_info["name"] = filename
@@ -210,11 +217,14 @@ class ImageField(Field, FileGroup, JsonMixin):
210
217
  ext_upper = "JPEG"
211
218
  img_info["ext_upper"] = ext_upper
212
219
  # Add path to target directory with images.
213
- img_info["imgs_dir_path"] = imgs_dir_path
220
+ img_info["imgs_dir_path"] = imgs_dir_path.as_posix()
214
221
  # Add url path to target directory with images.
215
222
  img_info["imgs_dir_url"] = imgs_dir_url
216
223
  # Add size of main image (in bytes).
217
224
  img_info["size"] = await to_thread.run_sync(getsize, main_img_path)
225
+ # Convert the number of bytes into a human-readable format.
226
+ # Examples: 200 bytes | 1 KB | 1.5 MB.
227
+ img_info["human_size"] = to_human_size(img_info["size"])
218
228
  #
219
229
  # to value.
220
230
  self.value = img_info
@@ -226,7 +236,7 @@ class ImageField(Field, FileGroup, JsonMixin):
226
236
  ) -> None:
227
237
  """Get image information and copy the image to the target directory."""
228
238
  src_path = src_path or None
229
- img_info: dict[str, str | int | bool] = {"save_as_is": False}
239
+ img_info: dict[str, Any] = {"save_as_is": False}
230
240
  img_info["is_new_img"] = True
231
241
  img_info["is_delete"] = is_delete
232
242
 
@@ -240,18 +250,24 @@ class ImageField(Field, FileGroup, JsonMixin):
240
250
  # Create the current date for the directory name.
241
251
  date_str: str = str(datetime.now(UTC_TIMEZONE).date())
242
252
  # Directory name for the original image and its thumbnails.
243
- general_dir = uuid.uuid4()
253
+ general_dir = str(uuid.uuid4())
244
254
  # Create path to target directory with images.
245
- imgs_dir_path = f"{MEDIA_ROOT}/uploads/{self.target_dir}/{date_str}/{general_dir}"
255
+ imgs_dir_path = Path(
256
+ MEDIA_ROOT,
257
+ "uploads",
258
+ self.target_dir,
259
+ date_str,
260
+ general_dir,
261
+ )
246
262
  # Create url path to target directory with images.
247
263
  imgs_dir_url = f"{MEDIA_URL}/uploads/{self.target_dir}/{date_str}/{general_dir}"
264
+ # Create target directory if it does not exist.
265
+ if not await imgs_dir_path.exists():
266
+ await imgs_dir_path.mkdir(parents=True)
248
267
  # Create a new name for the original image.
249
268
  new_original_name = f"original{extension}"
250
269
  # Create path to main image.
251
- main_img_path = f"{imgs_dir_path}/{new_original_name}"
252
- # Create target directory if it does not exist.
253
- if not await to_thread.run_sync(exists, imgs_dir_path):
254
- await to_thread.run_sync(makedirs, imgs_dir_path)
270
+ main_img_path = f"{imgs_dir_path.as_posix()}/{new_original_name}"
255
271
  # Save main image in target directory.
256
272
  await to_thread.run_sync(copyfile, src_path, main_img_path)
257
273
  # Add paths for main image.
@@ -267,11 +283,14 @@ class ImageField(Field, FileGroup, JsonMixin):
267
283
  ext_upper = "JPEG"
268
284
  img_info["ext_upper"] = ext_upper
269
285
  # Add path to target directory with images.
270
- img_info["imgs_dir_path"] = imgs_dir_path
286
+ img_info["imgs_dir_path"] = imgs_dir_path.as_posix()
271
287
  # Add url path to target directory with images.
272
288
  img_info["imgs_dir_url"] = imgs_dir_url
273
289
  # Add size of main image (in bytes).
274
290
  img_info["size"] = await to_thread.run_sync(getsize, main_img_path)
291
+ # Convert the number of bytes into a human-readable format.
292
+ # Examples: 200 bytes | 1 KB | 1.5 MB.
293
+ img_info["human_size"] = to_human_size(img_info["size"])
275
294
  #
276
295
  # to value.
277
296
  self.value = img_info
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ramifice
3
- Version: 0.8.43
3
+ Version: 0.8.45
4
4
  Summary: ORM-pseudo-like API MongoDB for Python language.
5
5
  Project-URL: Homepage, https://github.com/kebasyaty/ramifice
6
6
  Project-URL: Documentation, https://kebasyaty.github.io/ramifice/
@@ -25,10 +25,10 @@ ramifice/fields/color_field.py,sha256=eTWSqin6ZM1V6U9PRfEphuCdfNajFiT4nInorelWLq
25
25
  ramifice/fields/date_field.py,sha256=p-pMTDpIWrK0SqUk8FVnribIEmQk37modEveytOJuKA,6255
26
26
  ramifice/fields/date_time_field.py,sha256=UZLl8zvwLfytJDmrTIhpbMJVHHjg6AU_p9U0O1E0f34,6312
27
27
  ramifice/fields/email_field.py,sha256=7T1ks9Plusr-fon1wa9rO7S6_Zj7J9ZmwE504UFDHx8,4427
28
- ramifice/fields/file_field.py,sha256=G510AuGjtMerHjpUpIV6tYRShTBkNzWcI3hD_vqAGyQ,9532
28
+ ramifice/fields/file_field.py,sha256=EOFfOswvmX9u4Gmj_2uGFFrNTX-PXEVAStgZ7AIS88M,10071
29
29
  ramifice/fields/float_field.py,sha256=XtyaTgAFaTem4Hv7c0G_l6LIrspraijqkEqE8G7gEE0,5834
30
30
  ramifice/fields/id_field.py,sha256=pNDQRmqcsDtWVC2zW310T_3tqxpM-d3sd1elwxL14a8,4795
31
- ramifice/fields/image_field.py,sha256=LZqDXgRLXljNUqqMSpvfPJwwiNnLB2PpSQ8F8SBNg_g,12904
31
+ ramifice/fields/image_field.py,sha256=llvPLWBLHsVoU6z_7DE3SMeW-GZenyzPeXhRH5ptDZc,13503
32
32
  ramifice/fields/integer_field.py,sha256=kZohnGFJb9YFiJ3Fl-YnPQJW1YBCAEzyavY23hl9jxc,5808
33
33
  ramifice/fields/ip_field.py,sha256=FGf2Bnz87k9HRDY3SZ92OKdRb_8Z34iVq_UnP-fb7ew,4331
34
34
  ramifice/fields/password_field.py,sha256=aBY8XaRCTqHGi7LfCfImoaUZuPrXmvDwquGjEkCda0A,3958
@@ -77,7 +77,7 @@ ramifice/utils/mixins.py,sha256=5G3x8hK3igM7JBgCiCnVDdb_0K_D1gV1tw0QKpDlm-A,1161
77
77
  ramifice/utils/tools.py,sha256=WOazw24pvL96-0yNBy7YRGErN3vAQ4F6GS9TLvWFqrs,2747
78
78
  ramifice/utils/translations.py,sha256=YItvCWv-13FS2x8VYsAOH-Nx_qQOitOX7-bRGX3mc3A,4718
79
79
  ramifice/utils/unit.py,sha256=zHfvkyEU4sxHOc-QMuntEcvyfQqH75AK1zx8E25kM9Y,2535
80
- ramifice-0.8.43.dist-info/METADATA,sha256=Guw-rKPVol_ea-Ns3fGzE1i_hxbMAwpgXDiM01iRKjQ,18899
81
- ramifice-0.8.43.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
82
- ramifice-0.8.43.dist-info/licenses/LICENSE,sha256=LrEL0aTZx90HDwFUQCJutORiDjJL9AnuVvCtspXIqt4,1095
83
- ramifice-0.8.43.dist-info/RECORD,,
80
+ ramifice-0.8.45.dist-info/METADATA,sha256=BP_QaIlwdovrMtKs_69atBK6TLuwhZxE6WWK4pMM9AY,18899
81
+ ramifice-0.8.45.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
82
+ ramifice-0.8.45.dist-info/licenses/LICENSE,sha256=LrEL0aTZx90HDwFUQCJutORiDjJL9AnuVvCtspXIqt4,1095
83
+ ramifice-0.8.45.dist-info/RECORD,,