def add_post(request): comment = request.POST.get("comment").split(",") tags = request.POST.get("tags").split(",") user_details = { "first_name": request.POST.get("first_name"), "last_name": request.POST.get("last_name") } post = ObjectId(post_title=request.POST.get("post_title"), post_description=request.POST.get("post_description"), comment=comment, tags=tags, user_details=user_details) post.save() return HttpResponse("Inserted")
def data_and_condition_handler(obj, survey_id=None): if isinstance(obj, dict): for (k, v) in obj.items(): obj[k] = data_and_condition_handler(v, survey_id) elif isinstance(obj, list): for k, v in enumerate(obj): obj[k] = data_and_condition_handler(v, survey_id) elif isinstance(obj, ObjectId): obj = ObjectId(obj) elif isinstance(obj, str): if not obj.startswith(('date_', 'regex_')): return obj s = obj.split('_') if s[0] == 'date': d = s[1].split('-') obj = datetime(int(d[0]), int(d[1]), int(d[2])) elif s[0] == 'regex': obj = compile(s[1]) elif isinstance(obj, FileStorage): if not survey_id or not ObjectId.is_valid(survey_id): raise ValueError('invalid survey_id!') filename = '{}_{}_{}'.format(uuid1(), obj.name, obj.filename) file_path = join(FILE_FOLDER, survey_id, filename) obj.save(file_path) if obj.mimetype.startswith('image/') and obj.mimetype != 'image/gif': img = Image.open(file_path) if getsize(file_path) > 102400: img.thumbnail((img.width / 4, img.height / 4)) out = BytesIO() img.save(out, 'jpeg', quality=25) out.seek(0) data_url = b64encode(out.read()).decode() obj = 'img|{}|{}'.format(filename, data_url) else: obj = 'file|{}'.format(filename) return obj