def set_slug(self, doc, s): if s.strip().lower() in ['new', 'edit', 'remove']: raise Exception("Title cant be '%s'" % s.strip()) s = slugify(s) spec = {'type': self['type'], 'slug': s} if '_id' in doc: spec.update({'_id': {'$ne': doc['_id']}}) _s = s i = 1 while True: i += 1 if not self.__class__.one(spec): break _s = "%s-%d" % (s, i) spec.update({'slug': _s}) doc['slug'] = _s
def post(self): name = self.get_argument('name') doc_type = self.get_argument('content_type', models.CONTENT_TYPE.ARTICLE) attachments = self.get_argument('attachments', '') no = int(self.get_argument("attachment_counter", 0)) is_new_doc = bool(int(self.get_argument('is_new_doc'), 0)) f = self.request.files[name][0] name = unique_filename([self.current_user.username, \ str(doc_type), f['filename'], str(len(f['body']))]) try: """ Save file to tmp. Check mime; dont trust f['content_type']; use /usr/bin/file instead. If OK, move back to /static/uploads """ tmppath = os.path.join(tempfile.gettempdir(), name) with open(tmppath, mode="wb") as file: file.write(f['body']) file_type = '' try: out = subprocess.Popen("/usr/bin/file -i %s" % tmppath, shell=True, stdout=subprocess.PIPE).communicate()[0] file_type = out.split()[1] except: return self.json_response('FILE TYPE UNKNOWN', 'ERROR') ext = guess_extension(file_type) if file_type not in ALLOWED_CONTENT_TYPES: try: os.remove(tmppath) except: pass return self.json_response('FILE TYPE NOT ALLOWED', 'ERROR') if is_new_doc: name = os.path.join("tmp", name) src = name + ext upload_path = self.settings.upload_path path = os.path.join(upload_path, src) shutil.move(tmppath, path) if file_type in IMAGE_CONTENT_TYPES: create_thumbnails(path, PIC_SIZES) thumb_src = name + '.s' + ext self.log("thumb is " + thumb_src) else: thumb_src = 'attachment.png' no += 1 filename = slugify(f['filename']) _ = self._ """ no#filetype#src#thumb_src#filename""" # save to doc if not is_new_doc: #type = self.get_argument('type', None).title() type = models.CONTENT_MAP[doc_type].title() if type: cls = getattr(models, type, None) if cls: try: id = 'slug' if cls.structure.has_key('slug') else 'username' doc = cls.one({type: doc_type, id: self.get_argument('slug')} ) if doc: doc['attachments'] += [dict(type=unicode(file_type), src=unicode(src), thumb_src=unicode(thumb_src, 'utf-8'), filename=unicode(filename, 'utf-8'))] doc.save() except Exception, e: return self.json_response(_("Failed adding attachment.") + e.__str__(), "ERROR") else: return self.json_response("Unknown document type", "ERROR") attachment = "%s#%s#%s#%s" % (file_type, src, thumb_src, filename) attachments = [a for a in attachments.split('$') if a] attachments = "$".join(attachments + [attachment]) html = self.html % dict(no=no, type=file_type, thumb_src=thumb_src, filename=filename) return self.json_response(None, "OK", dict(html=html, attachments=attachments, counter=no))
def post(self): name = self.get_argument('name') doc_type = self.get_argument('content_type', models.CONTENT_TYPE.ARTICLE) attachments = self.get_argument('attachments', '') no = int(self.get_argument("attachment_counter", 0)) is_new_doc = bool(int(self.get_argument('is_new_doc'), 0)) f = self.request.files[name][0] name = unique_filename([self.current_user.username, \ str(doc_type), f['filename'], str(len(f['body']))]) try: """ Save file to tmp. Check mime; dont trust f['content_type']; use /usr/bin/file instead. If OK, move back to /static/uploads """ tmppath = os.path.join(tempfile.gettempdir(), name) with open(tmppath, mode="wb") as file: file.write(f['body']) file_type = '' try: out = subprocess.Popen("/usr/bin/file -i %s" % tmppath, shell=True, stdout=subprocess.PIPE).communicate()[0] file_type = out.split()[1] except: return self.json_response('FILE TYPE UNKNOWN', 'ERROR') ext = guess_extension(file_type) if file_type not in ALLOWED_CONTENT_TYPES: try: os.remove(tmppath) except: pass return self.json_response('FILE TYPE NOT ALLOWED', 'ERROR') if is_new_doc: name = os.path.join("tmp", name) src = name + ext upload_path = self.settings.upload_path path = os.path.join(upload_path, src) shutil.move(tmppath, path) if file_type in IMAGE_CONTENT_TYPES: create_thumbnails(path, PIC_SIZES) thumb_src = name + '.s' + ext self.log("thumb is " + thumb_src) else: thumb_src = 'attachment.png' no += 1 filename = slugify(f['filename']) _ = self._ """ no#filetype#src#thumb_src#filename""" # save to doc if not is_new_doc: #type = self.get_argument('type', None).title() type = models.CONTENT_MAP[doc_type].title() if type: cls = getattr(models, type, None) if cls: try: id = 'slug' if cls.structure.has_key( 'slug') else 'username' doc = cls.one({ type: doc_type, id: self.get_argument('slug') }) if doc: doc['attachments'] += [ dict(type=unicode(file_type), src=unicode(src), thumb_src=unicode(thumb_src, 'utf-8'), filename=unicode(filename, 'utf-8')) ] doc.save() except Exception, e: return self.json_response( _("Failed adding attachment.") + e.__str__(), "ERROR") else: return self.json_response("Unknown document type", "ERROR") attachment = "%s#%s#%s#%s" % (file_type, src, thumb_src, filename) attachments = [a for a in attachments.split('$') if a] attachments = "$".join(attachments + [attachment]) html = self.html % dict( no=no, type=file_type, thumb_src=thumb_src, filename=filename) return self.json_response( None, "OK", dict(html=html, attachments=attachments, counter=no))