def post(self, pub_no=None): try: pub = Publication() pub_json = json.loads(self.request.body) pub.no_babillard = int(pub_json['noBabillard']) pub.texte = pub_json['texte'] pub.date = datetime.datetime.strptime(pub_json['date'], '%Y-%m-%d') pub.noBabillard = int(pub_json['noBabillard']) pub.noCreateur = int(pub_json['noCreateur']) cle_pub = pub.put() self.response.set_status(201) self.response.headers['Location'] = (self.request.url + '/' + str(cle_pub.id())) self.response.headers['Content-Type'] = ('application/json;' + ' charset=utf-8') pub_dict = pub.to_dict() pub_dict["no"] = pub.key.id() pub_json = json.dumps(pub_dict, default=serialiser_pour_json) self.response.out.write(pub_json) except (db.BadValueError, ValueError, KeyError): logging.error("%s", traceback.format_exc()) self.error(400) except Exception: logging.error("%s", traceback.format_exc()) self.error(500)
def put(self, pub_no): try: cle = ndb.Key("Publication", int(pub_no)) pub = cle.get() pub_json = json.loads(self.request.body) if pub is None: status = 201 pub = Publication(key=cle) else: if int(pub_json['noCreateur']) == int(pub.noCreateur): status = 200 pub.texte = str(pub_json['texte']) pub.date = datetime.datetime.strptime(pub_json["date"], '%Y-%m-%d') pub.noBabillard = int(pub_json['noCreateur']) pub.put() else: status = 400 return pub_dict = pub.to_dict() pub_dict["no"] = pub.key.id() pub_json = json.dumps(pub_dict, default=serialiser_pour_json) self.response.set_status(status) self.response.headers['Content-Type'] = ('application/json;' + ' charset=utf-8') self.response.out.write(pub_json) # Exceptions en lien avec les données fournies. except (db.BadValueError, ValueError, KeyError): logging.error("%s", traceback.format_exc()) self.error(400) # Exceptions graves lors de l'exécution du code. except Exception: logging.error("%s", traceback.format_exc()) self.error(500)
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()