Java拖曳鼠標完成畫線功效的辦法。本站提示廣大學習愛好者:(Java拖曳鼠標完成畫線功效的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java拖曳鼠標完成畫線功效的辦法正文
django後台批量導入數據
在臨盆情況中,常常數據不是幾條或許幾百條,那末舉個例子,將公司一切員工員工號或許帳號暗碼導入進後台,那就不建議你去後台一條筆記錄去添加了
若何從xml中批量導入svn記載
第一步:
為數據樹立模子
@python_2_unicode_compatible class SVNLog(models.Model): vision = models.IntegerField(verbose_name=u"修訂版本", blank=False, null=False,) author = models.CharField(verbose_name=u"作者", max_length=60, blank=True, null=True) date = models.DateTimeField(verbose_name=u"修訂時光",null=True ) msg = models.TextField(verbose_name=u"正文新聞", blank=False, null=False, default=u"") paths = models.TextField(verbose_name=u"影響的文件", blank=False, null=False, default=u"") created_time = models.DateTimeField(verbose_name=u"創立時光", auto_now_add=True, ) update_time = models.DateTimeField(verbose_name=u"修正時光", auto_now=True, ) class Meta: ordering = ['revision'] def __str__(self): return u'r%s' % (self.revision or u"", )
既然樹立好了模子,那我們再去樹立接收我們xml文件的models
@python_2_unicode_compatible class ImportLogFile(models.Model): LogFile = models.FileField(upload_to='LogFile') FileName = models.CharField(max_length=50, verbose_name=u'文件名') class Meta: ordering = ['FileName'] def __str__(self): return self.FileName
ok,以上代碼我們界說好了數據和上傳文件的model
同步數據庫
python manage.py makemigrations python manage.py migrate
接著我們去修正admin.py 讓我們可以從後台上傳文件,
class ImportLogAdmin(admin.ModelAdmin): list_display = ('LogFile','FileName',) list_filter = ['FileName',] def save_model(self, request, obj, form, change): re = super(YDImportLogAdmin,self).save_model(request, obj, form, change) update_svn_log(self, request, obj, change) return re
留意下面代碼裡的save_model,這裡才是症結,在這裡我重寫了ModelAdmin裡的save_model辦法
由於我們要把上傳文件,讀取文件,解析文件,操作數據庫合為一步來操作,年夜家可以翻開debug,在上傳文件的時刻,前往參數的obj裡包含了文件上傳的途徑,這個途徑也是下一步我們操作解析文件的症結,好了我們在這個app文件夾下新建一個utils.py 用來操作我們操作文件和數據庫的對象類,為了簡略我寫成了函數以下
先貼一下我們要測試的xml文件
<?xml version="1.0" encoding="UTF-8"?> <log> <logentry revision="2"> <author>qwert</author> <date>2016-09-27T07:16:37.396449Z</date> <paths> <path action="A" prop-mods="false" text-mods="true" kind="file">/aaa/README </path> </paths> <msg>20160927 151630</msg> </logentry> <logentry revision="1"> <author>VisualSVN Server</author> <date>2016-09-20T05:03:12.861315Z</date> <paths> <path action="A" prop-mods="false" text-mods="false" kind="dir">/branches</path> <path action="A" prop-mods="false" text-mods="false" kind="dir">/tags</path> <path action="A" prop-mods="false" text-mods="false" kind="dir">/trunk</path> </paths> <msg>hello word</msg> </logentry> </log>
輸入成果格局
r2 | qwer | 2016-09-27 15:16:37 +0800 (二, 27 9 2016) | 1 line Changed paths: A /xxx/README 20160927 151630 ------------------------------------------------------------------------ r1 | VisualSVN Server | 2016-09-20 13:03:12 +0800 (二, 20 9 2016) | 1 line Changed paths: A /branches A /tags A /trunk Initial structure. from .models import SVNLog import xmltodict def update_svn_log(self, request, obj, change): headers = ['r','a','d','m','p'] filepath = obj.LogFile.path xmlfile = xmltodict.parse(open(filepath, 'r')) xml_logentry = xml.get('log').get('logentry') info_list = [] pathlist = [] sql_insert_list = [] sql_update_list = [] for j in xml: data_dict = {} # get path paths = j.get('paths').get('path') if isinstance(paths,list): for path in paths: action = path.get('@action') pathtext = path.get('#text') pathtext = action + ' ' + pathtext pathlist.append(pathtext) _filelist = u'\n'.join(pathlist) _paths = u"Changed paths:\n {}".format(_filelist) print _paths else: _filelist = paths.get('@action') + ' ' + paths.get('#text') _paths = u"Changed paths:\n {}".format(_filelist) print _paths # get revision vision = j.get('@vision') # get auth author = j.get('author') #get date date = j.get('date') #get msg msg = j.get('msg') data_dict[headers[0]] = int(vision) data_dict[headers[1]] = author data_dict[headers[2]] = date data_dict[headers[3]] = msg data_dict[headers[4]] = _paths info_list.append(data_dict) _svnlog = SVNLog.objects.filter().order_by('-vision').first() _last_version = _svnlog.vision if _svnlog else 0 for value in info_list: vision = value['r'] author = value['a'] date = value['d'] msg = value['m'] paths = value['p'] print vision,author _svnlog = YDSVNLog.objects.filter().order_by('-revision').first() _last_version = _svnlog.revision if _svnlog else 0 if vision > _last_version: sql_insert_list.append(SVNLog(revision=revision, author=author, date=date, msg = msg, paths = paths)) else: sql_update_list.append(SVNLog(revision=revision, author=author, date=date, msg = msg, paths = paths)) SVNLog.objects.bulk_create(sql_insert_list) SVNLog.objects.bulk_create(sql_update_list)
我們應用的xmltodict這個第三方庫來解析xml,他把內容解析成了高效力的orderdict類型,就是有序列的字典
這個xml中比擬龐雜的是誰人paths裡的path,由於這個xml中包括兩個元素,第一個元素的path只含有一個path,第二個元素中的paths包括有三個path,是以我們在解析獲得的時刻須要斷定一下
paths = j.get('paths').get('path') if isinstance(paths,list): pass
我們斷定這個path是否是一個list類型的,假如是,那我們就依照list的方法來處置,假如不是,那我們就按單個的方法來處置,獲得以後依照輸入成果格局處置下成果然後獲得其他內容
revision = j.get('@vision') # get auth author = j.get('author') #get date date = j.get('date') #get msg msg = j.get('msg')
最初我們將獲得到的元素存在字典裡
在輪回中斷定以後的版本號和數據庫中的版本號,
假如比本來的小,那末我們履行更新操作,反之履行拔出操作
最初應用了bulk_create來操作數據庫,如許防止了輪回中每次都停止數據庫操作形成的資本糟蹋