Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @receiver(post_save, sender=CompanyInfo)
- def create_url_company(sender, instance, created, **kwargs):
- """
- Run from CompanyInfo ( post_save signals )
- Set new url path or add new path
- """
- if instance.is_approved and instance.is_valid:
- slug_name = slugify(instance.name) # Company Name to URI string
- create_url_show = lambda slug, company: reverse('company_profile_info', kwargs=dict(company_name=slug))
- create_url_report = lambda slug, company: reverse('report-company-scam', kwargs=dict(company_name=slug))
- create_url_complaints = lambda slug, company: reverse('company-complaints',
- kwargs=dict(company_name=slug,
- cat=company.category.slug if company.category else 'none'))
- actions = {'show': create_url_show,
- 'complaints': create_url_complaints,
- 'report': create_url_report}
- if instance.slug == slug_name:
- del actions['show']
- del actions['report']
- for create_url, action in actions.iteritems():
- slug = instance.slug if instance.slug else ''
- content_type = ContentType.objects.get_for_model(instance)
- # check exists url for other objects
- new_slug = slug_name
- i = Url.objects.filter(content_type=content_type,
- action=action, path=create_url(new_slug, instance))\
- .exclude(object_id=instance.pk).count()
- if i:
- new_slug = '%s_%d' % (slug_name, i)
- # get or create new url object for CompanyInfo instance
- new_url, is_created = Url.objects.get_or_create(content_type=content_type, action=action,
- path=create_url(new_slug, instance), object_id=instance.pk)
- if not is_created:
- # if url was exists cancel redirect
- Url.objects.filter(pk=new_url.pk).update(redirect=Url.DIRECT)
- # set redirect mode (301) for other exists url for this object CompanyInfo
- Url.objects.filter(content_type=content_type, action=action, object_id=instance.pk)\
- .exclude(pk=new_url.pk)\
- .update(redirect=Url.MOVED_PERMANENTLY)
- CompanyInfo.objects.filter(pk=instance.pk).update(slug=new_slug)
Advertisement
Add Comment
Please, Sign In to add comment