예제 #1
0
파일: doc.py 프로젝트: gdbelvin/mailsafe
def create(request):
    author_email = request.POST["author_email"]
    text = request.POST["text"]
    subject = request.POST.get("subject")
    status = request.POST.get('status')

    author = Author.query(Author.email == author_email).get()
    if (author is None):
        return HttpResponseServerError("Author %s not found" % author_id)

    content = Content(author=author.key, text=text, subject=subject,
            status="draft")
    content.put()
    return HttpResponse(json_fixed.dumps(content))
예제 #2
0
    def post(self):
        if not self.user.administrator:
            return webapp2.redirect("/")

        mode = self.request.POST["mode"]

        if mode == "0":
            # Institution
            institution = Institution(name=self.request.POST["name"], website=self.request.POST["website"])
            institution.put()
        elif mode == "1":
            thumbnail_url = self.request.POST["thumbnail"]
            try:
                content = urllib2.urlopen(thumbnail_url)
                image = content.read()
            except urllib2.HTTPError:
                logging.warning("URL: " + thumbnail_url + "was not found.")
                image = ""

            institution = ndb.Key(urlsafe=self.request.POST["institution"])

            author = Author(
                name=self.request.POST["name"],
                website=self.request.POST["website"],
                thumbnail=image,
                institution=institution,
            )
            author.put()
        elif mode == "2":
            # Conference
            conference = Conference(name=self.request.POST["name"], acronym=self.request.POST["acronym"])
            conference.put()
            pass
        elif mode == "3":
            # Publication
            date = datetime.strptime(self.request.POST["date"], "%Y-%m-%d")

            # A bit messy, does author order
            authors = self.request.params.getall("authors")
            idx = 0
            author_order = [int(order_idx) for order_idx in self.request.POST["order"].split(",")]
            ordered_authors = []
            for author_idx in range(len(authors)):
                ordered_authors.append(ndb.Key(urlsafe=authors[author_order[author_idx] - 1]))

            conference = ndb.Key(urlsafe=self.request.POST["conference"])

            pdf_image_url = self.request.POST["pdfimage"]
            image = ""
            if pdf_image_url:
                try:
                    content = urllib2.urlopen(pdf_image_url)
                    image = content.read()
                except urllib2.HTTPError:
                    logging.warning("URL: " + pdf_image_url + "was not found.")

            publication = Publication(
                title=self.request.POST["title"],
                abstract=self.request.POST["abstract"],
                date=date,
                authors=ordered_authors,
                citation=self.request.POST["citation"],
                conference=conference,
                pdf=self.request.POST["pdf"],
                pdf_image=image,
                arxiv_link=self.request.POST["arxiv"],
                project_page=self.request.POST["projectpage"],
            )
            publication.put()
        elif mode == "4":
            # Content
            content = Content(name=self.request.POST["name"], content=self.request.POST["content"])
            content.put()
        elif mode == "5":
            # Project
            authors = []
            for author in self.request.params.getall("authors"):
                authors.append(ndb.Key(urlsafe=author))

            image_url = self.request.POST["image"]
            if image_url:
                try:
                    content = urllib2.urlopen(image_url)
                    image = content.read()
                except urllib2.HTTPError:
                    logging.warning("URL: " + image_url + "was not found.")
                    image = ""
            else:
                image = ""

            publications = []
            for publication in self.request.params.getall("publications"):
                publications.append(ndb.Key(urlsafe=publication))

            contents = []
            for content in self.request.params.getall("contents"):
                contents.append(ndb.Key(urlsafe=content))

            tags = []
            for tag in self.request.POST["tags"].split(","):
                # Try to find tag.
                stripped_tag = tag.strip()
                query = Tag.query(Tag.name == stripped_tag)
                if query.count() == 1:
                    query_tag = query.get(keys_only=True)
                    tags.append(query_tag)
                elif query.count() == 0:
                    query_tag = Tag(name=stripped_tag)
                    tags.append(query_tag.put())
                else:
                    logging.error("Tag count > 1 | < 0 (%s)." % stripped_tag)

            project = Project(
                title=self.request.POST["title"],
                description=self.request.POST["description"],
                authors=authors,
                image=image,
                publications=publications,
                extra_content=contents,
                tags=tags,
            )
            project.put()
        return self.get()