Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @python_2_unicode_compatible
- class Post(MPTTModel, NamedModel):
- uid = models.UUIDField(max_length=8, primary_key=True, default=gen_uuid, editable=False)
- content = models.TextField(blank=True, default='')
- created_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE, related_name='+')
- created_on = models.DateTimeField(auto_now_add=True, auto_now=False)
- modified_on = models.DateTimeField(auto_now_add=False, auto_now=True)
- parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True, on_delete=models.CASCADE)
- _upvotes = models.IntegerField(blank=True, default=0)
- _downvotes = models.IntegerField(blank=True, default=0)
- wsi = models.FloatField(blank=True, default=0) # Wilson score interval
- ip_address = models.GenericIPAddressField(blank=True, null=True)
- user_agent = models.CharField(max_length=150, blank=True, null=True)
- @python_2_unicode_compatible
- class Thread(NamedModel):
- title = models.CharField(max_length=70, blank=False)
- slug = models.SlugField(unique=False, null=True)
- url = models.URLField(max_length=120, blank=True, default='')
- views = models.IntegerField(blank=True, default=0)
- topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
- op = models.ForeignKey('Post', related_name='+', on_delete=models.CASCADE)
- locked = models.BooleanField(blank=True, default=False)
- @python_2_unicode_compatible
- class Topic(NamedModel):
- alphanumeric = RegexValidator(r'^[0-9a-zA-Z ]*$', 'Only alphanumeric characters are allowed.')
- title = models.CharField(max_length=20, blank=False, unique=True, validators=[alphanumeric])
- description = models.CharField(max_length=120, blank=True, default='')
- @staticmethod
- def getTopic(title):
- try:
- return Topic.objects.get(title=title)
- except ObjectDoesNotExist:
- try:
- return Topic.objects.get(title=title.replace('-', ' '))
- except ObjectDoesNotExist:
- return Topic.objects.get(title=title.replace('_', ' '))
- from .models import Topic, Thread
- from django.db.models import Q
- def search_threads(search_term, topic_term=None):
- if topic_term:
- topic = Topic.getTopic(topic_term)
- threads_by_topic = Thread.objects.filter(topic=topic)
- threads = threads_by_topic.objects.filter(Q(title__icontains=search_term) | Q(op__content__icontains=search_term))
- else:
- threads = Thread.objects.filter(Q(title__icontains=search_term) | Q(op__content__icontains=search_term))
- return threads
Add Comment
Please, Sign In to add comment