-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from tikazyq/develop
Develop
- Loading branch information
Showing
31 changed files
with
963 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import json | ||
|
||
from bson import ObjectId | ||
from pymongo import ASCENDING | ||
|
||
from db.manager import db_manager | ||
from routes.base import BaseApi | ||
from utils import jsonify | ||
|
||
|
||
class SiteApi(BaseApi): | ||
col_name = 'sites' | ||
|
||
arguments = ( | ||
('keyword', str), | ||
('category', str), | ||
) | ||
|
||
def get(self, id: str = None, action: str = None): | ||
# action by id | ||
if action is not None: | ||
if not hasattr(self, action): | ||
return { | ||
'status': 'ok', | ||
'code': 400, | ||
'error': 'action "%s" invalid' % action | ||
}, 400 | ||
return getattr(self, action)(id) | ||
|
||
elif id is not None: | ||
site = db_manager.get(col_name=self.col_name, id=id) | ||
return jsonify(site) | ||
|
||
# list tasks | ||
args = self.parser.parse_args() | ||
page_size = args.get('page_size') or 10 | ||
page_num = args.get('page_num') or 1 | ||
filter_str = args.get('filter') | ||
keyword = args.get('keyword') | ||
filter_ = {} | ||
if filter_str is not None: | ||
filter_ = json.loads(filter_str) | ||
if keyword is not None: | ||
filter_['$or'] = [ | ||
{'description': {'$regex': keyword}}, | ||
{'name': {'$regex': keyword}}, | ||
{'domain': {'$regex': keyword}} | ||
] | ||
|
||
items = db_manager.list( | ||
col_name=self.col_name, | ||
cond=filter_, | ||
limit=page_size, | ||
skip=page_size * (page_num - 1), | ||
sort_key='rank', | ||
sort_direction=ASCENDING | ||
) | ||
|
||
sites = [] | ||
for site in items: | ||
# get spider count | ||
site['spider_count'] = db_manager.count('spiders', {'site': site['_id']}) | ||
|
||
sites.append(site) | ||
|
||
return { | ||
'status': 'ok', | ||
'total_count': db_manager.count(self.col_name, filter_), | ||
'page_num': page_num, | ||
'page_size': page_size, | ||
'items': jsonify(sites) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import request from '../../api/request' | ||
|
||
const state = { | ||
siteList: [], | ||
|
||
// filter | ||
filter: { | ||
category: undefined | ||
}, | ||
keyword: '', | ||
|
||
// pagination | ||
pageNum: 1, | ||
pageSize: 10, | ||
totalCount: 0 | ||
} | ||
|
||
const getters = {} | ||
|
||
const mutations = { | ||
SET_KEYWORD (state, value) { | ||
state.keyword = value | ||
}, | ||
SET_SITE_LIST (state, value) { | ||
state.siteList = value | ||
}, | ||
SET_PAGE_NUM (state, value) { | ||
state.pageNum = value | ||
}, | ||
SET_PAGE_SIZE (state, value) { | ||
state.pageSize = value | ||
}, | ||
SET_TOTAL_COUNT (state, value) { | ||
state.totalCount = value | ||
} | ||
} | ||
|
||
const actions = { | ||
editSite ({ state, dispatch }, payload) { | ||
const { id, category } = payload | ||
return request.post(`/sites/${id}`, { | ||
category | ||
}) | ||
}, | ||
getSiteList ({ state, commit }) { | ||
return request.get('/sites', { | ||
page_num: state.pageNum, | ||
page_size: state.pageSize, | ||
keyword: state.keyword || undefined, | ||
filter: { | ||
category: state.filter.category || undefined | ||
} | ||
}) | ||
.then(response => { | ||
commit('SET_SITE_LIST', response.data.items) | ||
commit('SET_TOTAL_COUNT', response.data.total_count) | ||
}) | ||
} | ||
} | ||
|
||
export default { | ||
namespaced: true, | ||
state, | ||
getters, | ||
mutations, | ||
actions | ||
} |
Oops, something went wrong.