示例#1
0
 def test_get_by_week(self):
     year = self.snippets[0].year
     week = self.snippets[0].week
     user_id = self.snippets[0].user_id
     found = Snippet.get_by_week(user_id, year, week)
     self.assertIsNotNone(found)
     self.assertEqual(self.snippets[0].id, found.id)
示例#2
0
    def test_update_week_existing(self):
        # write the week
        Snippet.update(self.user.id, 2017, 9, "foo", ["blue", "red"])
        self.assertIsNotNone(Snippet.get_by_week(self.user.id, 2017, 9))

        # use api to update the week
        json = {"text": "bar", "tags": ["green"]}
        resp = self.post("/api/weeks/2017/9", self.valid_api_headers(), json)
        self.assertEqual(200, resp.status_code)

        # ensure current week is updated properly
        updated = Snippet.get_by_week(self.user.id, 2017, 9)
        self.assertIsNotNone(updated)
        self.assertEqual("bar", updated.text)
        self.assertSetEqual({"green"}, {tag.text for tag in updated.tags})
        self.assertEqual(2017, updated.year)
        self.assertEqual(9, updated.week)
示例#3
0
def get_week(year=None, week=None):
    if not year or not week:
        (year, week) = this_week()
    snippet = Snippet.get_by_week(g.current_user.id, year, week)
    if snippet is None:
        # don't worry about actually adding to the database until user saves
        snippet = Snippet(user_id=g.current_user.id, year=year, week=week)
    return jsonify(snippet.to_json())
示例#4
0
    def test_update_week_new(self):
        # verify the week hasn't been written yet
        self.assertIsNone(Snippet.get_by_week(self.user.id, 2017, 9))

        # use api to update the week
        json = {"text": "foo", "tags": ["blue", "red"]}
        resp = self.post("/api/weeks/2017/9", self.valid_api_headers(), json)
        self.assertEqual(200, resp.status_code)

        # check that the week was updated properly
        snippet = Snippet.get_by_week(self.user.id, 2017, 9)
        self.assertIsNotNone(snippet)
        self.assertEqual("foo", snippet.text)
        self.assertSetEqual({"blue", "red"},
                            {tag.text
                             for tag in snippet.tags})
        self.assertEqual(2017, snippet.year)
        self.assertEqual(9, snippet.week)
示例#5
0
def render_snippet_form(
    form: SnippetsForm, user: User, year: int, week: int
) -> Text:
    snippet = Snippet.get_by_week(user.id, year, week)
    text = snippet and snippet.text
    form.text.data = text
    form.year.data = year
    form.week.data = week
    tags = snippet and sorted(tag.text for tag in snippet.tags) or []
    form.tags.data = ", ".join(tags)
    return render_template(
        "edit.html.j2",
        name=user.name,
        user_id=user.id,
        week_begin=date.fromisocalendar(year, week, 1),
        content=text and markdown.markdown(text),
        form=form,
        tags=tags,
    )
示例#6
0
 def test_get_by_week_missing_week(self):
     year = self.snippets[0].year
     week = self.snippets[0].week + 1
     user_id = self.snippets[0].user_id
     self.assertIsNone(Snippet.get_by_week(user_id, year, week))
示例#7
0
 def test_get_by_week_no_such_user(self):
     year = self.snippets[0].year
     week = self.snippets[0].week
     user_id = self.snippets[0].user_id + 1
     self.assertIsNone(Snippet.get_by_week(user_id, year, week))