These are chat archives for django/django

28th
Jul 2018
MilanRgm
@MilanRgm
Jul 28 2018 05:24
How do i use Group features which is not now in channels 2?
class IncomingDataManager:
    def __init__(self, device_token, user, timestamp=None):
        self.device_token = device_token
        if not (user is None or isinstance(user, User)):
            user = User.objects.get(id=user)
        self.user = user
        self.ios = dict()
        if timestamp is None:
            timestamp = datetime.utcnow()
        self.timestamp = timestamp

    def sendToWebSocketClients(self):
          for deviceio_token, content in self.ios.items():
              try:
                  group_name = "{}-{}".format(self.device_token, deviceio_token)
                  message = {
                      "device_token": self.device_token,
                      "io_token": deviceio_token,
                      "value": str(content['value']),
                      "date_time": str(self.timestamp),
                      "io_name": content['name']
                  }
                  text = {
                      "text": json.dumps(message)
                  }
                  Group(group_name).send(text)
              except Exception as e:
                  '''Do nothing'''
          return
UmarMajeed-Rana
@UmarMajeed-Rana
Jul 28 2018 07:07
Hello Everyone what is best way to do api versioning for posting data in django app. I have a model which has field speed and old client send data in km/h new client send data in mph. Front end application now needs to show data in mph for both old and new clients. what's the best way to do it?
I know I can have different serializers for each version to fetch data and convert old clients data to mph. Is there better way to do this while storing in db? Like at insertion time update field in model to store mph?
MilanRgm
@MilanRgm
Jul 28 2018 08:42
I have a following kind of User and Role model. When I create a user form admin and then try to login from api i get unable to login error but it works if I create user from api instead of admin
from django.db import models
from django.dispatch import receiver
from django.contrib.auth.models import AbstractUser

from rest_framework.authtoken.models import Token

class Role(models.Model):

    ROLE_CHOICES = (
        ('agent', 'Agent'),
        ('agency', 'Agency'),
        ('manufacturer', 'Manufacturer'),
    )
    role = models.CharField(max_length=15, choices=ROLE_CHOICES)

    def __str__(self):
        return self.role


class User(AbstractUser):

    role = models.ForeignKey(
        Role,
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )

    def __str__(self):
        return self.username

    # def save(self, *args, **kwargs):
    #     if not self.pk:  # the instance is created
    #         self.role, created = Role.objects.get_or_create(role="agent")
    #     return super().save(*args, **kwargs)

@receiver(models.signals.post_save, sender=User)
def add_role_to_user(sender, instance, **kwargs):
    print ('instance in add role', instance)
    if kwargs.get('created'):
        try:
            role = Role.objects.get(user=instance)
            print('role', role)
        except Role.DoesNotExist:
            Role.objects.create(role="agent")
        try:
            token = Token.objects.get(user=instance)
            print ('token', token)
        except Token.DoesNotExist:
            Token.objects.create(user=instance)

# @receiver(models.signals.post_save, sender=User)
# def save_user_role(sender, instance, **kwargs):
#     print ('instance', instance)
#     instance.role.save()
When i query user model, i get that user but i cannot login
user = authenticate(**data) this returns none
MilanRgm
@MilanRgm
Jul 28 2018 10:38
what can be the reason for this?
Sanjeev Sharma
@thesanjeevsharma
Jul 28 2018 13:57
@funkybob I followed a tutorial online and installed boto3 and django-storages. I am using S3 to store my data. DB is in postgresql. In brief, its AWS EB + S3 + Postgresql.
1rjun
@1rjun
Jul 28 2018 18:11
hello i have a project in mind anyone interested plese pm me and i need only serious peoply people