コード例 #1
0
ファイル: fill_database.py プロジェクト: symfall/api
    def handle(self, *args, **options):
        faker = Faker("en_US")

        self.stdout.write("Start filling data")

        users = []
        for index in range(options["len"]):
            self.stdout.write(f"Process {index} line of User")

            user, _ = User.objects.get_or_create(
                username=faker.profile()["username"])
            user.first_name = faker.first_name()
            user.last_name = faker.last_name()
            user.email = faker.email()
            user.set_password("1234")
            user.save()
            users.append(user)

        for index in range(options["len"]):

            random_range = randrange(1000)

            self.stdout.write(f"Process {index} line of Chat -> "
                              f"Message {random_range} ->? File")

            chat = Chat()
            chat.title = faker.sentence()[0:40]
            chat.creator = choice(users)
            chat.save()

            for _ in range(random_range):
                message = Message()
                message.sender = choice(users)
                message.chat = chat
                message.message = faker.text()
                message.save()

                if choice([True, False, False, False]):
                    image_file = io.BytesIO()
                    image = Image.new(
                        "RGBA",
                        size=(100, 100),
                        color=(
                            randrange(255),
                            randrange(255),
                            randrange(255),
                        ),
                    )
                    image.save(image_file, "png")
                    image_file.name = "test.png"
                    image_file.seek(0)

                    file = File()
                    file.document = files.File(image_file)
                    file.message = message
                    file.save()

        self.stdout.write("End filling data")
コード例 #2
0
    def receive(self, text_data):
        data = json.loads(text_data)
        chat_obj = Chat.objects.get(pk=data['chat'])
        sender = chat_obj.group.users.all().filter(username=self.user_name)

        if chat_obj and sender:
            sender = sender[0]
            datetimeObj = datetime.now()
            created_time = datetimeObj.strftime("%b %d %Y %I:%M%p")

            # set last chat used by user.
            sender.selected_chat = chat_obj
            sender.save()

            # Save message to database
            message_obj = Message()
            # sender is in list format, get first sender
            message_obj.sender = sender
            message_obj.chat = chat_obj
            message_obj.text = data['message']
            if 'image' in data:

                # Generate a unique file name
                chars = string.ascii_letters + string.digits + '!@#$%^&*()'
                random.seed = (os.urandom(1024))
                filename = ''.join(random.choice(chars) for i in range(23))

                # Save to message object
                message_obj.image = base64_file(data=data['image'],
                                                name=filename)
            message_obj.save()

            # Emit message to online users
            for user in chat_obj.group.users.all():
                async_to_sync(self.send_user_message)(user.username,
                                                      message_obj,
                                                      created_time)