update to user upload
This commit is contained in:
@ -2,7 +2,7 @@ from django.contrib import admin
|
|||||||
from leaflet.admin import LeafletGeoAdmin
|
from leaflet.admin import LeafletGeoAdmin
|
||||||
from leaflet.admin import LeafletGeoAdminMixin
|
from leaflet.admin import LeafletGeoAdminMixin
|
||||||
from leaflet_admin_list.admin import LeafletAdminListMixin
|
from leaflet_admin_list.admin import LeafletAdminListMixin
|
||||||
from .models import RogUser, Location, SystemSettings, JoinedEvent, Favorite, TravelList, TravelPoint, ShapeLayers, Event, Location_line, Location_polygon, JpnAdminMainPerf, JpnAdminPerf, JpnSubPerf, Useractions, CustomUser, GifuAreas, UserTracks, ShapeFileLocations, templocation
|
from .models import RogUser, Location, SystemSettings, JoinedEvent, Favorite, TravelList, TravelPoint, ShapeLayers, Event, Location_line, Location_polygon, JpnAdminMainPerf, JpnAdminPerf, JpnSubPerf, Useractions, CustomUser, GifuAreas, UserTracks, ShapeFileLocations, templocation, UserUpload, UserUploadUser
|
||||||
from django.contrib.auth.admin import UserAdmin
|
from django.contrib.auth.admin import UserAdmin
|
||||||
|
|
||||||
class RogAdmin(LeafletAdminListMixin, LeafletGeoAdminMixin, admin.ModelAdmin):
|
class RogAdmin(LeafletAdminListMixin, LeafletGeoAdminMixin, admin.ModelAdmin):
|
||||||
@ -161,6 +161,8 @@ admin.site.register(UserTracks, LeafletGeoAdmin);
|
|||||||
admin.site.register(GifuAreas, LeafletGeoAdmin)
|
admin.site.register(GifuAreas, LeafletGeoAdmin)
|
||||||
admin.site.register(JpnSubPerf, LeafletGeoAdmin)
|
admin.site.register(JpnSubPerf, LeafletGeoAdmin)
|
||||||
admin.site.register(ShapeLayers, admin.ModelAdmin)
|
admin.site.register(ShapeLayers, admin.ModelAdmin)
|
||||||
|
admin.site.register(UserUpload, admin.ModelAdmin)
|
||||||
|
#admin.site.register(UserUploadUser, admin.ModelAdmin)
|
||||||
#admin.site.register(ShapeFileLocations, admin.ModelAdmin)
|
#admin.site.register(ShapeFileLocations, admin.ModelAdmin)
|
||||||
|
|
||||||
admin.site.register(CustomUser, UserAdminConfig)
|
admin.site.register(CustomUser, UserAdminConfig)
|
||||||
|
|||||||
23
rog/migrations/0025_userupload.py
Normal file
23
rog/migrations/0025_userupload.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 3.2.9 on 2022-08-30 05:26
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import rog.models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('rog', '0024_customuser_group'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='UserUpload',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=255, verbose_name='User uploads')),
|
||||||
|
('file', models.FileField(blank=True, upload_to=rog.models.get_file_path)),
|
||||||
|
('uploaded_date', models.DateField(auto_now_add=True)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
21
rog/migrations/0026_useruploaduser.py
Normal file
21
rog/migrations/0026_useruploaduser.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Generated by Django 3.2.9 on 2022-08-30 10:05
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('rog', '0025_userupload'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='UserUploadUser',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('userfile', models.CharField(blank=True, max_length=2048, null=True, verbose_name='User file')),
|
||||||
|
('email', models.CharField(max_length=255, verbose_name='User Email')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
18
rog/migrations/0027_alter_customuser_group.py
Normal file
18
rog/migrations/0027_alter_customuser_group.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 3.2.9 on 2022-08-30 10:22
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('rog', '0026_useruploaduser'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='customuser',
|
||||||
|
name='group',
|
||||||
|
field=models.CharField(choices=[('G1', '大垣-初心者'), ('G2', '大垣-3時間'), ('G3', '大垣-5時間')], default='G1', max_length=255),
|
||||||
|
),
|
||||||
|
]
|
||||||
18
rog/migrations/0028_alter_customuser_group.py
Normal file
18
rog/migrations/0028_alter_customuser_group.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 3.2.9 on 2022-08-30 10:58
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('rog', '0027_alter_customuser_group'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='customuser',
|
||||||
|
name='group',
|
||||||
|
field=models.CharField(choices=[('大垣-初心者', '大垣-初心者'), ('大垣-3時間', '大垣-3時間'), ('大垣-5時間', '大垣-5時間')], default='大垣-初心者', max_length=255),
|
||||||
|
),
|
||||||
|
]
|
||||||
129
rog/models.py
129
rog/models.py
@ -1,4 +1,5 @@
|
|||||||
from dataclasses import field
|
from dataclasses import field
|
||||||
|
import email
|
||||||
from pyexpat import model
|
from pyexpat import model
|
||||||
from sre_constants import CH_LOCALE
|
from sre_constants import CH_LOCALE
|
||||||
from typing import ChainMap
|
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"))
|
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):
|
class CustomUserManager(BaseUserManager):
|
||||||
|
|
||||||
def create_user(self, email, password, group, **other_fields):
|
def create_user(self, email, password, group, **other_fields):
|
||||||
@ -136,17 +163,29 @@ class GifuAreas(models.Model):
|
|||||||
db_table = 'gifu_areas'
|
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 CustomUser(AbstractBaseUser, PermissionsMixin):
|
||||||
class Groups(models.TextChoices):
|
class Groups(models.TextChoices):
|
||||||
GB1 = 'G1', '大垣-初心者'
|
GB1 = '大垣-初心者', '大垣-初心者'
|
||||||
GB2 = 'G2', '大垣-3時間'
|
GB2 = '大垣-3時間', '大垣-3時間'
|
||||||
GB3 = 'G3', '大垣-5時間'
|
GB3 = '大垣-5時間', '大垣-5時間'
|
||||||
|
|
||||||
email = models.EmailField(_("email address"), unique=True)
|
email = models.EmailField(_("email address"), unique=True)
|
||||||
is_staff = models.BooleanField(default=False)
|
is_staff = models.BooleanField(default=False)
|
||||||
is_active = models.BooleanField(default=True)
|
is_active = models.BooleanField(default=True)
|
||||||
group = models.CharField(max_length=2,
|
group = models.CharField(max_length=255,
|
||||||
choices=Groups.choices,
|
choices=Groups.choices,
|
||||||
default=Groups.GB1)
|
default=Groups.GB1)
|
||||||
USERNAME_FIELD = 'email'
|
USERNAME_FIELD = 'email'
|
||||||
@ -504,12 +543,6 @@ def getTempMappingforModel(tbl, shp):
|
|||||||
return LayerMapping(Location_polygon, shp, location_polygon_mapping, transform=False)
|
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):
|
class ShapeLayers(models.Model):
|
||||||
name = models.CharField(_("Shape Layer"), max_length=255)
|
name = models.CharField(_("Shape Layer"), max_length=255)
|
||||||
file = models.FileField(upload_to=get_file_path, blank=True)
|
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)
|
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)
|
@receiver(pre_save, sender=Location)
|
||||||
def location_presave(sender, instance, *args, **kwargs):
|
def location_presave(sender, instance, *args, **kwargs):
|
||||||
#print("------############------------", instance.location_id)
|
#print("------############------------", instance.location_id)
|
||||||
@ -655,8 +667,14 @@ def publish_data(sender, instance, created, **kwargs):
|
|||||||
|
|
||||||
|
|
||||||
def insertShapeLayerLocation(name, fields):
|
def insertShapeLayerLocation(name, fields):
|
||||||
sll = ShapeFileLocations(shapefile=name, locid=fields[0])
|
sll = UserUploadUser(userfile=name, email=fields[0])
|
||||||
sll.save();
|
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 '',
|
tags=fields[19] if len(fields) > 19 else '',
|
||||||
parammeters=fields[20] if len(fields) > 20 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)
|
||||||
|
|||||||
7
users.csv
Normal file
7
users.csv
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
z@z.com,z,大垣-初心者
|
||||||
|
x@x.com,x,大垣-3時間
|
||||||
|
c@c.com,c,大垣-5時間
|
||||||
|
v@v.com,v,大垣-初心者
|
||||||
|
b@b.com,b',大垣-3時間
|
||||||
|
n@n.com,n,大垣-5時間
|
||||||
|
m@m.com,m,大垣-初心者
|
||||||
|
Reference in New Issue
Block a user