def moderate(self, comment_id): try: comment = web.models.Comment.manager.get(comment_id=comment_id) comment.moderated = True comment.save() except Exception as ex: Logger.log_exception("CommentManager.moderate", ex)
def update(request, *args, **kwargs): result = None try: poll_id = json.loads(request.POST[u'poll_id']) choice_id = json.loads(request.POST[u'choice_id']) text = request.POST[u'text'] choices_list = request.session[u'poll_choices'] choice = web.models.Choice() if poll_id > 0: choice = web.models.Choice.manager.update(poll_id, choice_id, text) else: if u'poll_choices' in request.session: for x in choices_list: if int(x[u'choice_id']) == choice_id: choices_list.remove(x) x[u'text'] = text choices_list.append(x) request.session[u'poll_choices'] = choices_list choice = x break result = choice except Exception as ex: Logger.log_exception("choice_request, *args, **kwargss.Update.get", ex) return StreamingHttpResponse(json.dumps(JsonPoll.choice_to_json(result)), content_type="application/json")
def article_image_list(request, *args, **kwargs): result = list() try: if not request.session.exists(request.session.session_key): request.session.create() post = request.POST user_tmp_dir = const.TEMP_ARTICLE_IMG_PATH + "user_" + str(request.user.id) + "\\" path_for_template = const.TEMP_PATH_ART_IMG_FOR_TPL + "user_" + str(request.user.id) + "/" temp_article_images = web.models.Image.manager.load_article_temp_img_paths(user_tmp_dir, path_for_template) article_images = list() if u'article_id' in post: article_id = int(post[u'article_id']) if u'art_deletable_img_ids' in request.session: deletable_img_ids = request.session[u'art_deletable_img_ids'] else: deletable_img_ids = list() request.session.modified = True article_images = web.models.Image.manager.load_article_img_paths(article_id=article_id, art_deletable_img_ids=deletable_img_ids) temp_main = False for img in temp_article_images: if img[u'is_main']: temp_main = True break if temp_main: for img in article_images: img[u'is_main'] = False result = temp_article_images + article_images result = sorted(result, key=lambda k: (k['image_path'])) except Exception as ex: Logger.log_exception("image_views.article_image_list", ex) return StreamingHttpResponse(json.dumps(result), content_type="application/json")
def load(self, poll_id): result = list() try: result = self.filter(poll_id=poll_id, deleted=False) except Exception as ex: Logger.log_exception("ChoiceManager.load", ex) return result
def update(request, *args, **kwargs): result = {} try: article_data = request.POST article = web.models.Article.manager.update(article_data) if (article is not None): # SECTIONS section_ids = json.loads(article_data[u'sections']) web.models.Article.manager.update_sections(article, section_ids) # TAGS uploaded_tags = json.loads(article_data[u'tags']) web.models.Article.manager.update_tags(article, uploaded_tags) # SOURCES uploaded_sources = json.loads(article_data[u'sources']) web.models.Article.manager.update_sources(article, uploaded_sources) # IMAGES image_paths = json.loads(article_data[u'image_paths']) if u'art_deletable_img_ids' in request.session: deletable_img_ids = request.session[u'art_deletable_img_ids'] else: deletable_img_ids = list() request.session.modified = True web.models.Image.manager.update_article_images( request.user.id, article, image_paths, deletable_img_ids) result = JsonArticle.to_json(article) except Exception as ex: Logger.log_exception("article_views.Update", ex) web.models.Article.manager.clear_img_temp_folder(request.user.id) return HttpResponse(json.dumps(result), content_type="application/json")
def clean_author_tmp_img(request, *args, **kwargs): try: user = request.user web.models.Image.manager.clean_author_tmp_profile_img(user.id) except Exception as ex: Logger.log_exception("author_views.clean_author_tmp_img", ex) return HttpResponse()
def moderate(self, tag_id): try: tag = web.models.Tag.manager.get(tag_id=tag_id) tag.moderated = True tag.save() except Exception as ex: Logger.log_exception("TagManager.moderate", ex)
def to_json_list_for_block(cls, poll_list): result = list() for poll in poll_list: try: voting_period_start_str = dateh.DateTimeHelper.to_datetime_str(poll.voting_period_start) voting_period_end_str = dateh.DateTimeHelper.to_datetime_str(poll.voting_period_end) choices = list() for choice in poll.choice_set.all(): try: choice_json = cls.choice_votes_to_json(choice) choices.append(choice_json) except Exception as ex: Logger.log_exception("JsonPoll.to_json_list_for_block", ex) choice_json = { 'poll_id': poll.poll_id, 'title': poll.title, 'description': poll.description, 'multiple_choice': poll.multiple_choice, 'voting_period_start': voting_period_start_str, 'voting_period_end': voting_period_end_str, 'deleted': poll.deleted, 'editable': poll.editable, 'choices': choices, 'publisher_id': poll.publication.user_profile_id } result.append(choice_json) except Exception as ex: Logger.log_exception("JsonPoll.to_json_list_for_block", ex) return result
def get_time_mark(self): result = '' try: result = "#" + str(int(round(time.time() * 1000))) except Exception as ex: Logger.log_exception("ImageManager.get_time_mark", ex) return result
def update_article_images(self, user_id, article, image_paths, deletable_img_ids): try: temp_full_paths = [ x[u'path'] for x in image_paths if x[u'temp'] == True ] temp_paths = list() for img_id in deletable_img_ids: try: web.models.ArticleImage.objects.filter( image_id=img_id).delete() self.filter(image_id=int(img_id)).delete() except: pass for path in temp_full_paths: splitted = path.split('/') splitted_text = splitted[len(splitted) - 1] temp_paths.append(splitted_text.split('#')[0]) not_temp_paths = list() not_temp_full_paths = [ x[u'path'] for x in image_paths if x[u'temp'] == False ] for path in not_temp_full_paths: splitted = path.split('/') splitted_text = splitted[len(splitted) - 1] not_temp_paths.append(splitted_text.split('#')[0]) web.models.Image.manager.create_article_imgs( user_id, article, temp_paths) except Exception as ex: Logger.log_exception('ArticleManager.update_article_images', ex)
def update_article_tmp_main(self, user, file_data): try: req_file_name = file_data[u'fileName'] req_file_name = req_file_name.split("#")[0] # Si req_file_name ya es main no hago nada # Si no es main: if not "main" in req_file_name: user_tmp_dir = const.TEMP_ARTICLE_IMG_PATH + "user_" + str( user.id) + "\\" for file_full_name in os.listdir(user_tmp_dir): file_name, file_ext = os.path.splitext(file_full_name) if file_full_name == req_file_name: new_file_name = file_name + "_main" + file_ext os.rename(user_tmp_dir + file_full_name, user_tmp_dir + new_file_name) elif "_main" in file_full_name: new_file_name = file_full_name.replace("_main", "") os.rename(user_tmp_dir + file_full_name, user_tmp_dir + new_file_name) try: if u'imageId' in file_data and file_data[ u'imageId'] != u'null': image_id = int(file_data[u'imageId']) article_id = int(file_data[u'articleId']) self.set_main(article_id=article_id, image_id=image_id) except Exception as ex: Logger.log_exception( "ImageManager.update_article_tmp_main", ex) except Exception as ex: Logger.log_exception("ImageManager.update_article_main_tmp", ex)
def create_new_user_profile_img(self, user_profile_id, temp_file_name): result = '' try: found = False for name in os.listdir(const.TEMP_PROFILE_IMG_PATH): if temp_file_name in name: temp_file_name = name found = True break if found: name, extension = os.path.splitext(temp_file_name) final_file_name = "user_img_" + str( user_profile_id) + extension final_file_full_name = const.PROFILES_IMG_PATH + final_file_name temp_file_full_name = const.TEMP_PROFILE_IMG_PATH + "\\" + temp_file_name if not os.path.exists(const.PROFILES_IMG_PATH): os.makedirs(const.PROFILES_IMG_PATH) shutil.copyfile(temp_file_full_name, final_file_full_name) os.remove(temp_file_full_name) result = final_file_name except Exception as ex: Logger.log_exception("ImageManager.create_new_user_profile_img", ex) return result
def clean_user_tmp_profile_img(self, file_key): try: if type(file_key) == int: # Se recibe id de usuario de sesion # (Se esta registrando un usuario desde administracion) search_file_name = str(file_key) + "_new_user_temp_img" else: # Se recibe el nombre del archivo if file_key is None: search_file_name = "" else: search_file_name = file_key temp_file_name = "" found = False if os.path.exists(const.TEMP_PROFILE_IMG_PATH): for name in os.listdir(const.TEMP_PROFILE_IMG_PATH): if search_file_name in name: temp_file_name = name found = True break if found: temp_file_full_name = const.TEMP_PROFILE_IMG_PATH + "\\" + temp_file_name os.remove(temp_file_full_name) except Exception as ex: Logger.log_exception("ImageManager.clean_user_tmp_profile_img", ex)
def load_random(request, *args, **kwargs): try: now = datetime.utcnow().replace(tzinfo=utc) kwargs = { 'deleted': False, 'voting_period_start__lte': now, 'voting_period_end__gte': now } if u'section_id' in request.POST: kwargs['pollsection__section_id'] = json.loads(request.POST[u'section_id']) if request.user.is_authenticated(): user_profile = web.models.UserProfile.manager.get(user_id=request.user.id) user_votes_query = web.models.Vote.manager.filter(user_profile_id=user_profile.user_profile_id).values('poll_id') user_votes_ids = [id['poll_id'] for id in user_votes_query] polls = web.models.Poll.manager.exclude(poll_id__in=user_votes_ids) polls = polls.filter(**kwargs) else: polls = web.models.Poll.manager.filter(**kwargs) polls = polls.order_by("poll_id")[:3] except Exception as ex: polls = list() Logger.log_exception("poll_views.load_random.post", ex) user_profile = web.models.UserProfile.manager.get_by_user_id(request.user.id) if request.user.is_authenticated(): result = JsonPoll.to_json_list_for_user_block(polls, user_profile.user_profile_id) else: result = JsonPoll.to_json_list_for_block(polls) return HttpResponse(json.dumps(result), content_type="application/json")
def get_by_user_id(self, user_id): result = None try: result = self.get(user_id=user_id) except Exception as ex: Logger.log_exception("UserProfileManager.get_by_user_id", ex) return result
def increase_visits(self, article): try: article.visits += 1 article.save() except Exception as ex: Logger.log_exception("ArticleManager.increase_visits", ex) return article
def load(self): result = list() try: result = self.filter(deleted=False).order_by('name') except Exception as ex: Logger.log_exception("SectionManager.load", ex) return result
def delete(self, section_id): try: section = web.models.Section.manager.get(pk=section_id) section.deleted = True section.save() except Exception as ex: Logger.log_exception("SectionManager.delete", ex)
def load(self): try: result = web.models.Author.manager.filter( deleted=False).order_by('author_id') except Exception as ex: result = list() Logger.log_exception("AuthorManager.load", ex) return result
def load(request, *args, **kwargs): result = list() try: authors = web.models.Author.manager.load() result = JsonAuthor.to_json_list(authors) except Exception as ex: Logger.log_exception("author_views.load.get", ex) return HttpResponse(json.dumps(result), content_type="application/json")
def convert_input_str_to_datetime(datetime_str): date_time_result = None try: date_time_result = parser.parse(datetime_str) except Exception as ex: Logger.log_exception( "DateTimeHelper.convert_input_str_to_datetime", ex) return date_time_result
def load_for_combo(request, *args, **kwargs): result = list() try: query = web.models.Tag.manager.load_for_combo() result = JsonTag.to_json_for_select(query) except Exception as ex: Logger.log_exception("tag_views.load_for_combo", ex) return HttpResponse(json.dumps(result), content_type="application/json")
def name_exists(request, *args, **kwargs): result = False try: name = request.POST[u'name'] result = web.models.Section.manager.name_exists(name) except Exception as ex: Logger.log_exception("section_views.CheckName.post", ex) return HttpResponse(json.dumps(result), content_type="application/json")
def load(request, *args, **kwargs): result = list() try: query = web.models.Section.manager.load() result = JsonSection.to_json_list(query) except Exception as ex: Logger.log_exception("section_views.load", ex) return HttpResponse(json.dumps(result), content_type="application/json")
def load_for_combo(request, *args, **kwargs): result = list() try: query = web.models.UserProfile.manager.filter(deleted=False) result = JsonUserProfile.to_json_for_cbo_list(query) except Exception as ex: Logger.log_exception("user_profile_views.load_for_combo.get", ex) return HttpResponse(json.dumps(result), content_type="application/json")
def load_for_combo(self): result = list() try: result = self.filter(deleted=False, moderated=False).distinct('text') except Exception as ex: Logger.log_exception("TagMaanger.load_for_combo", ex) return result
def clear_temp(request, *args, **kwargs): try: if u'poll_choices' in request.session: request.session[u'poll_choices'] = list() request.session.modified = True except Exception as ex: Logger.log_exception("choice_views.clear_temp", ex) return StreamingHttpResponse("OK", content_type="application/json")
def load_articles(request, *args, **kwargs): try: query = web.models.Article.manager.load() result = JsonArticle.to_json_list(query) except Exception as ex: result = list() Logger.log_exception("article_views.load_articles", ex) return HttpResponse(json.dumps(result), content_type="application/json")
def search(self, search_data): query = None try: kwargs = {'deleted': False} if u'title' in search_data and u'title' in search_data and not search_data[ u'title'] == u'': kwargs['title__icontains'] = search_data[u'title'] if u'sections-check' in search_data and u'sections' in search_data and len( json.loads(search_data[u'sections'])) > 0: kwargs['articlesection__section_id__in'] = json.loads( search_data[u'sections']) if u'tags-check' in search_data and u'tags' in search_data and json.loads( search_data[u'tags']).length > 0: tag_query = web.models.Tag.manager.filter(text__in=json.loads( search_data[u'tags']), deleted=False) kwargs['tag__in'] = tag_query if u'author-check' in search_data and u'author' in search_data and not search_data[ u'author'] == u'': if query is None: query = web.models.Article.manager.filter(deleted=False) kwargs['author_id'] = json.loads(search_data[u'author']) if u'publisher-check' in search_data and u'publisher' in search_data and not search_data[ u'publisher'] == u'': if query is None: query = web.models.Article.manager.filter(deleted=False) publications = web.models.Publication.manager.filter( user_profile_id=json.loads(search_data[u'publisher'])) kwargs['publication__in'] = publications if u'date-from-check' in search_data and u'date-from' in search_data and not search_data[ u'date-from'] == u'': if query is None: query = web.models.Article.manager.filter(deleted=False) date_array = search_data[u'date-from'].split('/') date_from = datetime(int(date_array[2]), int(date_array[1]), int(date_array[0]), tzinfo=utc) publications = web.models.Publication.manager.filter( date__gte=date_from) kwargs['publication__in'] = publications if u'date-to-check' in search_data and u'date-to' in search_data and not search_data[ u'date-to'] == u'': if query is None: query = web.models.Article.manager.filter(deleted=False) date_array = search_data[u'date-to'].split('/') date_to = datetime(int(date_array[2]), int(date_array[1]), int(date_array[0]), tzinfo=utc) publications = web.models.Publication.manager.filter( date__lte=date_to) kwargs['publication__in'] = publications if (len(kwargs.keys()) > 1): query = web.models.Article.manager.filter(**kwargs) except Exception as ex: Logger.log_exception("ArticleManager.search", ex) return query
def search_result(request, *args, **kwargs): post = request.POST try: search_query = web.models.Article.manager.search(post) except Exception as ex: search_query = list() Logger.log_exception("article_views.search_result", ex) result = JsonArticle.to_json_list_for_search(search_query) return HttpResponse(json.dumps(result), content_type="application/json")