class Consumer(models.Model): name = models.CharField(max_length=255) description = models.TextField() key = models.CharField(max_length=KEY_SIZE) secret = models.CharField(max_length=SECRET_SIZE) status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending') user = models.ForeignKey(User, null=True, blank=True, related_name='consumers') objects = ConsumerManager() def __unicode__(self): return u"Consumer %s with key %s" % (self.name, self.key) def generate_random_codes(self): """ Used to generate random key/secret pairings. Use this after you've added the other data in place of save(). c = Consumer() c.name = "My consumer" c.description = "An app that makes ponies from the API." c.user = some_user_object c.generate_random_codes() """ key = User.objects.make_random_password(length=KEY_SIZE) secret = generate_random(SECRET_SIZE) while Consumer.objects.filter(key__exact=key, secret__exact=secret).count(): secret = generate_random(SECRET_SIZE) self.key = key self.secret = secret self.save()
class Consumer(models.Model): name = models.CharField(max_length=255) description = models.TextField() key = models.CharField(max_length=CONSUMER_KEY_SIZE) secret = models.CharField(max_length=SECRET_SIZE, blank=True) status = models.SmallIntegerField(choices=CONSUMER_STATES, default=PENDING) user = models.ForeignKey(User, null=True, blank=True) objects = ConsumerManager() def __unicode__(self): return u"Consumer %s with key %s" % (self.name, self.key) def generate_random_codes(self): """ Used to generate random key/secret pairings. Use this after you've added the other data in place of save(). """ key = generate_random(length=KEY_SIZE) secret = generate_random(length=SECRET_SIZE) while Consumer.objects.filter(models.Q(key__exact=key) | models.Q(secret__exact=secret)).count(): key = generate_random(length=KEY_SIZE) secret = generate_random(length=SECRET_SIZE) self.key = key self.secret = secret self.save()
class Consumer(models.Model): name = models.CharField(max_length=255) callback = models.CharField(max_length=255) key = models.CharField(max_length=CONSUMER_KEY_SIZE) secret = models.CharField(max_length=SECRET_SIZE, blank=True) user = models.ForeignKey(User, null=True, blank=True) objects = ConsumerManager() def __unicode__(self): return u"Consumer %s with key %s" % (self.name, self.key) def generate_random_codes(self): key = User.objects.make_random_password(length=KEY_SIZE) secret = User.objects.make_random_password(length=SECRET_SIZE) while Consumer.objects.filter(key__exact=key, secret__exact=secret).count(): secret = User.objects.make_random_password(length=SECRET_SIZE) self.key = key self.secret = secret self.save()
class Consumer(models.Model): name = models.CharField(max_length=100) description = models.TextField() key = models.CharField(max_length=CONSUMER_KEY_SIZE) secret = models.CharField(max_length=SECRET_SIZE, blank=True) status = models.SmallIntegerField(choices=CONSUMER_STATES, default=PENDING) user = models.ForeignKey(User, null=True, blank=True) objects = ConsumerManager() def __unicode__(self): return u"Consumer '%s' with key '%s'" % (self.name, self.key) def generate_random_codes(self): """ Used to generate random key/secret pairings. Use this after you've added the other data in place of save(). """ self.key = uuid.uuid4().hex self.secret = generate_random(length=SECRET_SIZE) self.save()
class Consumer(models.Model): name = models.CharField(max_length=255) description = models.TextField() key = models.CharField(max_length=KEY_SIZE) secret = models.CharField(max_length=SECRET_SIZE) status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending') user = models.ForeignKey(User, null=True, blank=True, related_name='consumers') objects = ConsumerManager() def __unicode__(self): return u"Consumer %s with key %s" % (self.name, self.key) def generate_random_codes(self): key = User.objects.make_random_password(length=KEY_SIZE) secret = User.objects.make_random_password(length=SECRET_SIZE) while Consumer.objects.filter(key__exact=key, secret__exact=secret).count(): secret = User.objects.make_random_password(length=SECRET_SIZE) self.key = key self.secret = secret self.save() # -- def save(self, **kwargs): super(Consumer, self).save(**kwargs) if self.id and self.user: subject = "API Consumer" rcpt = [ self.user.email, ] if self.status == "accepted": template = "api/mails/consumer_accepted.txt" subject += " was accepted!" elif self.status == "canceled": template = "api/mails/consumer_canceled.txt" subject += " has been canceled" else: template = "api/mails/consumer_pending.txt" subject += " application received" for admin in settings.ADMINS: bcc.append(admin[1]) body = loader.render_to_string(template, { 'consumer': self, 'user': self.user }) send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, rcpt, fail_silently=True) if self.status == 'pending': mail_admins(subject, body, fail_silently=True) if settings.DEBUG: print "Mail being sent, to=%s" % rcpt print "Subject: %s" % subject print body