コード例 #1
0
ファイル: view_model.py プロジェクト: pjshu/flask-blog
 def __init__(self, post):
     self.id = post.id
     self.title = post.title or ''
     self.tags = [tag.name for tag in post.tags]
     self.visibility = post.visibility
     self.create_date = format_time(post.create_date)
     self.change_date = format_time(post.change_date)
     self.article = post.article or ''
     self.comments = post.comments
     self.excerpt = post.excerpt
コード例 #2
0
 def to_json(self, detail=False):
     data = {
         "id": self.id,
         "content": self.content,
         "post_at": format_time(self.creat_at),
         "update_at": format_time(self.update_at),
         "user": self.master.to_json()
     }
     if detail:
         data_detail = {
             "comments_count": len(self.comments)
         }
         data_detail.update(Comment.list_to_json(self.comments))
         data.update(data_detail)
     return data
コード例 #3
0
def create_commit(course, expires):
    begin = time()
    end = begin + expires
    new_commit = dict()
    new_commit['begin'] = format_time(begin)
    new_commit['end'] = format_time(end)
    finished = []
    unfinished = []
    for user in course.students:
        d = dict()
        d["id"] = user.id
        d["name"] = user.name
        unfinished.append(d)
    new_commit['finished'] = finished
    new_commit['unfinished'] = unfinished
    new_commit['id'] = uuid4()
    return new_commit
コード例 #4
0
    def save_plot(self):

        time_list = [format_time(t) for t in self.forecast_parsed.keys()]
        temp_list = [temp for temp, _, _ in self.forecast_parsed.values()]

        plot_path = make_plot(time_list, temp_list)

        return plot_path
コード例 #5
0
 def to_json(self):
     data = {
         'uuid': str(self.id),
         "name": self.name,
         'type': self.type,
         'url': self.url,
         'upload_at': format_time(self.upload_at)
     }
     return data
コード例 #6
0
 def to_json(self,detail=False):
     data = {
         "uuid": self.id,
         "status": self.status,
         "score": self.score,
         "create_at": format_time(self.create_at),
         "update_at": format_time(self.update_at)
     }
     if detail:
         if self.task.answer_visible:
             correct_ans = True
         else:
             correct_ans = False
         data_detail = {
             "answers": [answer.to_json(return_correct_answer=correct_ans) for answer in self.answers]
         }
         data.update(data_detail)
     return data
コード例 #7
0
 def to_json(self, with_documents=False, with_movies=False):
     data = {
         "id": self.id,
         "name": self.name,
         "create_at": format_time(self.create_at),
         "update_at": format_time(self.update_at)
     }
     data_documents = {
         "document_count": len(pickle.loads(self.documents)) if self.documents is not None else 0,
         "documents": Media.load_medias_from_uuid_list(pickle.loads(self.documents)) if self.documents is not None else None
     }
     data_movies = {
         "movie_count": len(pickle.loads(self.movies)) if self.movies is not None else 0,
         "movies": Media.load_medias_from_uuid_list(pickle.loads(self.movies)) if self.movies is not None else None
     }
     data.update(data_documents) if with_documents else 1  # else pass
     data.update(data_movies) if with_movies else 1
     return data
コード例 #8
0
 def to_json(self, user, detail=False):
     key = "task_finished:"+str(self.id)
     if not r.sismember(key, user.id):
         finished = False
     else:
         finished = True
     data = {
         "self": request.host_url[0:-1] + url_for('task_bp.task', tid=self.id),
         "type": self.type,
         "id": self.id,
         "task_name": self.name,
         "time_begin": format_time(self.time_begin),
         "time_end": format_time(self.time_end),
         "finished": finished,
         "statistic_select": self.generate_prob_statistic("select"),
         "statistic_blank": self.generate_prob_statistic("blank"),
         "statistic_subjective": self.generate_prob_statistic("subjective"),
         "time_excess": not self.time_begin < time() < self.time_end,
         "answer_visible": self.answer_visible,
         "max_score": self.max_score,
         "create_at": format_time(self.create_at)
     }
     if finished and self.answer_visible:
         show_answer = True
     else:
         show_answer = False
     if detail:
         data_detail = {
             "problems": [prob.to_json(show_answer) for prob in self.problems]
         }
         if self.type == "exam":
             key = "exam_begin:tid:{}:uid:{}".format(self.id, user.id)
             print(key)
             exam_begin = r.get(key)
             exam_begin = float(exam_begin) if exam_begin is not None else None
             data_detail['exam_started'] = 1 if exam_begin is not None else 0
             data_detail['exam_time_excess'] = time()-exam_begin > self.expires if exam_begin is not None else 0
             data_detail['exam_end'] = format_time(exam_begin + self.expires) if exam_begin is not None else None
         data.update(data_detail)
     return data
コード例 #9
0
 def to_json(self, detail=False):
     data = {
         "self": request.host_url[0:-1] + url_for('course_bp.course', cid=self.id),
         "id": self.id,
         "name": self.name,
         "avatar": Media.load_media_from_uuid(self.avatar, return_model=True).url if self.avatar is not None else None,
         "introduce": self.introduce,
         "public": self.public,
         "create_status": 1 if g.current_user.is_teacher(self) else 0,
         "join_status": 1 if self in g.current_user.courses else 0,
         "stat_at": format_time(self.start_at),
         "end_at": format_time(self.end_at),
         "time_excess": not self.start_at <= time() <= self.end_at,
         "teacher_name": self.teacher.name if self.teacher.name is not None else self.teacher.nickname
     }
     if detail:
         data_detail = {
             "teacher": self.teacher.to_json(detail=False)
             # "students": [student.to_json(detail=False) for student in self.students]
         }
         data.update(data_detail)
     return data
コード例 #10
0
 def to_json(self, with_problem=False, return_correct_answer=False):
     data = {
         "id": str(self.id),
         "content": pickle.loads(self.content) if self.content is not None else None,
         "medias": Media.load_medias_from_uuid_list(pickle.loads(self.media)) if self.media is not None else None,
         "score": self.score,
         "order": self.order,
         "comment": self.comment,
         "answer_at": format_time(self.create_at),
         "update_at": format_time(self.update_at)
     }
     if with_problem:
         data_problem = self.problem.to_json(return_correct_answer)
         data_problem['student_answer'] = data
         data = data_problem
         return data
     if return_correct_answer:
         data_answer = {
             "correct_answer": pickle.loads(self.problem.answer) if self.problem.answer is not None else None,
             # "answer_detail": self.detail_answer_to_json()
             "answer_detail": self.problem.answer_detail
         }
         data.update(data_answer)
     return data
コード例 #11
0
 def to_json(self):
     replies = []
     for reply_id in pickle.loads(self.replies):
         reply = Comment.query.filter_by(id=reply_id).first()
         if reply is not None:
             replies.append(reply.to_json())
     data = {
         "id": str(self.id),
         "content": self.content,
         "likes": r.scard("like_comment:{}".format(str(self.id))),
         "liked": g.current_user.liked(str(self.id), "comment"),
         "replies": replies,
         "reply": self.reply,
         "post_at": format_time(self.creat_at),
         "author": self.author.to_json()
     }
     return data
コード例 #12
0
    def to_json(self, detail=False):

        read = 0
        key = "read:{}".format(self.id)
        if r.sismember(key, g.current_user.id):
            read = 1

        data = {
            "id": self.id,
            "self": request.host_url[:-1] + url_for("course_bp.notice", notice_id=self.id, cid=self.course.id),
            "title": self.title,
            "read": read
        }
        if detail:
            data_detail = {
                "content": self.content,
                "create_at": format_time(self.create_at)
            }
            data.update(data_detail)
        return data
コード例 #13
0
 def to_json(self, return_answer=False):
     data = {
         "type": self.type,
         "order": self.order,
         "id": self.id,
         "content": self.content if self.type not in current_app.config['SELECT_TYPE'] else pickle.loads(self.content),
         "medias": Media.load_medias_from_uuid_list(pickle.loads(self.media)) if self.media is not None else None,
         "max_score": self.max_score,
         "create_at": format_time(self.create_at),
         "picture_exist": 1 if self.media is not None else 0
     }
     if return_answer:
         data_answer = {
             "answer": pickle.loads(self.answer) if self.answer is not None else None,
             # "answer_detail": self.detail_answer_to_json()
             "answer_detail": self.answer_detail
         }
         print(data_answer)
         data.update(data_answer)
     return data