def get_recommend_media_list(self, media_id): media = MediaCache().get_media_by_id(media_id) if isinstance(media, Exception): return media media_tags_key_dict = MediaCache().get_media_tags_dict() match_result = match_resource_action_with_tags(media_tags_key_dict, media.tags) if isinstance(match_result, Exception): return match_result media_list = [] match_count = 2 count = 0 for item_media_id in match_result: if item_media_id == media_id: continue item_media = MediaCache().get_media_detail_by_id(item_media_id) if isinstance(item_media, Exception): continue media_list.append(item_media) count += 1 if count >= match_count: break return media_list[:2]
def get_relevant_information_list(self, information_id, match_count=6): information = MediaCache().get_information_by_id(information_id) if isinstance(information, Exception): return information information_tags_key_dict = MediaCache().get_information_tags_dict() match_result = match_resource_action_with_tags( information_tags_key_dict, information.tags) if isinstance(match_result, Exception): return match_result information_list = [] count = 0 for item_information_id in match_result: if item_information_id == information_id: continue item_information = MediaCache().get_information_detail_by_id( item_information_id) if isinstance(item_information, Exception): continue information_list.append(item_information) count += 1 if count >= match_count: break return information_list[:match_count]
def get_next_case_detail(self, case_id): sort_order_list = MediaCache().get_case_sort_order_list() try: index = sort_order_list.index(case_id) except Exception as e: return e if len(sort_order_list) <= index + 1: return None detail = MediaCache().get_case_detail_by_id(sort_order_list[index + 1]) return detail
def search_action(self, **search_kwargs): case_search_dict = MediaCache().get_case_search_dict() match_result = [] for case_id, case_detail in case_search_dict.items(): does_match = False for kw, value in search_kwargs.items(): if case_detail.get(kw) != value: break else: does_match = True if does_match: match_result.append({ 'case_id': case_id, 'case_detail': case_detail }) return match_result
class RelevantCaseList(APIView): """ 案例:相关文章 """ def get_relevant_case_list(self, case_id, match_count=6): case = MediaCache().get_case_by_id(case_id) if isinstance(case, Exception): return case
def search_action(self, **search_kwargs): information_search_dict = MediaCache().get_information_search_dict() match_result = [] for information_id, information_detail in information_search_dict.items( ): does_match = False for kw, value in search_kwargs.items(): if information_detail.get(kw) != value: break else: does_match = True if does_match: match_result.append({ 'information_id': information_id, 'information_detail': information_detail }) return match_result
def get_relevant_case_list(self, media_id): media = MediaCache().get_media_by_id(media_id) if isinstance(media, Exception): return media case_tags_key_dict = MediaCache().get_case_tags_dict() match_result = match_resource_action_with_tags(case_tags_key_dict, media.tags) if isinstance(match_result, Exception): return match_result case_list = [] match_count = 2 count = 0 for case_id in match_result: case = MediaCache().get_case_detail_by_id(case_id) if isinstance(case, Exception): continue
def perfect_detail(self): detail = model_to_dict(self) if hasattr(self, 'AdminMeta'): if hasattr(self.AdminMeta, 'json_fields'): for field in self.AdminMeta.json_fields: if field == 'tags': tag_ids = json.loads(detail[field]) detail[field] = self.get_perfect_tags(tag_ids) else: detail[field] = json.loads(detail[field]) media = MediaCache().get_media_by_id(self.media_id) if isinstance(media, Exception): return media detail['media_picture'] = media.picture return detail
def get_media_detail_list(self, **kwargs): sort_key = kwargs['sort'] if kwargs.get('sort') else 'updated' pop_keys = ('sort', 'page_size', 'page_index') for key in pop_keys: if key in kwargs: kwargs.pop(key) match_media_list = self.search_action(**kwargs) match_media_list = sorted(match_media_list, key=lambda x: x['media_detail'][sort_key], reverse=True) perfect_details = [] for item_media in match_media_list: media_detail = MediaCache().get_media_detail_by_id( item_media['media_id']) perfect_details.append(media_detail) return perfect_details
def get_case_detail_list(self, **kwargs): sort_key = kwargs['sort'] if 'sort' in kwargs else 'updated' # 最新发布(栏目) if kwargs.get('column') == RESOURCE_COLUMN_CONFIG['newest']: kwargs.pop('column') pop_keys = ('page_size', 'page_index') for key in pop_keys: if key in kwargs: kwargs.pop(key) match_case_list = self.search_action(**kwargs) match_case_list = sorted(match_case_list, key=lambda x: x['case_detail'][sort_key], reverse=True) perfect_details = [] for item_case in match_case_list: detail = MediaCache().get_case_detail_by_id(item_case['case_id']) perfect_details.append(detail) return perfect_details
class SearchResourceAction(APIView): """ 搜索资源 """ search_dict_config = { Media: MediaCache().get_media_search_dict(), Information: MediaCache().get_information_search_dict(), Case: MediaCache().get_case_search_dict(), } resource_detail_config = { Media: MediaCache().get_media_detail_by_id, Information: MediaCache().get_information_detail_by_id, Case: MediaCache().get_case_detail_by_id, } def search_action(self, search_dict, keywords): if isinstance(keywords, (str, unicode)): keywords = keywords.split() match_result = [] re_com_list = [re.compile(keyword) for keyword in keywords] for source_id in search_dict: count = 0 for key2, value2 in search_dict[source_id].items(): if key2 == 'tags': value2 = ' '.join(value2) for re_com in re_com_list: try: count += len(re_com.findall(value2)) except: continue if count > 0: item_result = {'resource_id': source_id, 'match_count': count} match_result.append(item_result) return sorted(match_result, key=lambda x: x['match_count'], reverse=True) def get_search_resource_list(self, source_type, keywords): model_class = SOURCE_TYPE_DB[source_type] search_dict = self.search_dict_config[model_class] match_result = self.search_action(search_dict, keywords) detail_function = self.resource_detail_config[model_class] perfect_result = [] for item in match_result: resource_id = item['resource_id'] detail = detail_function(resource_id) if model_class == Media: detail['picture'] = detail['picture_profile'] perfect_result.append(detail) return perfect_result def post(self, request, *args, **kwargs): form = SearchResourceActionForm(request.data) if not form.is_valid(): return Response({'Detail': form.errors}, status=status.HTTP_400_BAD_REQUEST) cld = form.cleaned_data details = self.get_search_resource_list(cld['source_type'], cld['keywords']) serializer = ResourceListSerializer(data=details) if not serializer.is_valid(): return Response({'Detail': serializer.errors}, status=status.HTTP_400_BAD_REQUEST) list_data = serializer.list_data() if isinstance(list_data, Exception): return Response({'Detail': list_data.args}, status=status.HTTP_400_BAD_REQUEST) return Response(list_data, status=status.HTTP_200_OK)
def get_advert_resource_list(self, source_type): return MediaCache().get_advert_list(source_type)
def get_case_detail(self, case_id): return MediaCache().get_case_detail_by_id(case_id)
def get_information_detail(self, information_id): return MediaCache().get_information_detail_by_id(information_id)
def get_media_detail(self, media_id): return MediaCache().get_media_detail_by_id(media_id)
if isinstance(list_data, Exception): return Response({'Detail': list_data.args}, status=status.HTTP_400_BAD_REQUEST) return Response(list_data, status=status.HTTP_200_OK) class RelevantCaseList(APIView): """ 案例:相关文章 """ def get_relevant_case_list(self, case_id, match_count=6): case = MediaCache().get_case_by_id(case_id) if isinstance(case, Exception): return case case_tags_key_dict = MediaCache().get_case_tags_dict() match_result = match_resource_action_with_tags(case_tags_key_dict, case.tags) if isinstance(match_result, Exception): return match_result case_list = [] count = 0 for item_case_id in match_result: if item_case_id == case_id: continue item_case = MediaCache().get_case_detail_by_id(item_case_id) if isinstance(item_case, Exception): continue case_list.append(item_case) count += 1