def createuser(self, username, email, password, active=False, send_email=True): """ A simple wrapper that creates a new :class:`User`. :param username: String containing the username of the new user. :param email: String containing the email address of the new user. :param password: String containing the password for the new user. :param active: Boolean that defines if the user requires activation by clicking on a link in an e-mail. Defaults to ``False``. :param send_email: Boolean that defines if the user should be sent an email. You could set this to ``False`` when you want to create a user in your own code, but don't want the user to activate through email. :return: :class:`User` instance representing the new user. """ new_user = get_user_model().objects.create_user( username=username, email=email, password=password, is_active=active, id=getRandomID()) userena_profile = self.create_userena_profile(new_user) if send_email: userena_profile.send_activation_email() return new_user
def uploadPhoto(request): if request.method == 'POST': file = request.FILES["uploader_input"] parser = ImageFile.Parser() for chunk in file.chunks(): parser.feed(chunk) img = parser.close() photo_id = getRandomID() filename = photo_id + '.jpg' file_path = MEDIA_ROOT + '/photo/' + filename if img.mode != "RGB": img = img.convert("RGB") img.save(file_path) res = upload_qiniu(file_path, filename) if not res: logging.error('上传失败') data = {"id": photo_id, "status": "007003", "key": filename} return HttpResponse(json.dumps(data), content_type="application/json") data = {"id": photo_id, "status": "107000", "key": filename} return HttpResponse(json.dumps(data), content_type="application/json")
def uploadAvatar(request): try: if request.method == 'POST': file = request.FILES["avatar_file"] parser = ImageFile.Parser() for chunk in file.chunks(): parser.feed(chunk) img = parser.close() photo_id = getRandomID() filename = photo_id + '.jpg' file_path = MEDIA_ROOT + '/avatar/' + filename try: if os.path.isdir(MEDIA_ROOT + '/avatar/'): shutil.rmtree(MEDIA_ROOT + '/avatar/') os.mkdir(MEDIA_ROOT + '/avatar/') except Exception as e: logging.error(e) return if img.mode != "RGB": img = img.convert("RGB") img.save(file_path) data = {"status": "114000", "key": filename} return HttpResponse(json.dumps(data), content_type="application/json") except Exception as e: logging.error(e) return render(request, '404.html', locals())
def newpost(request): try: if request.method == 'POST': title = request.POST['title'] content = request.POST['content'] doc = PyQuery(content).text() summary = doc[:100] tags = request.POST['tags'].split(' ') visible_status = request.POST['visible_status'], comment_status = request.POST['comment_status'], id = getRandomID() post = Post.objects.create( id=id, author=request.user, title=title, content=content, summary=summary, content_nohtml=doc, visible_status=int(request.POST['visible_status']), comment_status=int(request.POST['comment_status'])) for i in tags: try: tag = Tag.objects.get(name=i) except Tag.DoesNotExist: id = getRandomID() tag = Tag.objects.create(name=i, id=id) post.tags.add(tag) post.save() toAddEvent(EventType.POST, post) data = {'status': '105000'} return HttpResponse(json.dumps(data), content_type="application/json") except Exception as e: logger.error(e) data = {'status': '0'} return HttpResponse(json.dumps(data), content_type="application/json") else: return render(request, 'post/create.html', locals())
def editAlbum(request, id): if request.method == 'GET': try: album = get_object_or_404(Album, id=id) return render(request, 'album/edit_album.html', locals()) except Exception as e: logging.error(e) return render(request, 'album/index.html', locals()) elif request.method == 'POST': try: data = json.loads(request.body.decode("utf-8")) album = Album.objects.get(pk=id) tags, photos, album_desc, album_title = data["tags"], data["photos"], \ data["album_desc"], data["album_title"] album.tags.clear() for t in tags: tag, created = Tag.objects.get_or_create( name=t, defaults={'id': getRandomID()}) album.tags.add(tag) album.photos.clear() for p in photos: photo, created = Photo.objects.get_or_create(id=p["id"], defaults={ 'desc': p["desc"], 'url': p["url"], 'photos': album }) album.photos.add(photo) album.content = album_desc album.title = album_title album.save() toEditEvent(id, content=album.content, title=album.title, tags=album.tags.all(), photos=album.photos.all()) mydict = {'status': "106000"} return HttpResponse(json.dumps(mydict), content_type="application/json") except Exception as e: logging.error(e) mydict = {'status': "006000"} return HttpResponse(json.dumps(mydict), content_type="application/json")
def uploadAlbum(request): if request.is_ajax(): if request.method == 'POST': try: data = json.loads(request.body.decode("utf-8")) album = Album.objects.create(id=getRandomID(), author=request.user) tags, photos, album_desc, album_title = data["tags"], data[ "photos"], data["album_desc"], data["album_title"] for t in tags: tag_query = Tag.objects.filter(name=t) if tag_query.count(): tag = Tag.objects.get(name=t) else: tag = Tag.objects.create(id=getRandomID(), name=t) album.tags.add(tag) for p in photos: photo = Photo.objects.create(id=p["id"], desc=p["desc"], url=p["url"], photos=album) photo.save() album.content = album_desc album.title = album_title album.save() toAddEvent(EventType.ALBUM, album) mydict = {'status': "106000"} return HttpResponse(json.dumps(mydict), content_type="application/json") except Exception as e: logging.error(e) mydict = {'status': "006000"} return HttpResponse(json.dumps(mydict), content_type="application/json")
def editpost(request, id): if request.method == 'POST': title = request.POST['title'] content = request.POST['content'] summary = content[:100] tags = request.POST['tags'].split(' ') visible_status = request.POST['visible_status'] comment_status = request.POST['comment_status'] post = Post.objects.get(id=id) if post is None: logger.error('post is None') data = {'status': '0'} return HttpResponse(json.dumps(data), content_type="application/json") post.tags.clear() for i in tags: try: tag = Tag.objects.get(name=i) except Tag.DoesNotExist: id = getRandomID() tag = Tag.objects.create(name=i, id=id) post.tags.add(tag) post.save() Post.objects.filter(pk=id).update(title=title, content=content, summary=summary, visible_status=visible_status, comment_status=comment_status) # tags可能会有问题 toEditEvent(event_id=id, title=title, content=content, summary=summary, visible_status=visible_status, comment_status=comment_status, tags=post.tags.all()) data = {'status': '105001'} return HttpResponse(json.dumps(data), content_type="application/json") else: post = Post.objects.get(id=id) # tags = ' '.join([i.name for i in post.tags.all()]) return render(request, 'post/editpost.html', locals())
def toAddEvent(type, obj): event = Event.objects.create(id=getRandomID(), object_type=type, object_id=obj.id, author=obj.author, time=obj.time, lasttime=obj.lasttime, title=obj.title, content=obj.content, visible_status=obj.visible_status, comment_status=obj.comment_status, pwd=obj.pwd, like_count=obj.like_count, share_count=obj.share_count, comment_count=obj.comment_count) event.tags = obj.tags.all() if type == EventType.POST: event.summary = obj.summary event.cover = obj.cover else: event.photos = obj.photos.all() event.cover = obj.photos[0] event.save()