Fakultas Ilmu Komputer UI

forms.py 2.82 KB
Newer Older
igor lestin sianipar's avatar
igor lestin sianipar committed
1
from django import forms
2

3
from app.models import Materi, Category, RatingContributor
4
from authentication.models import User
5
import datetime
igor lestin sianipar's avatar
igor lestin sianipar committed
6

7
8
def year_choices():
    return[(r,r) for r in range(2000, datetime.date.today().year+1)]
9

igor lestin sianipar's avatar
igor lestin sianipar committed
10
class UploadMateriForm(forms.ModelForm):
11
12
13
    
    categories = forms.ModelMultipleChoiceField(queryset=Category.objects.all(),widget=forms.CheckboxSelectMultiple(attrs={'style' : 'column-count:2'}),required=True)
    #categories.widget.attrs["style"] = "column-count:2"
14
    release_year = forms.TypedChoiceField(coerce=int, choices=year_choices, initial=datetime.date.today().year)
15
16
17
18
    yt_video_id = forms.CharField(label="Youtube Video Id", \
    help_text="This is not required.<br>\
        Please insert only Youtube link videos! Take a note for the video id!<br>\
        Example : https://www.youtube.com/watch?v=DkJ-50GLi2I <br> has video id DkJ-50GLi2I", required=False)
19

igor lestin sianipar's avatar
igor lestin sianipar committed
20
21
    class Meta:
        model = Materi
22
        fields = ["title", "author", "publisher", "release_year",
23
                  "categories", "descriptions", "cover", "content", "yt_video_id"]
igor lestin sianipar's avatar
igor lestin sianipar committed
24
25
26
27

    def __init__(self, *args, **kwargs):
        super(UploadMateriForm, self).__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
28
29
            if (field_name == "categories"):
                continue
30
            field.widget.attrs["class"] = "form-control"
31
32
            field.widget.attrs["placeholder"] = field.initial
            field.initial = ""
33
34
        self.fields['categories'].widget.attrs.update({'class' : "native"})
        
35

36

37
38
39
class SuntingProfilForm(forms.ModelForm):
    class Meta:
        model = User
40
41
        fields = ["email","name","instansi", "nik", "alamat", "nomor_telpon",
                  "profile_picture", "linkedin",
42
43
                  "facebook", "twitter", "instagram", "biography",
                  "is_subscribing_to_material_comments"]
44

45
46
    def __init__(self, *args, **kwargs):
        super(SuntingProfilForm, self).__init__(*args, **kwargs)
47

48
        for field_name, field in self.fields.items():
49
            field.widget.attrs["class"] = "form-control"
50
51
52
53
54
55
56

        if any(self.errors):
            key = list(self.errors)[0]
            self.fields[key].widget.attrs["autofocus"] = ""
            

        self.fields["email"].widget.attrs["readonly"] = True
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74


class RatingContributorForm(forms.ModelForm):
    class Meta:
        model = RatingContributor
        fields = ['score', 'user']
        SCORE_CHOICE = (
            ('', 'Select score'),
            ('1', '1'),  # First one is the value of select option and second is the displayed value in option
            ('2', '2'),
            ('3', '3'),
            ('4', '4'),
            ('5', '5'),
        )
        widgets = {
            'score': forms.Select(choices=SCORE_CHOICE, attrs={'class': 'form-control'},),
            'user': forms.HiddenInput()
        }