5 @author: ymh |
5 @author: ymh |
6 ''' |
6 ''' |
7 from .models import TermLabel |
7 from .models import TermLabel |
8 from dateutil import parser |
8 from dateutil import parser |
9 import re |
9 import re |
|
10 from core.models.notice import NoticeImage |
10 |
11 |
11 class ImportProcessor(object): |
12 class ImportProcessor(object): |
12 |
13 |
13 def __init__(self, field): |
14 def __init__(self, field): |
14 self.field = field |
15 self.field = field |
35 |
36 |
36 class DateFieldProcessor(ImportProcessor): |
37 class DateFieldProcessor(ImportProcessor): |
37 |
38 |
38 def process(self, obj, value): |
39 def process(self, obj, value): |
39 setattr(obj, self.field, parser.parse(value) if value else None) |
40 setattr(obj, self.field, parser.parse(value) if value else None) |
|
41 |
|
42 class VideoFieldProcessor(ImportProcessor): |
|
43 |
|
44 def process(self, obj, value): |
|
45 res = {} |
|
46 images_str = getattr(obj, self.field, None) |
|
47 if not images_str: |
|
48 return res |
|
49 for image_path in [path.strip() for path in images_str.split(";")]: |
|
50 if not image_path: |
|
51 continue |
|
52 if not NoticeImage.objects.filter(relative_url=image_path, notice=obj).exists(): |
|
53 res.setdefault(NoticeImage,[]).append(NoticeImage(relative_url=image_path, notice=obj)) |
|
54 return res |
40 |
55 |
41 class TermProcessor(ImportProcessor): |
56 class TermProcessor(ImportProcessor): |
42 |
57 |
43 def __init__(self, field, context, notice_term_klass, re_split = r"[\;\,\:\(\)]", re_sub = "\(.+?\)"): |
58 def __init__(self, field, context, notice_term_klass, re_split = r"[\;\,\:\(\)]", re_sub = "\(.+?\)"): |
44 ImportProcessor.__init__(self, field) |
59 ImportProcessor.__init__(self, field) |
61 |
76 |
62 def process(self, obj, value): |
77 def process(self, obj, value): |
63 res = {} |
78 res = {} |
64 #remove everything between () |
79 #remove everything between () |
65 value = getattr(obj, self.field) |
80 value = getattr(obj, self.field) |
|
81 if not value : |
|
82 return res |
66 if self.re_sub: |
83 if self.re_sub: |
67 value = self.re_sub.sub("", value) |
84 value = self.re_sub.sub("", value) |
68 for token in self.re_split.split(value): |
85 for token in self.re_split.split(value): |
69 token = token.strip() |
86 token = token.strip() |
70 nt = self.build_notice_term(token, obj) |
87 nt = self.build_notice_term(token, obj) |