Guest User

Untitled

a guest
Jan 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1.  
  2. @python_2_unicode_compatible
  3. class Post(MPTTModel, NamedModel):
  4.     uid = models.UUIDField(max_length=8, primary_key=True, default=gen_uuid, editable=False)
  5.     content = models.TextField(blank=True, default='')
  6.     created_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE, related_name='+')
  7.     created_on = models.DateTimeField(auto_now_add=True, auto_now=False)
  8.     modified_on = models.DateTimeField(auto_now_add=False, auto_now=True)
  9.     parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True, on_delete=models.CASCADE)
  10.     _upvotes = models.IntegerField(blank=True, default=0)
  11.     _downvotes = models.IntegerField(blank=True, default=0)
  12.     wsi = models.FloatField(blank=True, default=0) # Wilson score interval
  13.     ip_address = models.GenericIPAddressField(blank=True, null=True)
  14.     user_agent = models.CharField(max_length=150, blank=True, null=True)
  15.  
  16. @python_2_unicode_compatible
  17. class Thread(NamedModel):
  18.     title = models.CharField(max_length=70, blank=False)
  19.     slug = models.SlugField(unique=False, null=True)
  20.     url = models.URLField(max_length=120, blank=True, default='')
  21.     views = models.IntegerField(blank=True, default=0)
  22.     topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
  23.     op = models.ForeignKey('Post', related_name='+', on_delete=models.CASCADE)
  24.     locked = models.BooleanField(blank=True, default=False)
  25.  
  26. @python_2_unicode_compatible
  27. class Topic(NamedModel):
  28.     alphanumeric = RegexValidator(r'^[0-9a-zA-Z ]*$', 'Only alphanumeric characters are allowed.')
  29.     title = models.CharField(max_length=20, blank=False, unique=True, validators=[alphanumeric])
  30.     description = models.CharField(max_length=120, blank=True, default='')
  31.  
  32.     @staticmethod
  33.     def getTopic(title):
  34.         try:
  35.             return Topic.objects.get(title=title)
  36.         except ObjectDoesNotExist:
  37.             try:
  38.                 return Topic.objects.get(title=title.replace('-', ' '))
  39.             except ObjectDoesNotExist:
  40.                 return Topic.objects.get(title=title.replace('_', ' '))
  41.  
  42.  
  43. from .models import Topic, Thread
  44. from django.db.models import Q
  45. def search_threads(search_term, topic_term=None):
  46.     if topic_term:
  47.         topic = Topic.getTopic(topic_term)
  48.         threads_by_topic = Thread.objects.filter(topic=topic)
  49.         threads = threads_by_topic.objects.filter(Q(title__icontains=search_term) | Q(op__content__icontains=search_term))
  50.     else:
  51.         threads = Thread.objects.filter(Q(title__icontains=search_term) | Q(op__content__icontains=search_term))
  52.     return threads
Add Comment
Please, Sign In to add comment