Bhupesh-V / tutorialdb

A search 🔎 engine for programming/dev tutorials,
MIT License
124 stars 62 forks source link

Improve forms. #23

Closed Animesh-Ghosh closed 5 years ago

Animesh-Ghosh commented 5 years ago

Description: Forms have hard-coded options. I couldn't access the field choices inside the app's views so as to pass them to the context of the required templates. This question looked appropriate and thus I followed the thread and arrived here.

According to the latter, we should remove them according to the DRY principle. This will make it easy to add/remove new categories as needed.

It will also make the queries simpler and no actual strings that are specified in the TUTORIAL_CATEGORIES tuple will have to be used again (either in the shell or otherwise).

Proposed Solution: Encapsulate the TUTORIAL_CATEGORIES tuple inside the tutorial model. Like so: From:

TUTORIAL_CATEGORIES = (
    ('article', 'article'),
    ('video', 'video'),
    ('course', 'course'),
    ('docs', 'docs'),
    ('book', 'book'),
    ('cheatsheet', 'cheatsheet'),
)
.
.
.
class tutorial(models.Model):
    title = models.CharField(max_length=200)
    link = models.URLField()
    tags = models.ManyToManyField(tag)
    category = models.CharField(max_length=200, choices = TUTORIAL_CATEGORIES)
    created_date = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.title

To:

class tutorial(models.Model):
    ARTICLE = 'article'
    BOOK = 'book'
    CHEATSHEET = 'cheatsheet'
    COURSE = 'course'
    DOCS = 'docs'
    VIDEO = 'video'

    CATEGORIES = (
        (ARTICLE, 'Article'),
    (BOOK, 'Book'),
    (CHEATSHEET, 'Cheatsheet'),
    (COURSE, 'Course'),
    (DOCS, 'Documentation'),
    (VIDEO, 'Video'),
    )

    title = models.CharField(max_length=200)
    link = models.URLField()
    tags = models.ManyToManyField(tag)
    category = models.CharField(max_length=20, choices=CATEGORIES)
    created_date = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.title

Alternatives: Leave the code as is. Forms remain difficult to update (atleast the selection box does).

Bhupesh-V commented 5 years ago
class tutorial(models.Model):
    ARTICLE = 'article'
    BOOK = 'book'
    CHEATSHEET = 'cheatsheet'
    COURSE = 'course'
    DOCS = 'docs'
    VIDEO = 'video'

    CATEGORIES = (
        (ARTICLE, 'Article'),
    (BOOK, 'Book'),
    (CHEATSHEET, 'Cheatsheet'),
    (COURSE, 'Course'),
    (DOCS, 'Documentation'),
    (VIDEO, 'Video'),
    )

    title = models.CharField(max_length=200)
    link = models.URLField()
    tags = models.ManyToManyField(tag)
    category = models.CharField(max_length=20, choices=CATEGORIES)
    created_date = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.title

The class is getting populated unnecessarily, plus the code is getting repeated

Animesh-Ghosh commented 5 years ago

It's abstracting away the category that will be saved in the database. Typing 'article', 'book', 'course' etc. each time you generate a query seems wrong.

By encapsulating the CATEGORIES tuple inside the tutorial class, I can access it as an attribute.

Animesh-Ghosh commented 5 years ago

24 closed.

Opened #25 in its place.

Animesh-Ghosh commented 5 years ago

25 merged.

Suggestion: create a forms.py file for all 2 of the forms?

Bhupesh-V commented 5 years ago

Found a bug for the first time you search something, the filter option in working but once you search something it gets diappeared

Bhupesh-V commented 5 years ago

Suggestion: create a forms.py file for all 2 of the forms?

No

Bhupesh-V commented 5 years ago

Found a bug for the first time you search something, the filter option in working but once you search something it gets diappeared

Fixed in 42f9cb8