class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) about = EditorMdField( help_text= "Write a long article about yourself, see; /u/@your_username/about/", verbose_name="About Yourself", blank=True, null=True, ) bio = models.CharField( help_text= "Write something short about yourself, this will appear in your profile.", max_length=260, blank=True, null=True, ) address = models.ManyToManyField(OtherAddressesOfUsers, blank=True) email_permission = models.BooleanField( help_text="Allow email notifications.", default=True) title = models.CharField( max_length=30, default="user", choices=make_choices(TITLES), verbose_name="title", help_text="Title", ) # company = TODO def __str__(self): return str(self.user)
class Issue(AbstractThreadedComments, Common, View, Vote): user = models.ForeignKey(User, on_delete=models.CASCADE) permlink = models.SlugField(max_length=200) title = models.CharField( max_length=200, help_text="Be sure to choose the best title", null=True, blank=True, ) utopic = models.ForeignKey(UTopic, on_delete=models.CASCADE) body = EditorMdField( null=True, blank=True, help_text="Your problem | question | or anything else" ) status = models.CharField( default="open", choices=make_choices(ISSUE_CHOICES), max_length=55, help_text="Status", null=True, blank=True, ) issue_id = models.IntegerField(default=0) class Meta: ordering = ["-created"] unique_together = [["utopic", "permlink"]] def save(self, *args, **kwargs): self.permlink = slugify(self.title) super().save(*args, **kwargs) @property def get_absolute_url(self): return reverse( "detail-issue", kwargs=dict( username=str(self.utopic.user), utopic_permlink=self.utopic.permlink, permlink=self.permlink, ), ) @property def get_dor(self): # TODO this function same in content.py models times = "" for f, t in second_convert(dor(self.body)).items(): if t != 0: times += f" {t}{f} " if times == "": return "0" return times
class OtherAddressesOfUsers(models.Model): "maybe ManyToManyField in UserProfile" choices = models.CharField( blank=True, null=True, max_length=15, choices=make_choices(FOLLOW), verbose_name="website", ) address = models.CharField( blank=True, null=True, max_length=50, verbose_name="write address / username" ) def __str__(self): return f"{self.choices} - {self.address}"
class ReportModel(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="reporter") content = models.ForeignKey(Content, on_delete=models.CASCADE, verbose_name="reported person") complaints = models.CharField(choices=make_choices(REPORTS), max_length=40, verbose_name="type of report") add = models.CharField( blank=True, null=True, max_length=600, verbose_name="Can you give more information ?", ) date = models.DateTimeField(auto_now_add=True)
class Content(AbstractThreadedComments, Common, View, Vote): user = models.ForeignKey(User, on_delete=models.CASCADE) permlink = models.SlugField(max_length=200) title = models.CharField( max_length=200, help_text="Be sure to choose the best title", null=True, blank=True, ) body = EditorMdField( null=True, blank=True, verbose_name="", help_text="Your content | problem | question | or anything else", ) utopic = models.ForeignKey( UTopic, on_delete=models.CASCADE, verbose_name="Your topic", help_text="Please, write your topic about your contents.", ) language = models.CharField( max_length=30, choices=make_choices(LANGUAGES), help_text="The language of your content", ) category = models.ForeignKey(Category, on_delete=models.CASCADE, help_text="select content category") tags = models.CharField( max_length=200, verbose_name="Keywords", help_text="Write your tags using spaces, max:5", ) image_address = models.URLField(null=True, blank=True) status = models.CharField( default="ready", max_length=30, choices=make_choices(STATUS_CHOICES), verbose_name="article's status", help_text= "if your article isn't ready to publish yet, select 'not ready to publish'.", ) contributors = models.ManyToManyField(User, blank=True, related_name="content_contributors") contributors_count = models.PositiveIntegerField( default=0, verbose_name="Total contributors count") class Meta: ordering = ["-created"] unique_together = [["user", "permlink"]] def __str__(self): return str(self.get_absolute_url) @property def get_absolute_url(self): return reverse( "content-detail", kwargs=dict(username=str(self.user), permlink=self.permlink), ) @property def is_exists(self): return self.__class__.objects.filter(user=self.user, permlink=self.permlink).exists() def generate_permlink(self): self.permlink = slugify(self.title) obj, redirect_is_exists = check_redirect_exists( old_path=self.get_absolute_url) if self.is_exists or redirect_is_exists: import random return self.permlink + str(random.randrange(99999999)) else: return self.permlink @property def get_dor(self): times = "" for f, t in second_convert(dor(self.body)).items(): if t != 0: times += f" {t}{f} " if times == "": return "0" return times def next_or_previous(self, next=True): queryset = self.__class__.objects.filter(user=self.user, utopic=self.utopic) index = list(queryset).index(queryset.get(id=self.id)) if next: try: return queryset[index - 1] except (IndexError, AssertionError): return None else: try: return queryset[index + 1] except (IndexError): return None @property def next_post(self): try: return self.next_or_previous().get_absolute_url except AttributeError: return False @property def previous_post(self): try: return self.next_or_previous(next=False).get_absolute_url except AttributeError: return False @property def other_content_of_this_topic(self): "left of content detail page section" return self.__class__.objects.filter( user=self.user, utopic=self.utopic).order_by("created") def save(self, *args, **kwargs): # always update self.image_address = get_first_image(self.body) self.tags = ready_tags(self.tags) self.last_update = timezone.now() super().save(*args, **kwargs) @property def description(self): renderer = mistune.Renderer(escape=False, parse_block_html=True) markdown = mistune.Markdown(renderer=renderer) return (BeautifulSoup(markdown(self.body), "html.parser").text[0:200].replace("\n", " ")) @property def get_last_commit(self): return self.commit_set.first() @property def get_contributors(self): return self.contributors.all()
class Content(AbstractThreadedComments, Common, View, Vote): user = models.ForeignKey(User, on_delete=models.CASCADE) permlink = models.SlugField(max_length=200) title = models.CharField( max_length=200, help_text="Be sure to choose the best title", null=True, blank=True, ) body = EditorMdField( null=True, blank=True, verbose_name="", help_text="Your content | problem | question | or anything else", ) utopic = models.ForeignKey( UTopic, on_delete=models.CASCADE, verbose_name="Your topic", help_text="Please, write your topic about your contents.", ) language = models.CharField( max_length=30, choices=make_choices(LANGUAGES), help_text="The language of your content", ) category = models.ForeignKey(Category, on_delete=models.CASCADE, help_text="select content category") tags = models.CharField( max_length=200, verbose_name="Keywords", help_text="Write your tags using spaces, max:5", ) image_address = models.URLField(null=True, blank=True) status = models.CharField( default="ready", max_length=30, choices=make_choices(STATUS_CHOICES), verbose_name="article's status", help_text= "if your article isn't ready to publish yet, select 'not ready to publish'.", ) class Meta: ordering = ["-created"] unique_together = [["user", "permlink"]] def __str__(self): return str(self.get_absolute_url) @property def get_absolute_url(self): return reverse( "content-detail", kwargs=dict(username=str(self.user), permlink=self.permlink), ) @property def get_dor(self): times = "" for f, t in second_convert(dor(self.body)).items(): if t != 0: times += f" {t}{f} " if times == "": return "0" return times def next_or_previous(self, next=True): filter_field = dict(user=self.user, utopic=self.utopic) n_or_p = NextOrPrevious(self.__class__, filter_field, self.id) if next: return n_or_p.next_query return n_or_p.previous_query @property def next_post(self): try: return self.next_or_previous().get_absolute_url except AttributeError: return False @property def previous_post(self): try: return self.next_or_previous(next=False).get_absolute_url except AttributeError: return False @property def other_content_of_this_topic(self): "left of content detail page section" return self.__class__.objects.filter( user=self.user, utopic=self.utopic).order_by("created") def save(self, *args, **kwargs): self.image_address = get_first_image(self.body) self.permlink = slugify(self.title) super().save(*args, **kwargs) @property def description(self): renderer = mistune.Renderer(escape=False, parse_block_html=True) markdown = mistune.Markdown(renderer=renderer) return (BeautifulSoup(markdown(self.body), "html.parser").text[0:200].replace("\n", " ")) @property def get_last_commit(self): return self.commit_set.first()