示例#1
0
def send_message(title, body, image, video, user_post):
    import uuid
    nameFile = str(uuid.uuid4())[:12]
    user_postv = User.objects.get(username=user_post)
    imgstr = re.search(r'base64,(.*)', image).group(1)
    # path = default_storage.save('%s.png' % nameFile, ContentFile(imgstr))
    img_file = open("media/%s.png" % nameFile, 'wb')
    img_file.write(base64.b64decode(imgstr))
    img_file.close()

    # user_postv = User.objects.get(username=user_post)
    post = Post()
    post.title = title
    post.body = body
    # post.image = default_storage.save('%s.png' % nameFile, ContentFile(imgstr))
    post.image = nameFile
    post.video = video
    # post.audio = audio
    post.user_post = user_postv
    post.save()

    user_post = str(user_post)

    r = redis.StrictRedis()

    if user_post:
        r.publish(
            "".join(["/"]),
            json.dumps({
                "timestamp": dateformat.format(post.date_post, 'U'),
                "image": nameFile,
                "user_post": user_post,
                "title": title,
                "id": post.id
            }))
示例#2
0
    def handle(self, *args, **options):
        # now do the things that you want with your models here
        password = "******"
        exp_posts = 50
        exp_users = 20
        avg_votes_per_post = 5
        avg_comments_per_post = 3

        # Post.objects.all().delete()

        rootExists = User.objects.filter(username="******").count()
        if (rootExists < 1):
            self.stdout.write("Creating root user")
            User.objects.create_superuser("root", "*****@*****.**", password)

        num_posts = Post.objects.all().count()
        num_users = User.objects.all().count()
        num_votes = Vote.objects.all().count()
        num_comments = Comment.objects.all().count()

        faker = Faker()

        self.stdout.write("Default password: "******"Number of posts: " + str(num_posts))
        self.stdout.write("Number of votes: " + str(num_votes))
        self.stdout.write("Number of comments: " + str(num_comments))
        self.stdout.write("Number of users: " + str(num_users))

        if (num_users < exp_users):
            self.stdout.write("Creating " + str(exp_users - num_users) +
                              " Users")
            for x in range(num_users + 1, exp_users + 1):
                username = faker.name().replace(" ", ".")
                email = username + "@test.com"
                User.objects.create_superuser(username, email, password)
                self.stdout.write("   " + username)
            num_users = User.objects.all().count()

        if (num_posts < exp_posts):
            self.stdout.write("Creating " + str(exp_posts - num_posts) +
                              " Posts")
            for x in range(num_posts + 1, exp_posts + 1):
                day_offset = randint(1, 364)
                pub_date = timezone.now() - datetime.timedelta(days=day_offset)
                u = self.randomUser()
                title = faker.text()
                self.stdout.write("   [" + u.username + "] " + title)
                rec = Post()
                rec.author = u
                rec.title = title
                rec.text = faker.text()
                rec.created_date = pub_date
                rec.published_date = pub_date
                rec.save()
            num_posts = Post.objects.all().count()

        self.stdout.write("Voting...")
        while (num_votes < avg_votes_per_post * num_posts):
            user = self.randomUser()
            post = self.randomPost()
            voted = Vote.objects.all().filter(post=post, author=user).count()
            if (post.author != user and voted == 0):
                rec = Vote()
                rec.author = user
                rec.post = post
                rec.created_date = self.randomDate(post.created_date,
                                                   timezone.now())
                rec.save()
                num_votes = Vote.objects.all().count()
                self.stdout.write(
                    str(num_votes) + ": " + user.username + " voted for [" +
                    post.title + "]")

        self.stdout.write("Commenting...")
        while (num_comments < avg_comments_per_post * num_posts):
            user = self.randomUser()
            post = self.randomPost()
            rec = Comment()
            rec.author = user
            rec.post = post
            rec.contents = faker.text()
            rec.created_date = self.randomDate(post.created_date,
                                               timezone.now())
            rec.save()
            num_comments = Comment.objects.all().count()
            self.stdout.write(
                str(num_comments) + ": " + user.username + " commented on [" +
                post.title + "]")