Auto fill IntroContentInfo

Brightcells 7 gadi atpakaļ
vecāks
revīzija
693b2ddffb
5 mainītis faili ar 88 papildinājumiem un 7 dzēšanām
  1. 4 0
      .gitignore
  2. 40 2
      intro/admin.py
  3. 35 0
      intro/migrations/0002_auto_20170910_2116.py
  4. 5 3
      intro/models.py
  5. 4 2
      requirements.txt

+ 4 - 0
.gitignore

@@ -17,3 +17,7 @@ build
17 17
 # Python egg metadata, regenerated from source files by setuptools.
18 18
 /*.egg-info
19 19
 /*.egg
20
+
21
+.idea/
22
+media/
23
+collect_static/

+ 40 - 2
intro/admin.py

@@ -1,6 +1,11 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 
3
+import os
4
+
5
+import shortuuid
3 6
 from django.contrib import admin
7
+from django.core.files.storage import default_storage
8
+from pyPdf import PdfFileReader, PdfFileWriter
4 9
 
5 10
 from intro.models import IntroCatalogInfo, IntroCategoryInfo, IntroContentInfo, IntroFavoriteInfo, IntroNameInfo
6 11
 
@@ -18,13 +23,46 @@ class IntroNameInfoAdmin(admin.ModelAdmin):
18 23
 
19 24
 
20 25
 class IntroCatalogInfoAdmin(admin.ModelAdmin):
21
-    list_display = ('catalog', 'name', 'position', 'status', 'created_at', 'updated_at')
26
+    list_display = ('catalog', 'name', 'pdf', 'content', 'position', 'status', 'created_at', 'updated_at')
22 27
     list_filter = ('name', 'status')
23 28
     search_fields = ('catalog', 'name__name')
24 29
 
30
+    def save_model(self, request, obj, form, change):
31
+        obj.save()
32
+
33
+        if 'pdf' in form.changed_data or 'content' in form.changed_data:
34
+            IntroContentInfo.objects.filter(catalog=obj).delete()
35
+
36
+            inpdf = PdfFileReader(obj.pdf)
37
+            for idx in xrange(inpdf.getNumPages()):
38
+                # PDF 文件写对象
39
+                outpdf = PdfFileWriter()
40
+                # PDF 新增一页
41
+                outpdf.addPage(inpdf.getPage(idx))
42
+                # PDF 文件路径
43
+                pdfpath = 'pdf/{}.pdf'.format(shortuuid.uuid())
44
+                # PDF 文件生成
45
+                outstream = default_storage.open(pdfpath, 'wb')
46
+                outpdf.write(outstream)
47
+
48
+                content = ''
49
+                while True:
50
+                    tmpLine = obj.content.readline()
51
+                    if tmpLine == '' or tmpLine == os.linesep:
52
+                        break
53
+                    content += tmpLine
54
+
55
+                IntroContentInfo.objects.create(
56
+                    title=obj.catalog,
57
+                    pdf=pdfpath,
58
+                    content=content,
59
+                    catalog=obj,
60
+                    position=idx,
61
+                )
62
+
25 63
 
26 64
 class IntroContentInfoAdmin(admin.ModelAdmin):
27
-    list_display = ('title', 'content', 'pdf', 'catalog', 'position', 'status', 'created_at', 'updated_at')
65
+    list_display = ('title', 'pdf', 'content', 'catalog', 'position', 'status', 'created_at', 'updated_at')
28 66
     list_filter = ('catalog', 'status')
29 67
     search_fields = ('title', 'content', 'catalog__catalog')
30 68
 

+ 35 - 0
intro/migrations/0002_auto_20170910_2116.py

@@ -0,0 +1,35 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.11.3 on 2017-09-10 13:16
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+import intro.models
7
+
8
+
9
+class Migration(migrations.Migration):
10
+
11
+    dependencies = [
12
+        ('intro', '0001_initial'),
13
+    ]
14
+
15
+    operations = [
16
+        migrations.AddField(
17
+            model_name='introcataloginfo',
18
+            name='content',
19
+            field=models.FileField(blank=True, help_text='content \u6587\u4ef6', null=True, upload_to=intro.models.upload_path, verbose_name='content'),
20
+        ),
21
+        migrations.AddField(
22
+            model_name='introcataloginfo',
23
+            name='pdf',
24
+            field=models.FileField(blank=True, help_text='PDF \u6587\u4ef6', null=True, upload_to=intro.models.upload_path, verbose_name='pdf'),
25
+        ),
26
+        migrations.AlterField(
27
+            model_name='introcontentinfo',
28
+            name='content',
29
+            field=models.TextField(blank=True, help_text='\u5185\u5bb9', null=True, verbose_name='content'),
30
+        ),
31
+        migrations.AlterUniqueTogether(
32
+            name='introfavoriteinfo',
33
+            unique_together=set([('user_id', 'content')]),
34
+        ),
35
+    ]

+ 5 - 3
intro/models.py

@@ -81,6 +81,8 @@ class IntroNameInfo(CreateUpdateMixin):
81 81
 class IntroCatalogInfo(CreateUpdateMixin):
82 82
     catalog = models.CharField(_(u'catalog'), max_length=255, blank=True, null=True, help_text=u'目录')
83 83
     name = models.ForeignKey(IntroNameInfo, verbose_name=_(u'name'), blank=True, null=True, help_text=u'名称')
84
+    pdf = models.FileField(_(u'pdf'), upload_to=upload_path, blank=True, null=True, help_text=u'PDF 文件')
85
+    content = models.FileField(_(u'content'), upload_to=upload_path, blank=True, null=True, help_text=u'content 文件')
84 86
     position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')
85 87
 
86 88
     class Meta:
@@ -111,8 +113,8 @@ class IntroCatalogInfo(CreateUpdateMixin):
111 113
 
112 114
 class IntroContentInfo(CreateUpdateMixin):
113 115
     title = models.CharField(_(u'title'), max_length=255, blank=True, null=True, help_text=u'标题')
114
-    content = models.CharField(_(u'content'), max_length=255, blank=True, null=True, help_text=u'内容')
115 116
     pdf = models.FileField(_(u'pdf'), upload_to=upload_path, blank=True, null=True, help_text=u'PDF 文件')
117
+    content = models.TextField(_(u'content'), blank=True, null=True, help_text=u'内容')
116 118
     catalog = models.ForeignKey(IntroCatalogInfo, verbose_name=_(u'catalog'), blank=True, null=True, help_text=u'目录')
117 119
     position = models.IntegerField(_(u'position'), default=1, help_text=u'排序')
118 120
 
@@ -132,8 +134,8 @@ class IntroContentInfo(CreateUpdateMixin):
132 134
         return {
133 135
             'pk': self.pk,
134 136
             'title': self.title,
135
-            'content': self.content,
136 137
             'pdf_url': self.pdf_url,
138
+            'content': self.content,
137 139
         }
138 140
 
139 141
     @property
@@ -141,8 +143,8 @@ class IntroContentInfo(CreateUpdateMixin):
141 143
         return {
142 144
             'pk': self.pk,
143 145
             'title': self.title,
144
-            'content': self.content,
145 146
             'pdf_url': self.pdf_url,
147
+            'content': self.content,
146 148
             'catalog': self.catalog.data2,
147 149
         }
148 150
 

+ 4 - 2
requirements.txt

@@ -2,14 +2,16 @@ Django==1.11.3
2 2
 MySQL-python==1.2.5
3 3
 StatusCode==1.0.0
4 4
 TimeConvert==1.4.1
5
-django-admin==1.0.4
5
+django-admin==1.0.6
6 6
 django-detect==1.0.5
7 7
 django-json-response==1.1.5
8 8
 django-logit==1.0.6
9 9
 django-paginator2==1.0.3
10 10
 django-shortuuidfield==0.1.3
11 11
 django-uniapi==1.0.0
12
-django-we==1.0.10
12
+django-we==1.0.11
13 13
 hiredis==0.2.0
14
+pyPdf==1.13
14 15
 redis==2.10.6
15 16
 redis-extensions==1.1.1
17
+shortuuid==0.5.0