pixabay https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png 改了一点[上回](https://peakd.com/hive-105017/@june0620/python-13-django-6)实现的搜索功能的逻辑。每当搜索文章发现这并不是我所需要的。之前的逻辑是搜索所有的tag, title, body值,即,tag OR title OR body。 tag和title/text本身就不是一个性质,所以不能这样实现。通常搜索,都是先搜索tag,然后在下级继续搜索title或者body。也就是说tag AND (title OR body) 。 哎~ 事先没明确设计,导致出现如此问题。没办法,只能再次改逻辑了。 顺便都改为python的类。虽然我不太懂python的类到底哪里好。(#当作学习#python初学者) ##### services.py 1. 在 services.py 创建类,之后新建account, q_titles, q_texts, q_tags, blogs 类变量。 2. 创建 __init__ 函数,定义参数 query 并赋值给类变量 3. 添加 search_posts 函数。 当tag为空或者有匹配值时继续检查title或body,并且把匹配值装入另一个list里。 4. 另创建 is_keywords_contains 函数,内部检查 title 和 body 值。 5. 创建一个 get_tags 函数 ``` import json from steem import Steem class Search: s = Steem() account = 'june0620' q_titles = [] q_texts = [] q_tags = [] blogs = [] def __init__(self, query: dict): self.q_titles = list(filter(None, query['titles'])) self.q_texts = list(filter(None, query['texts'])) self.q_tags = list(filter(None, query['tags'])) self.blogs = self.s.get_blog(self.account, 0, 400) def search_posts(self): s_blogs = [] for blog in self.blogs: blog_data = blog['comment'] title = blog_data['title'].lower() body = blog_data['body'].lower() tags = self.get_tags(blog_data['json_metadata']) if not self.q_tags or set(self.q_tags) & set(tags): if not self.q_titles and not self.q_texts: s_blogs.append(blog) elif self.is_keywords_contains(title, body): s_blogs.append(blog) return s_blogs def is_keywords_contains(self, title: str, body: str) -> bool: if any(q_title in title for q_title in self.q_titles): return True if any(q_text in body for q_text in self.q_texts): return True return False def get_tags(self, json_metadata: str) -> list: ''' steempython의 get_blog()함수의 json_metadata값이 str이므로 list로 변환 ''' metadata = json.loads(json_metadata) if metadata: return metadata['tags'] else: return [] ```  ##### views.py 继承 django的 ListView,把文章目录装入 queryset 一并映射到 html。 ``` from django.http import HttpResponse from django.views.generic import ListView from .services import Search from steem import Steem s = Steem() def main_view(request): data = s.get_account('june0620') response = HttpResponse() response.write(data) return response class posts(ListView): template_name = 'album.html' context_object_name = 'all_posts' def get(self, request, *args, **kwargs): # self.queryset = s.get_blog(kwargs['account'], entry_id=0, limit=100) query = { 'tags': ['kr'], 'titles': ['python'], 'texts': ['사랑하겠습니다'] } se = Search(query=query) self.queryset = se.search_posts() return super().get(request, *args, **kwargs) ```  ##### Results 经简单测试,未发现问题。 下回学习 django 的 test 功能,再好好测试一番。  *** **[Cookie 😅]** Python 3.7.4 Django 2.2.4 steem-python 1.0.1 goorm IDE 1.3
author | june0620 |
---|---|
permlink | python-14-django-7-2-qfv6ej |
category | hive-105017 |
json_metadata | {"app":"peakd/2020.08.3","format":"markdown","tags":["cn","whalepower","dblog","palnet","python","goorm","dev","django"],"users":["june0620"],"links":["/hive-105017/@june0620/python-13-django-6"],"image":["https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png","https://cdn.steemitimages.com/DQmbEpGTwUDzwmECJT12w2fQgpTKERTYcUUJQM34jENqCjM/image.png","https://cdn.steemitimages.com/DQmbXLZvspSFdpEbstMw34PAQa3P8MmFWBiFamLDSrGvDGm/image.png","https://cdn.steemitimages.com/DQmUdGzz9HsnXQKMK3ZDnLf7gpYmWkCTJQKU31RqUCxXzZg/image.png"]} |
created | 2020-08-30 06:37:36 |
last_update | 2020-08-30 06:37:36 |
depth | 0 |
children | 1 |
last_payout | 2020-09-06 06:37:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 4.026 HBD |
curator_payout_value | 3.542 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 3,493 |
author_reputation | 118,592,211,436,406 |
root_title | "[Python #14] [Django #7] 搜索我的文章 #2" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 99,350,494 |
net_rshares | 25,132,888,414,335 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
team | 0 | 73,903,636,035 | 10% | ||
mangou007 | 0 | 10,530,251,482 | 6.56% | ||
livingfree | 0 | 314,281,493,447 | 4% | ||
ace108 | 0 | 269,946,966,746 | 7% | ||
magicmonk | 0 | 2,727,567,053,991 | 100% | ||
bert0 | 0 | 34,229,429,942 | 6.56% | ||
sweetsssj | 0 | 13,784,185,248,353 | 33% | ||
tumutanzi | 0 | 551,238,440 | 50% | ||
ebargains | 0 | 5,558,904,662 | 20% | ||
created | 0 | 656,900,956,687 | 4% | ||
coldhair | 0 | 889,528,312 | 50% | ||
lordneroo | 0 | 4,023,229,954 | 19.4% | ||
frankk | 0 | 10,456,411,701 | 10% | ||
eliel | 0 | 38,191,568,514 | 20% | ||
mys | 0 | 2,113,439,025 | 2% | ||
improv | 0 | 206,358,544 | 2% | ||
mxzn | 0 | 1,021,871,858 | 10% | ||
alphacore | 0 | 242,505,839,118 | 20% | ||
shenchensucc | 0 | 17,740,952,324 | 30% | ||
whd | 0 | 2,116,374,700 | 2% | ||
d-pend | 0 | 5,732,241,614 | 0.2% | ||
furious-one | 0 | 7,486,236,765 | 20% | ||
gniksivart | 0 | 33,542,168,131 | 20% | ||
runicar | 0 | 186,487,296,606 | 19.4% | ||
codingdefined | 0 | 52,448,040,408 | 16% | ||
dado13btc | 0 | 645,464,075 | 19.4% | ||
stinawog | 0 | 3,594,667,638 | 20% | ||
pardeepkumar | 0 | 39,143,904,804 | 19.4% | ||
kimzwarch | 0 | 8,878,648,410 | 4% | ||
crokkon | 0 | 3,990,479,919 | 14% | ||
fbslo | 0 | 3,301,663,046 | 1% | ||
accelerator | 0 | 56,957,288,696 | 5% | ||
estream.studios | 0 | 863,070,398 | 20% | ||
june0620 | 0 | 478,838,892,462 | 100% | ||
mawit07 | 0 | 2,683,598,601 | 50% | ||
rakkasan84 | 0 | 973,009,163 | 16% | ||
minloulou | 0 | 2,466,691,225 | 10% | ||
flamingirl | 0 | 1,699,870,327 | 6.56% | ||
lindalex | 0 | 578,398,266 | 50% | ||
cnbuddy | 0 | 906,836,706,023 | 100% | ||
ocupation | 0 | 56,775,576,123 | 19.4% | ||
itchyfeetdonica | 0 | 53,242,515,347 | 50% | ||
nokodemion | 0 | 10,136,183,170 | 100% | ||
enjoyinglife | 0 | 3,047,404,101 | 19.4% | ||
dudream | 0 | 57,945,901,145 | 100% | ||
adityajainxds | 0 | 9,955,687,094 | 20% | ||
suhunter | 0 | 958,763,194 | 50% | ||
curx | 0 | 73,184,077,649 | 20% | ||
leslierevales | 0 | 3,128,548,275 | 50% | ||
udabeu | 0 | 9,375,620,138 | 30% | ||
jsj1215 | 0 | 5,002,875,920 | 100% | ||
yasu | 0 | 6,715,930,180 | 100% | ||
kristves | 0 | 3,098,642,341 | 13% | ||
ofildutemps | 0 | 1,805,296,633 | 20% | ||
mindtrap | 0 | 685,287,330,909 | 19.4% | ||
realprince | 0 | 29,799,719,126 | 100% | ||
donekim | 0 | 2,600,567,044 | 100% | ||
josevas217 | 0 | 12,286,760,620 | 5.24% | ||
beleg | 0 | 866,516,552 | 2% | ||
franyeligonzalez | 0 | 650,523,581 | 3.28% | ||
lucky2015 | 0 | 8,064,844,838 | 100% | ||
sustainablelivin | 0 | 1,119,062,933 | 10% | ||
gghite | 0 | 187,561,457,692 | 100% | ||
julialee66 | 0 | 1,116,237,399,434 | 8.5% | ||
andrewma | 0 | 11,221,315,430 | 50% | ||
steem.services | 0 | 1,344,484,916,108 | 20% | ||
archisteem | 0 | 2,602,804,425 | 7.5% | ||
ravenkim | 0 | 1,076,598,907 | 100% | ||
milaan | 0 | 1,802,634,851 | 19.4% | ||
mastersa | 0 | 647,196,766 | 11% | ||
daath | 0 | 718,024,414 | 100% | ||
julian2013 | 0 | 1,245,957,055 | 0.9% | ||
melaniewang | 0 | 8,104,625,514 | 50% | ||
lupafilotaxia | 0 | 61,699,364,463 | 50% | ||
changxiu | 0 | 5,037,538,174 | 50% | ||
bluengel | 0 | 15,358,356,749 | 100% | ||
laissez-faire | 0 | 118,351,851 | 100% | ||
cherryzz | 0 | 156,665,251,111 | 50% | ||
memeteca | 0 | 833,903,238 | 6.56% | ||
forecasteem | 0 | 79,371,970,709 | 100% | ||
cwow2 | 0 | 210,966,452,564 | 19% | ||
vcs | 0 | 2,563,506,551 | 20% | ||
moneytron | 0 | 6,516,308,792 | 100% | ||
starrouge | 0 | 1,029,983,213 | 50% | ||
wherein | 0 | 472,026,724,134 | 100% | ||
steemfriends | 0 | 13,903,964,429 | 100% | ||
zerofive | 0 | 1,060,877,211 | 50% | ||
cnstm | 0 | 296,146,546,678 | 100% | ||
lianjingmedia | 0 | 992,702,441 | 100% | ||
smileyboy | 0 | 187,956,654 | 20% | ||
cpt-sparrow | 0 | 4,086,420,288 | 100% | ||
holydog | 0 | 108,796,549 | 10% | ||
tina3721 | 0 | 4,850,358,719 | 50% | ||
andyhsia | 0 | 7,558,318,623 | 100% | ||
bergelmirsenpai | 0 | 1,163,688,117 | 20% | ||
botante | 0 | 51,735,360,360 | 20% | ||
real3earch | 0 | 9,060,597,358 | 100% | ||
jgb | 0 | 492,508,761 | 5.24% | ||
sacrosanct | 0 | 5,792,666,994 | 19.4% | ||
toni.pal | 0 | 0 | 0.52% | ||
hivebuzz | 0 | 4,369,700,245 | 2% | ||
hiveyoda | 0 | 11,000,419,269 | 4% | ||
logicforce | 0 | 2,100,163,379 | 50% | ||
blogstats | 0 | 1,269,288,790 | 100% |
Congratulations @june0620! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) : <table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@june0620/upvoted.png?202008300659"></td><td>You received more than 30000 upvotes. Your next target is to reach 35000 upvotes.</td></tr> </table> <sub>_You can view [your badges on your board](https://hivebuzz.me/@june0620) And compare to others on the [Ranking](https://hivebuzz.me/ranking)_</sub> <sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub> To support your work, I also upvoted your post! **Do not miss the last post from @hivebuzz:** <table><tr><td><a href="/hivebuzz/@hivebuzz/pud-countdown"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg"></a></td><td><a href="/hivebuzz/@hivebuzz/pud-countdown">Hive Power Up Day - The countdown is ticking</a></td></tr><tr><td><a href="/hivebuzz/@hivebuzz/pud-202009"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg"></a></td><td><a href="/hivebuzz/@hivebuzz/pud-202009">Hive Power Up Day - Let's grow together!</a></td></tr></table>
author | hivebuzz |
---|---|
permlink | hivebuzz-notify-june0620-20200830t142641000z |
category | hive-105017 |
json_metadata | {"image":["http://hivebuzz.me/notify.t6.png"]} |
created | 2020-08-30 14:26:39 |
last_update | 2020-08-30 14:26:39 |
depth | 1 |
children | 0 |
last_payout | 2020-09-06 14:26:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,205 |
author_reputation | 369,969,972,233,437 |
root_title | "[Python #14] [Django #7] 搜索我的文章 #2" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 99,355,714 |
net_rshares | 0 |