Exemplo n.º 1
0
    def post(self):
        if not self.authenticate(author=True):
            return

        content_url = url_factory.load_basic_parameters(self)
        content = self.models.content()

        if (not self.constants['single_user_site']
                and content_url["profile"] != self.current_user["username"]):
            raise tornado.web.HTTPError(400, "i call shenanigans")

        self.save_content(content, content_url, new=True)

        if content.album:
            cache.remove(
                self,
                self.nav_url(username=content.username,
                             section=content.section,
                             name=content.album))
        cache.remove(
            self,
            self.nav_url(username=content.username, section=content.section))

        if not content.hidden:
            socialize.socialize(self, content)
            self.sup_ping(content)

        self.set_header('Location', self.content_url(content))
        content.restricted = False

        content = self.ui["modules"].Content(content)

        self.write(self.ui["modules"].ContentView([content]))
        self.set_status(201)
Exemplo n.º 2
0
  def comment(self):
    commented_content = self.models.content.get(
        username=self.get_argument('local_user'),
        name=self.get_argument('local_name'))[0]
    self.display["user"] = commented_user = self.models.users.get(
        username=commented_content.username)[0]
    comment = self.get_argument('comment')
    sanitized_comment = content_remote.sanitize(comment)

    is_spam = spam.guess(comment, self.application.settings["private_path"],
        commented_content.username)

    post_remote = self.models.content_remote()
    post_remote.to_username = commented_content.username
    post_remote.from_user = self.current_user["email"]
    post_remote.username = self.current_user["email"].split('@')[0]
    local_url = ('/' + commented_content.username +
        '/remote-comments/comment-' + str(uuid.uuid4()))
    post_remote.post_id = ('tag:' + self.request.host + ',' +
        self.display["tag_date"] + ':' + local_url)
    post_remote.link = 'http://' + self.request.host + local_url
    from_username = post_remote.username
    if self.current_user["user"]:
      profile = self.get_author_user()
      post_remote.avatar = profile.logo if profile.logo else profile.favicon
    else:
      post_remote.avatar = ('http://www.gravatar.com/avatar/' +
          hashlib.md5(self.current_user["email"].lower()).hexdigest())
    post_remote.date_created = datetime.datetime.utcnow()
    if is_spam:
      post_remote.is_spam = True
    else:
      spam.train_ham(comment, self.application.settings["private_path"],
          commented_content.username)
    post_remote.type = 'comment'
    post_remote.local_content_name = commented_content.name
    post_remote.view = sanitized_comment
    post_remote.save()

    commented_content.comments_count += 1
    commented_content.comments_updated = datetime.datetime.utcnow()
    commented_content.save()

    cache.remove(self, self.content_url(commented_content))
    socialize.socialize(self, commented_content)
    smtp.comment(self, from_username, post_remote.from_user,
        commented_user.email, self.content_url(commented_content, host=True),
        sanitized_comment)
Exemplo n.º 3
0
    def put(self):
        if not self.authenticate(author=True):
            return

        content_url = url_factory.load_basic_parameters(self)
        content = self.models.content.get(username=content_url["profile"],
                                          section=content_url["section"],
                                          name=content_url["name"])[0]

        if not content:
            raise tornado.web.HTTPError(404)

        if (not self.constants['single_user_site']
                and content.username != self.current_user["username"]):
            raise tornado.web.HTTPError(400, "i call shenanigans")

        old_section = content.section
        old_album = content.album
        old_name = content.name
        old_hidden = content.hidden

        # remove old cached content
        if content.album:
            cache.remove(
                self,
                self.nav_url(username=content.username,
                             section=content.section,
                             name=content.album))
        cache.remove(
            self,
            self.nav_url(username=content.username, section=content.section))
        cache.remove(self, self.content_url(content))

        section = (self.get_argument('section')
                   if self.get_argument('section') else content_url["section"])
        name = (self.get_argument('name')
                if self.get_argument('name', "") else content_url["name"])
        hidden = int(self.get_argument('hidden', 0))

        if content.section == 'main' and old_hidden != hidden:
            collection = self.models.content.get(username=content.username,
                                                 section=old_name)[:]
            for item in collection:
                item.hidden = hidden
                item.save()

        if content.album == 'main' and old_hidden != hidden:
            collection = self.models.content.get(username=content.username,
                                                 section=old_section,
                                                 album=old_name)[:]
            for item in collection:
                item.hidden = hidden
                item.save()

        did_redirect = False
        if content.section == 'main' and old_name != name:
            did_redirect = True
            new_section = content_logic.rename_section(self, old_name, name,
                                                       content.title)

        if content.album == 'main' and old_name != name:
            did_redirect = True
            new_album = content_logic.rename_album(self, content.section,
                                                   old_name, name,
                                                   content.title)

        try:
            self.save_content(content, content_url, new=False)
        except tornado.web.HTTPError as ex:
            if ex.status_code == 400:
                # dup
                return
            else:
                # re-raise
                raise ex

        # remove cached in possible new album and new sections
        if content.album:
            cache.remove(
                self,
                self.nav_url(username=content.username,
                             section=content.section,
                             name=content.album))
        cache.remove(
            self,
            self.nav_url(username=content.username, section=content.section))

        if content.album == 'main' and old_section != content.section:
            album_name = new_album if did_redirect else old_name
            album_items = self.models.content.get(username=content.username,
                                                  album=album_name)[:]
            for item in album_items:
                item.section = (album_name if content.section == 'main' else
                                content.section)
                if content.section == 'main':
                    item.album = ""
                item.save()

        if content.name != old_name and not did_redirect:
            content_logic.create_redirect(self, content, old_section, old_name)

        if content.name != old_name or content.section != old_section:
            self.set_header('Location', self.content_url(content))

        if not content.hidden:
            socialize.socialize(self, content)

        self.set_status(204)