forms.py 1.52 KB
Newer Older
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
1 2 3 4 5 6
from datetime import datetime
from django import forms

from .models import News


7
class NewsForm(forms.Form):
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
8 9 10

    def update(self):
        news = News.objects.get(pk=self.data['news_id'])
11 12 13 14 15 16
        news = self._make(news)
        news.save()

    def create(self):
        news = News()
        news = self._make(news)
17
        news.company_id = self.data['entity']
18 19 20
        news.save()

    def _make(self, news):
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
21 22 23 24 25 26 27 28 29 30
        if self.data['title']:
            news.title = self.data['title']
        if self.data['description']:
            news.description = self.data['description']
        if self.data['url']:
            news.url = self.data['url']

        if self.data['radar']:
            news.radar_id = self.data['radar']
        if self.data['industry']:
31
            news.industry_id = [int(i) for i in self.data['industry'].split(',')]
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
32
        if self.data['function']:
33
            news.function_id = [int(i) for i in self.data['function'].split(',')]
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
34 35 36
        if self.data['media']:
            news.media_id = self.data['media']
        if self.data['technology']:
37
            news.technology_id = [int(i) for i in self.data['technology'].split(',')]
Vasyl Bodnaruk's avatar
Vasyl Bodnaruk committed
38 39 40 41 42 43 44 45 46 47
        if self.data['type']:
            news.type_id = self.data['type']
        if self.data['region']:
            news.region_id = self.data['region']
        if self.data['date']:
            news.publish_date = datetime.strptime(self.data['date'], '%Y-%m-%d')

        if self.data['tags']:
            news.tags_id = [int(i) for i in self.data['tags'].split(',')]

48
        return news