<file_sep># Generated by Django 3.1 on 2020-08-09 07:29

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('activity', '0004_activitytimeline'),
    ]

    operations = [
        migrations.AlterField(
            model_name='activity',
            name='participant',
            field=models.ManyToManyField(blank=True, to='activity.Student'),
        ),
    ]
<file_sep>from django.http import HttpResponse
from django.shortcuts import render, redirect
from rest_framework import  viewsets

from .forms import ActivityForm
from .models import Activity
from .serializers import ActivitySerializer


class ActivityViewSet(viewsets.ModelViewSet) :
    queryset = Activity.objects.all()
    serializer_class = ActivitySerializer

# Create your views here.
def index(request):
    editActivity = None
    if 'editId' in request.GET:
        editActivity = Activity.objects.get(id=request.GET['editId'])

    if request.POST:
        createForm = ActivityForm(request.POST, instance=editActivity)
        if (createForm.is_valid()):
            newAct = createForm.save(commit=False)
            if 'isDelete' in request.POST :
                if request.POST['isDelete']:
                    newAct.delete()
            else :
                newAct.save()
        return redirect('/')

    activities = Activity.objects.all()
    if editActivity:
        activityForm = ActivityForm(instance=editActivity)
    else:
        activityForm = ActivityForm()

    return render(request, "activity/index.html", {
        'title': "Hello From Title",
        'activities': activities,
        "activityForm": activityForm,
        'editActivity': editActivity
    })
<file_sep>from django import  forms
from .models import Activity
class ActivityForm(forms.ModelForm) :
    class Meta :
        model = Activity
        fields = ['activity_name',
                  'activity_type',
                  'activity_desc']<file_sep>from .models import Activity, ActivityTimeline, Student
from rest_framework import serializers


class ActivityTimelineSerializer(serializers.ModelSerializer):
    class Meta:
        model = ActivityTimeline
        fields = ['id',
                  'activity_start',
                  'activity_stop',
                  'activity_plantitle']


class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = ['id', 'code', 'name']


class ActivitySerializer(serializers.ModelSerializer):
    participant = StudentSerializer(many=True,required=False)
    activitytimeline_set = ActivityTimelineSerializer(many=True,required=False)

    class Meta:
        model = Activity
        fields = ['id',
                  'activity_name',
                  'activity_type','activity_desc',
                  'activity_date','participant','activitytimeline_set']
<file_sep>from django.contrib import admin
from django.utils.html import format_html

from .models import Activity,Student,ActivityTimeline

# Register your models here.
class ActivityTimelineInline(admin.TabularInline) :
    model = ActivityTimeline



class ActivityAdmin(admin.ModelAdmin) :
    list_display = ['id','activity_name',
                    'act_type',
                    'activity_type',
                    'activity_date']
    list_display_links = ['id','activity_name']

    inlines = [
        ActivityTimelineInline
    ]

    COLORDICT = {
        'AC' : 'red',
        'CU' : 'green',
        'SA' : 'blue',
    }
    save_as = True

    fields = ('activity_name',
              'activity_type',
              'activity_desc',
              'participant')

    def act_type(self,obj):
        return format_html(
            '<span style="color : {}">{}</span>',
            self.COLORDICT[obj.activity_type],
            obj.activity_type
        )
    search_fields = ['activity_name']

    pass

admin.site.register(Activity,ActivityAdmin)
admin.site.register(Student)
admin.site.register(ActivityTimeline)
<file_sep>from django.db import models

# Create your models here.
ACTIVITY_TYPES = [
    ('AC', 'ACADEMIC'),
    ('SA', 'STUDENT AFFAIR'),
    ('CU', 'CULTURAL'),
]


class Student(models.Model):
    code = models.CharField(max_length=255)
    name = models.CharField(max_length=255)

    def __str__(self):
        return f'{self.code} - {self.name}'


class Activity(models.Model):
    class Meta:
        verbose_name_plural = "Activities"

    activity_name = models.CharField(max_length=255)
    activity_type = models.CharField(max_length=2, choices=ACTIVITY_TYPES)
    activity_desc = models.TextField(blank=True, null=True)
    activity_date = models.DateTimeField(auto_now_add=True)  # วันที่สร้างกิจกรรมขึ้นมาในระบบ

    participant = models.ManyToManyField(Student,blank=True)

    def __str__(self):
        return f'{self.activity_name} - {self.activity_date}'

    pass


class ActivityTimeline(models.Model):
    activity = models.ForeignKey(Activity, on_delete=models.CASCADE)
    activity_start = models.DateTimeField()
    activity_stop = models.DateTimeField(blank=True, null=True)
    activity_plantitle = models.CharField(max_length=255)
