Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionnal: have icon in message to precise status #220

Open
GayLaurent opened this issue Jan 26, 2016 · 1 comment
Open

Optionnal: have icon in message to precise status #220

GayLaurent opened this issue Jan 26, 2016 · 1 comment

Comments

@GayLaurent
Copy link

It will be nice to have, in optionnal, an icon associate for each message.
This icon will be associate to a status like "question", "info", "error", "resolved", ...

@DylannCordel
Copy link
Contributor

Hi @GayLaurent

It's a specific need you can resolve with a OneToOneFied (it's non tested code but you'll get the main idea) :

In your models.py :

from django.db import models
from pybb.models import Topic

class TopicExtender(models.Model):
    TYPE_CHOICES = (
        (TYPE_CLASSIC, ''),
    )
    topic = models.OneToOneField(
        Topic,
        on_delete=models.CASCADE,
        primary_key=True,
    )
    type = models.CharField(
        choices=TYPE_CHOICES, 
        default=TYPE_CLASSIC
    )
    closed = models.BooleanField()

In your forms.py :

from pybb.forms import PostForm
from .models import TopicExtender

class ExtendedTopicForm(PostForm):
    def __init__(self, *args, **kwargs):
        super(ExtendedTopicForm, self).__init__(*args, **kwargs)
        if not self.topic and self.forum:
            self.is_new_topic = True
            # add you new fields here

    def save(self, commit=True):
        super(ExtendedTopicForm, self).save(commit)
        if self.is_new_topic:
            self.topic_extender = TopicExtender(topic=self.topic)
            # assign your data here
            if commit:
                self.topic_extender.save()
            

In your views.py :

from pybb.views import AddPostView
from .forms import ExtendedTopicForm

class ExtendedAddPostView(AddPostView):
    post_form_class = ExtendedTopicForm

    def form_valid(self, form):
        response = super(ExtendedAddPostView, self).form_valid(form)
        # currently, you must check if response is a success (redirect) because 
        # form.save(commit=True) is not called in the super. So you must save your instance here.
        # Maybe we could add a `save_instances` method in `AddPostView` to make 
        # this type of code easier
        if hasattr(form, 'topic_extender') and isinstance(response, HttpResponseRedirect):
            topic_extender.save()
        return response

In your project's url.py, override the add_topic URL to use your own view.

If someone want to test this and create a PR to add this example in the doc, I think it would be useful for people. What do you think about it @lampslave and @skolsuper ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants