update to user upload

This commit is contained in:
Mohamed Nouffer
2022-08-30 18:20:15 +05:30
parent 8cb73b9890
commit 51202e8166
7 changed files with 187 additions and 33 deletions

View File

@ -1,4 +1,5 @@
from dataclasses import field
import email
from pyexpat import model
from sre_constants import CH_LOCALE
from typing import ChainMap
@ -33,6 +34,32 @@ environ.Env.read_env(env_file=".env")
db = Db(dbname=env("POSTGRES_DBNAME"), user=env("POSTGRES_USER"), password=env("POSTGRES_PASS"), host="postgres-db", port=env("PG_PORT"))
def get_file_path(instance, filename):
ext = filename.split('.')[-1]
filename = "%s/%s.%s" % (uuid.uuid4(), uuid.uuid4(), ext)
return os.path.join('uploads/geoms', filename)
def remove_bom_inplace(path):
"""Removes BOM mark, if it exists, from a file and rewrites it in-place"""
buffer_size = 4096
bom_length = len(codecs.BOM_UTF8)
with open(path, mode="r+b") as fp:
chunk = fp.read(buffer_size)
if chunk.startswith(codecs.BOM_UTF8):
i = 0
chunk = chunk[bom_length:]
while chunk:
fp.seek(i)
fp.write(chunk)
i += len(chunk)
fp.seek(bom_length, os.SEEK_CUR)
chunk = fp.read(buffer_size)
fp.seek(-bom_length, os.SEEK_CUR)
fp.truncate()
class CustomUserManager(BaseUserManager):
def create_user(self, email, password, group, **other_fields):
@ -136,17 +163,29 @@ class GifuAreas(models.Model):
db_table = 'gifu_areas'
class UserUpload(models.Model):
name = models.CharField(_("User uploads"), max_length=255)
file = models.FileField(upload_to=get_file_path, blank=True)
uploaded_date = models.DateField(auto_now_add=True)
def __str__(self):
return self.name
class UserUploadUser(models.Model):
userfile=models.CharField(_('User file'), max_length=2048 ,blank=True, null=True)
email=models.CharField(_('User Email'), max_length=255)
class CustomUser(AbstractBaseUser, PermissionsMixin):
class Groups(models.TextChoices):
GB1 = 'G1', '大垣-初心者'
GB2 = 'G2', '大垣-3時間'
GB3 = 'G3', '大垣-5時間'
GB1 = '大垣-初心者', '大垣-初心者'
GB2 = '大垣-3時間', '大垣-3時間'
GB3 = '大垣-5時間', '大垣-5時間'
email = models.EmailField(_("email address"), unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
group = models.CharField(max_length=2,
group = models.CharField(max_length=255,
choices=Groups.choices,
default=Groups.GB1)
USERNAME_FIELD = 'email'
@ -504,12 +543,6 @@ def getTempMappingforModel(tbl, shp):
return LayerMapping(Location_polygon, shp, location_polygon_mapping, transform=False)
def get_file_path(instance, filename):
ext = filename.split('.')[-1]
filename = "%s/%s.%s" % (uuid.uuid4(), uuid.uuid4(), ext)
return os.path.join('uploads/geoms', filename)
class ShapeLayers(models.Model):
name = models.CharField(_("Shape Layer"), max_length=255)
file = models.FileField(upload_to=get_file_path, blank=True)
@ -525,27 +558,6 @@ class ShapeFileLocations(models.Model):
locid=models.IntegerField(blank=True, null=True)
def remove_bom_inplace(path):
"""Removes BOM mark, if it exists, from a file and rewrites it in-place"""
buffer_size = 4096
bom_length = len(codecs.BOM_UTF8)
with open(path, mode="r+b") as fp:
chunk = fp.read(buffer_size)
if chunk.startswith(codecs.BOM_UTF8):
i = 0
chunk = chunk[bom_length:]
while chunk:
fp.seek(i)
fp.write(chunk)
i += len(chunk)
fp.seek(bom_length, os.SEEK_CUR)
chunk = fp.read(buffer_size)
fp.seek(-bom_length, os.SEEK_CUR)
fp.truncate()
@receiver(pre_save, sender=Location)
def location_presave(sender, instance, *args, **kwargs):
#print("------############------------", instance.location_id)
@ -655,8 +667,14 @@ def publish_data(sender, instance, created, **kwargs):
def insertShapeLayerLocation(name, fields):
sll = ShapeFileLocations(shapefile=name, locid=fields[0])
sll = UserUploadUser(userfile=name, email=fields[0])
sll.save();
def insertUserUploadUser(name, fields):
with transaction.atomic():
sll = UserUploadUser(userfile=name, email=fields[0])
sll.save()
@ -762,3 +780,50 @@ def updatePolygonTable(mdl, fields):
tags=fields[19] if len(fields) > 19 else '',
parammeters=fields[20] if len(fields) > 20 else ''
)
def createUser(fields):
with transaction.atomic():
user = CustomUser.objects.create_user(email=fields[0], password=fields[1], group=fields[2])
user.is_superuser = False
user.is_staff = False
user.save()
@receiver(post_delete,sender=UserUpload)
def deleteUserUploadUser(sender,instance,*args,**kwargs):
pass
emails = UserUploadUser.objects.filter(userfile=instance.name).values_list('email')
print("------- email----")
print(emails)
print("------- name----")
CustomUser.objects.filter(email__in=emails).delete()
templocation.objects.all().delete()
UserUploadUser.objects.filter(userfile=instance.name).delete();
@receiver(post_save, sender=UserUpload)
def publish_data(sender, instance, created, **kwargs):
file = instance.file.path
#os.remove(file)
try:
#csv_f = glob.glob(r'{}/**/*.csv'.format(file), recursive=True)[0]
remove_bom_inplace(file)
print(file)
with open(file, mode="r", encoding="utf-8") as txt_file:
#heading = next(txt_file)
reader = csv.reader(txt_file, delimiter=",")
for fields in reader:
print("@@@@@@@@@@@@")
print(fields[0])
print("@@@@@@@@@@@@")
createUser(fields)
with open(file, mode="r", encoding="utf-8") as txt_file:
reader_2 = csv.reader(txt_file, delimiter=",")
for fields in reader_2:
insertUserUploadUser(instance.name, fields)
except Exception as e:
print('######## user csv file ##########',e)