Skip to content

Commit

Permalink
feat: 升级支持python3,以及django3+
Browse files Browse the repository at this point in the history
  • Loading branch information
rayer4u committed Dec 18, 2020
1 parent 6f5484a commit ae95229
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 137 deletions.
21 changes: 11 additions & 10 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#coding:utf-8
# coding:utf-8
from django.conf import settings
from os.path import splitext,join,dirname
from os.path import splitext, join, dirname
import os
import ConfigParser
import configparser

PACKAGE_DIR="package"
UPLOAD_DIR="upload"
PROFILES_DIR=join(settings.MEDIA_ROOT, "profiles")
CERTS={}

PACKAGE_DIR = "package"
UPLOAD_DIR = "upload"
PROFILES_DIR = join(settings.MEDIA_ROOT, "profiles")
CERTS = {}

for root, dirs, files in os.walk(PROFILES_DIR):
if root == PROFILES_DIR:
for dir in dirs:
CERTS[dir] = ''
else:
root = root[len(PROFILES_DIR)+1:]
root = root[len(PROFILES_DIR) + 1:]
if root not in CERTS:
continue
if 'cert.cfg' not in files:
continue
cf = ConfigParser.SafeConfigParser()

cf = configparser.SafeConfigParser()
cf.read(join(PROFILES_DIR, root, 'cert.cfg'))
if 'certification' in cf.sections():
CERTS[root] = dict(cf.items('certification'))
Expand Down
16 changes: 10 additions & 6 deletions admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.contrib import admin
from models import UpFile
from .models import UpFile
import os


class PathFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
Expand All @@ -12,8 +13,9 @@ class PathFilter(admin.SimpleListFilter):

def lookups(self, request, model_admin):
qs = model_admin.get_queryset(request)

ret = [(b, b) for b in set((os.path.dirname(a) if os.path.dirname(a) != '' else a) for a in qs.values_list('path', flat=True))]

ret = [(b, b) for b in set((os.path.dirname(a) if os.path.dirname(
a) != '' else a) for a in qs.values_list('path', flat=True))]
print(ret)
return ret

Expand All @@ -23,10 +25,12 @@ def queryset(self, request, queryset):
if self.value() is not None:
print(self.value())
return queryset.filter(path__startswith=self.value(),)



class UpFileAdmin(admin.ModelAdmin):
list_display = ('path', 'label')
list_editable = ('label',)
list_filter = ('user', PathFilter)

list_filter = ('user', PathFilter)


admin.site.register(UpFile, UpFileAdmin)
6 changes: 3 additions & 3 deletions forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django import forms
from models import UpFile

from .models import UpFile


class UploadModelFileForm(forms.ModelForm):
class Meta:
model = UpFile
exclude = ('pub', 'plist', 'status', 'up_date', 'from_ip')

34 changes: 34 additions & 0 deletions migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.0.11 on 2020-12-17 09:16

from django.db import migrations, models
import ipapub.contenttyperestrictedfilefield
import ipapub.models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='UpFile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('path', models.CharField(max_length=200)),
('file', ipapub.contenttyperestrictedfilefield.ContentTypeRestrictedFileField(blank=True, null=True, upload_to=ipapub.models.PathAndRename2('upload'))),
('icons', ipapub.contenttyperestrictedfilefield.ContentTypeRestrictedFileField(upload_to=ipapub.models.PathAndRename('package'))),
('iconb', ipapub.contenttyperestrictedfilefield.ContentTypeRestrictedFileField(upload_to=ipapub.models.PathAndRename('package'))),
('plist', models.FileField(upload_to='package')),
('pub', models.FileField(upload_to='package')),
('signed', ipapub.contenttyperestrictedfilefield.ContentTypeRestrictedFileField(blank=True, null=True, upload_to=ipapub.models.PathAndRename('package'))),
('status', models.CharField(blank=True, max_length=10)),
('user', models.CharField(max_length=10)),
('label', models.CharField(blank=True, max_length=200)),
('up_date', models.DateTimeField(auto_now_add=True, verbose_name='upload date')),
('from_ip', models.GenericIPAddressField(blank=True, null=True)),
],
),
]
Empty file added migrations/__init__.py
Empty file.
109 changes: 65 additions & 44 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,88 @@
# -*- coding: utf-8 -*-

import os,ipapub
import os
import ipapub

from django.db import models
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
from django.utils.deconstruct import deconstructible
from uuid import uuid4
from contenttyperestrictedfilefield import ContentTypeRestrictedFileField
from .contenttyperestrictedfilefield import ContentTypeRestrictedFileField


def random_path(path):
def wrapper(instance, filename):
ext = ".".join(filename.split('.')[1:])
# get filename
@deconstructible
class PathAndRename(object):

def __init__(self, sub_path):
self.path = sub_path

def __call__(self, instance, filename):
ext = filename.split('.')[-1]
# set filename as random string
if instance.pk:
ext = filename.split('.')[-1]
filename = '{0}.{1}'.format(instance.pk, ext)
else:
# set filename as random string
filename = '{0}.{1}'.format(uuid4().hex, ext)
# set filename with path
filename = os.path.join(os.path.dirname(instance.path), filename)
# return the whole path to the file
return os.path.join(path, filename)
return wrapper
return os.path.join(self.path, filename)


icon_path = PathAndRename(ipapub.PACKAGE_DIR)


@deconstructible
class PathAndRename2(object):

def icon_path(path):
def wrapper(instance, filename):
# get filename
def __init__(self, sub_path):
self.path = sub_path

def __call__(self, instance, filename):
ext = filename.split('.')[-1]
# set filename as random string
if instance.pk:
ext = filename.split('.')[-1]
filename = '{0}.{1}'.format(instance.pk, ext)
else:
# set filename with path
filename = os.path.join(os.path.dirname(instance.path), filename)
# set filename as random string
filename = '{0}.{1}'.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(path, filename)
return wrapper
return os.path.join(self.path, filename)


random_path = PathAndRename2(ipapub.UPLOAD_DIR)


class UpFile(models.Model):
path = models.CharField(max_length=200) #上传路径
path = models.CharField(max_length=200) # 上传路径
# file = models.FileField(upload_to=path_and_rename(ipapub.UPLOAD_DIR))
file = ContentTypeRestrictedFileField(content_types=['application/x-gtar'],
max_upload_size=104857600,
upload_to=random_path(ipapub.UPLOAD_DIR), blank=True, null=True) #上传的文件
icons = ContentTypeRestrictedFileField(content_types=['image/png'],
max_upload_size=2621440,
upload_to=icon_path(ipapub.PACKAGE_DIR)) #小图标
iconb = ContentTypeRestrictedFileField(content_types=['image/png'],
max_upload_size=2621440,
upload_to=icon_path(ipapub.PACKAGE_DIR)) #大图标
plist = models.FileField(upload_to=ipapub.PACKAGE_DIR) #plist文件
pub = models.FileField(upload_to=ipapub.PACKAGE_DIR) #发布的文件压缩包集合
signed = ContentTypeRestrictedFileField(content_types=['application/iphone'],
max_upload_size=104857600,
upload_to=icon_path(ipapub.PACKAGE_DIR), blank=True, null=True) #签名好的文件
status = models.CharField(max_length=10, blank=True) #状态,uploaded,。。。
user = models.CharField(max_length=10, blank=False) #上传的svn用户名
label = models.CharField(max_length=200, blank=True) #标签
up_date = models.DateTimeField('upload date', auto_now_add=True) #上传的时间
from_ip = models.GenericIPAddressField(blank=True, null=True) #上传的ip
file = ContentTypeRestrictedFileField(content_types=['application/x-gtar'],
max_upload_size=104857600,
upload_to=random_path, blank=True, null=True) # 上传的文件
icons = ContentTypeRestrictedFileField(content_types=['image/png'],
max_upload_size=2621440,
upload_to=icon_path) # 小图标
iconb = ContentTypeRestrictedFileField(content_types=['image/png'],
max_upload_size=2621440,
upload_to=icon_path) # 大图标
plist = models.FileField(upload_to=ipapub.PACKAGE_DIR) # plist文件
pub = models.FileField(upload_to=ipapub.PACKAGE_DIR) # 发布的文件压缩包集合
signed = ContentTypeRestrictedFileField(content_types=['application/iphone'],
max_upload_size=104857600,
upload_to=icon_path, blank=True, null=True) # 签名好的文件
status = models.CharField(max_length=10, blank=True) # 状态,uploaded,。。。
user = models.CharField(max_length=10, blank=False) # 上传的svn用户名
label = models.CharField(max_length=200, blank=True) # 标签
up_date = models.DateTimeField('upload date', auto_now_add=True) # 上传的时间
from_ip = models.GenericIPAddressField(blank=True, null=True) # 上传的ip

def __unicode__(self):
return self.path

# These two auto-delete files from filesystem when they are unneeded:


@receiver(models.signals.post_delete, sender=UpFile)
def auto_delete_file_on_delete(sender, instance, **kwargs):
"""Deletes file from filesystem
Expand All @@ -81,29 +102,29 @@ def auto_delete_file_on_delete(sender, instance, **kwargs):
# """
# if not instance.pk:
# return False
#
#
# while True:
# try:
# old_file = UpFile.objects.get(pk=instance.pk).file
# except UpFile.DoesNotExist:
# break;
#
#
# new_file = instance.file
# if not old_file == new_file:
# if os.path.isfile(old_file.path):
# os.remove(old_file.path)
#
#
# break
#
#
# while True:
# try:
# old_file = UpFile.objects.get(pk=instance.pk).signed
# except UpFile.DoesNotExist:
# break;
#
#
# new_file = instance.signed
# if not old_file == new_file:
# if os.path.isfile(old_file.path):
# os.remove(old_file.path)
#
#
# break
2 changes: 1 addition & 1 deletion templates/ipapub/upload.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% load progress_bar %}
{% load staticfiles %}
{% load static %}

<!DOCTYPE html>
<html>
Expand Down
17 changes: 9 additions & 8 deletions urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.conf.urls import patterns, include, url
from django.urls import path
from django.views.generic import FormView
from views import upload, AllView, OneView
from forms import UploadModelFileForm
from .views import upload, AllView, OneView
from .forms import UploadModelFileForm

urlpatterns = patterns('',
url(r'^$', upload, name='upload'),
url(r'^all', AllView.as_view(), name='all'),
url(r'^(?P<path>.*)$', OneView.as_view(), name='one'),
)

urlpatterns = [
path('', upload, name='upload'),
path('all', AllView.as_view(), name='all'),
path('<path>', OneView.as_view(), name='one'),
]
Loading

0 comments on commit ae95229

Please sign in to comment.