from django.db import models
from django.db.models.signals import post_save
import mimetypes
import wellknown

#
# create default host-meta handler
#

from wellknown.resources import HostMeta
wellknown.register('host-meta', handler=HostMeta(), content_type='application/xrd+xml')

#
# resource model
#

class Resource(models.Model):
    path = models.CharField(max_length=128)
    content = models.TextField(blank=True)
    content_type = models.CharField(max_length=128, blank=True)
    
    class Meta:
        ordering = ('path',)
    
    def __unicode__(self):
        return self.path
    
    def save(self, **kwargs):
        self.path = self.path.strip('/')
        if not self.content_type:
            self.content_type = mimetypes.guess_type(self.path)[0] or 'text/plain'
        super(Resource, self).save(**kwargs)

#
# update resources when models are saved
#

def save_handler(sender, **kwargs):
    reg = kwargs['instance']
    wellknown.register(
        reg.path,
        content=reg.content,
        content_type=reg.content_type,
        update=True
    )

post_save.connect(save_handler, sender=Resource)

from django.db import models
from django.db.models.signals import post_save
import mimetypes
import wellknown

#
# create default host-meta handler
#

from wellknown.resources import HostMeta

try:
    wellknown.register('host-meta', handler=HostMeta(),
                       content_type='application/xrd+xml')
except ValueError:
    pass

#
# resource model
#

class Resource(models.Model):
    path = models.CharField(max_length=128)
    content = models.TextField(blank=True)
    content_type = models.CharField(max_length=128, blank=True)
    
    class Meta:
        ordering = ('path',)
    
    def __unicode__(self):
        return self.path
    
    def save(self, **kwargs):
        self.path = self.path.strip('/')
        if not self.content_type:
            self.content_type = mimetypes.guess_type(self.path)[0] or 'text/plain'
        super(Resource, self).save(**kwargs)

#
# update resources when models are saved
#

def save_handler(sender, **kwargs):
    reg = kwargs['instance']
    wellknown.register(
        reg.path,
        content=reg.content,
        content_type=reg.content_type,
        update=True
    )

post_save.connect(save_handler, sender=Resource)

Check for an existing handler before registering default host-meta handler.
Check for an existing handler before registering default host-meta handler.

  from django.db import models
  from django.db.models.signals import post_save
  import mimetypes
  import wellknown
  
  #
  # create default host-meta handler
  #
  
  from wellknown.resources import HostMeta
- wellknown.register('host-meta', handler=HostMeta(), content_type='application/xrd+xml')
+ 
+ try:
+     wellknown.register('host-meta', handler=HostMeta(),
+                        content_type='application/xrd+xml')
+ except ValueError:
+     pass
  
  #
  # resource model
  #
  
  class Resource(models.Model):
      path = models.CharField(max_length=128)
      content = models.TextField(blank=True)
      content_type = models.CharField(max_length=128, blank=True)
      
      class Meta:
          ordering = ('path',)
      
      def __unicode__(self):
          return self.path
      
      def save(self, **kwargs):
          self.path = self.path.strip('/')
          if not self.content_type:
              self.content_type = mimetypes.guess_type(self.path)[0] or 'text/plain'
          super(Resource, self).save(**kwargs)
  
  #
  # update resources when models are saved
  #
  
  def save_handler(sender, **kwargs):
      reg = kwargs['instance']
      wellknown.register(
          reg.path,
          content=reg.content,
          content_type=reg.content_type,
          update=True
      )
  
  post_save.connect(save_handler, sender=Resource)
  
Check for an existing handler before registering default host-meta handler.
## Code Before:
from django.db import models
from django.db.models.signals import post_save
import mimetypes
import wellknown

#
# create default host-meta handler
#

from wellknown.resources import HostMeta
wellknown.register('host-meta', handler=HostMeta(), content_type='application/xrd+xml')

#
# resource model
#

class Resource(models.Model):
    path = models.CharField(max_length=128)
    content = models.TextField(blank=True)
    content_type = models.CharField(max_length=128, blank=True)
    
    class Meta:
        ordering = ('path',)
    
    def __unicode__(self):
        return self.path
    
    def save(self, **kwargs):
        self.path = self.path.strip('/')
        if not self.content_type:
            self.content_type = mimetypes.guess_type(self.path)[0] or 'text/plain'
        super(Resource, self).save(**kwargs)

#
# update resources when models are saved
#

def save_handler(sender, **kwargs):
    reg = kwargs['instance']
    wellknown.register(
        reg.path,
        content=reg.content,
        content_type=reg.content_type,
        update=True
    )

post_save.connect(save_handler, sender=Resource)

## Instruction:
Check for an existing handler before registering default host-meta handler.
## Code After:
from django.db import models
from django.db.models.signals import post_save
import mimetypes
import wellknown

#
# create default host-meta handler
#

from wellknown.resources import HostMeta

try:
    wellknown.register('host-meta', handler=HostMeta(),
                       content_type='application/xrd+xml')
except ValueError:
    pass

#
# resource model
#

class Resource(models.Model):
    path = models.CharField(max_length=128)
    content = models.TextField(blank=True)
    content_type = models.CharField(max_length=128, blank=True)
    
    class Meta:
        ordering = ('path',)
    
    def __unicode__(self):
        return self.path
    
    def save(self, **kwargs):
        self.path = self.path.strip('/')
        if not self.content_type:
            self.content_type = mimetypes.guess_type(self.path)[0] or 'text/plain'
        super(Resource, self).save(**kwargs)

#
# update resources when models are saved
#

def save_handler(sender, **kwargs):
    reg = kwargs['instance']
    wellknown.register(
        reg.path,
        content=reg.content,
        content_type=reg.content_type,
        update=True
    )

post_save.connect(save_handler, sender=Resource)
