# Path: API/chat/models.py
# class Message(models.Model):
#     def __str__(self):
#         return self.message_content
# 
#     def to_dict(self):
#         serializable_fields = ('message_content', 'datetime_start', 'datetime_sent', 'username')
#         return {key: getattr(self, key) for key in serializable_fields}
# 
#     TEXT = 'text'
#     IMAGE = 'image'
# 
#     MESSAGE_TYPE = (
#         (TEXT, 'text'),
#         (IMAGE, 'image'),
#     )
# 
#     message_content = models.TextField(max_length=2000)
#     datetime_start = models.DateTimeField(default=None)
#     datetime_sent = models.DateTimeField(default=None, null=True)
#     typing = models.BooleanField(default=False)
#     username = models.CharField(max_length=20)
#     channel = models.ForeignKey(Channel)
#     message_type = models.CharField(max_length=10,
#                                     choices=MESSAGE_TYPE,
#                                     default=TEXT)
# 
# Path: API/chat/utils.py
# def timestamp_to_datetime(timestamp):
#     """
#     Takes a timestamp and returns
#     a datetime.
#     """
#     return datetime.datetime.fromtimestamp(timestamp / 1000.0).replace(tzinfo=pytz.UTC)
# 
# def datetime_to_timestamp(datetime):
#     """
#     Takes a datetime and returns
#     a unix epoch ms.
#     """
#     time_without_ms = time.mktime(datetime.timetuple()) * 1000
#     ms = int(datetime.microsecond / 1000)
# 
#     return time_without_ms + ms


import time
from django import forms
from .models import Message
from .utils import timestamp_to_datetime, datetime_to_timestamp
    def clean_datetime_start(self):
        now = int(round(time.time() * 1000))
        timestamp = int(self.data['datetime_start'])
        if now < timestamp:
            timestamp = now

        self.cleaned_data['datetime_start'] = timestamp_to_datetime(timestamp)

    def save(self):
        self.clean_datetime_start()

        message = Message.objects.create(channel=self.channel, **self.cleaned_data)

        if not message.typing:
            message.datetime_sent = message.datetime_start
            message.save()

        return message;


class MessagePatchForm(MessageForm):
    datetime_sent = forms.IntegerField()

    def save(self, message):
        timestamp_start = datetime_to_timestamp(message.datetime_start)
        timestamp_sent = int(self.cleaned_data['datetime_sent'])

        if timestamp_sent < timestamp_start:
            timestamp_sent = timestamp_start


# Path: API/chat/models.py
# class Message(models.Model):
#     def __str__(self):
#         return self.message_content
# 
#     def to_dict(self):
#         serializable_fields = ('message_content', 'datetime_start', 'datetime_sent', 'username')
#         return {key: getattr(self, key) for key in serializable_fields}
# 
#     TEXT = 'text'
#     IMAGE = 'image'
# 
#     MESSAGE_TYPE = (
#         (TEXT, 'text'),
#         (IMAGE, 'image'),
#     )
# 
#     message_content = models.TextField(max_length=2000)
#     datetime_start = models.DateTimeField(default=None)
#     datetime_sent = models.DateTimeField(default=None, null=True)
#     typing = models.BooleanField(default=False)
#     username = models.CharField(max_length=20)
#     channel = models.ForeignKey(Channel)
#     message_type = models.CharField(max_length=10,
#                                     choices=MESSAGE_TYPE,
#                                     default=TEXT)
# 
# Path: API/chat/utils.py
# def timestamp_to_datetime(timestamp):
#     """
#     Takes a timestamp and returns
#     a datetime.
#     """
#     return datetime.datetime.fromtimestamp(timestamp / 1000.0).replace(tzinfo=pytz.UTC)
# 
# def datetime_to_timestamp(datetime):
#     """
#     Takes a datetime and returns
#     a unix epoch ms.
#     """
#     time_without_ms = time.mktime(datetime.timetuple()) * 1000
#     ms = int(datetime.microsecond / 1000)
# 
#     return time_without_ms + ms


# Path: API/chat/forms.py
import time
from django import forms
from .models import Message
from .utils import timestamp_to_datetime, datetime_to_timestamp

    def clean_datetime_start(self):
        now = int(round(time.time() * 1000))
        timestamp = int(self.data['datetime_start'])
        if now < timestamp:
            timestamp = now

        self.cleaned_data['datetime_start'] = timestamp_to_datetime(timestamp)

    def save(self):
        self.clean_datetime_start()

        message = Message.objects.create(channel=self.channel, **self.cleaned_data)

        if not message.typing:
            message.datetime_sent = message.datetime_start
            message.save()

        return message;


class MessagePatchForm(MessageForm):
    datetime_sent = forms.IntegerField()

    def save(self, message):
        timestamp_start = datetime_to_timestamp(message.datetime_start)
        timestamp_sent = int(self.cleaned_data['datetime_sent'])

        if timestamp_sent < timestamp_start:
            timestamp_sent = timestamp_start


        message.datetime_sent = timestamp_to_datetime(timestamp_sent)