newmediamonitoring/polls/models.py

287 lines
10 KiB
Python

from django.db import models
import uuid
import datetime
from django.contrib.auth.models import User
from django.db import models
from dashboard.models import Group, Group_user, NewMedia
VERIFY_CODE_TYPE_CHOICES = (
(0, 'register'),
(1, 'password_recover'),
)
class VerifyCode(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
code = models.CharField('code', max_length=8, null=False)
phone = models.CharField('phone', max_length=11, null=False)
timeouted = models.DateTimeField('timeouted', null=False)
category = models.IntegerField(
'category', choices=VERIFY_CODE_TYPE_CHOICES, default=0)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["-added"]
def is_in_progress(self):
now = datetime.datetime.now()
return now <= self.timeouted
def __str__(self):
return self.phone + ':' + self.code
NOTICE_TYPE_CHOICES = (
(0, 'normal'),
(1, 'reply'),
(2, 'news')
)
class Notice(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
user = models.ForeignKey(User, on_delete=models.CASCADE)
type = models.IntegerField(
'category', choices=NOTICE_TYPE_CHOICES, default=0)
content = models.CharField('内容', max_length=256, null=False)
group_id = models.CharField(
'group_id', max_length=256, null=True, blank=True)
news_id = models.CharField(
'news_id', max_length=256, null=True, blank=True)
app = models.CharField('app', max_length=256, null=True, blank=True)
model = models.CharField('model', max_length=256, null=True, blank=True)
field = models.CharField('field', max_length=256, null=True, blank=True)
record_id = models.CharField(
'record_id', max_length=256, null=True, blank=True)
record_pass_value = models.CharField(
'record_pass_value', max_length=256, null=True, blank=True)
record_reject_value = models.CharField(
'record_reject_value', max_length=256, null=True, blank=True)
is_read = models.BooleanField('是否阅读', default=False)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["-added"]
def is_in_progress(self):
now = datetime.datetime.now()
return now <= self.timeouted
@classmethod
def create_normal_notice(cls, user_id, content, group_id):
return Notice.objects.create(user_id=user_id, content=content, group_id=group_id)
@classmethod
def create_news_notice(cls, user_id, content, news_id):
return Notice.objects.create(type=2, user_id=user_id, content=content, news_id=news_id)
@classmethod
def create_reply_notice(cls, user_id, content, app, model, field, record_id, record_pass_value,
record_reject_value):
return Notice.objects.create(type=1, user_id=user_id, content=content, app=app, model=model, field=field,
record_id=record_id, record_pass_value=record_pass_value,
record_reject_value=record_reject_value)
def __str__(self):
return self.content + ':' + self.user.username
class Task(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
groups = models.ManyToManyField(Group)
content = models.TextField('内容', null=True, blank=True)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def add_groups(self, groups):
# if not len(groups):
# groups = Group.objects.filter(status='开启')
for g in groups:
self.groups.add(g)
def total(self):
return sum(g.total_user() for g in self.groups.all())
def record(self):
return TaskRecord.objects.filter(task=self).count()
def records(self):
group_ids = [group.id for group in self.groups.all()]
return Group_user.objects.filter(group__in=group_ids)
def add_task_message(self, user_id):
for group in self.groups.all():
Message.objects.create(
type=4, send_from_id=user_id, send_to_id=group.id, task=self, content=self.content)
def add_url_message(self, user_id, title, description, url, image):
for group in self.groups.all():
Message.objects.create(
type=1, send_from_id=user_id, send_to_id=group.id, task=self, page_title=title,
page_description=description, page_image=image, url=url)
class Meta:
ordering = ["-added"]
TASK_ADDITION_TYPE_CHOICES = (
(0, 'url'),
(1, 'file'),
(2, 'picture')
)
class TaskAddition(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
category = models.IntegerField(
'category', choices=TASK_ADDITION_TYPE_CHOICES, default=1)
url = models.CharField('url', max_length=512, null=True, blank=True)
file = models.FileField(
upload_to='task/file/%Y/%m/%d/', null=True, blank=True)
picture = models.ImageField(
upload_to='task/image/%Y/%m/%d/', null=True, blank=True)
TASK_RECORD_STATUS_CHOICES = (
(0, '未完成'),
(1, '完成'),
)
class TaskRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
user = models.ForeignKey(User, on_delete=models.CASCADE)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
status = models.IntegerField(
'状态', choices=TASK_RECORD_STATUS_CHOICES, default=1)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
MESSAGE_TYPE_CHOICES = (
(0, 'text'),
(1, 'url'),
(2, 'file'),
(3, 'image'),
(4, 'task')
)
class MessageIndicator(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
processing = models.BooleanField(default=True)
class Message(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
send_from = models.ForeignKey(User, on_delete=models.CASCADE)
send_to = models.ForeignKey(Group, on_delete=models.CASCADE)
task = models.ForeignKey(
Task, on_delete=models.CASCADE, null=True, blank=True)
type = models.IntegerField(
'Type', choices=MESSAGE_TYPE_CHOICES, default=1)
# text
content = models.TextField('内容', null=True, blank=True)
# url
page_title = models.CharField(
'page_title', max_length=512, null=True, blank=True)
page_description = models.CharField(
'page_description', max_length=1024, null=True, blank=True)
page_image = models.CharField(
'page_image', max_length=512, null=True, blank=True)
url = models.CharField('url', max_length=512, null=True, blank=True)
# file
file_title = models.CharField(
'file_title', max_length=256, null=True, blank=True)
file = models.CharField('file', max_length=256, null=True, blank=True)
# image
image_title = models.CharField(
'image_title', max_length=256, null=True, blank=True)
picture = models.CharField(
'picture', max_length=256, null=True, blank=True)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["added"]
class GroupRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
user = models.ForeignKey(User, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE)
enter_at = models.DateTimeField(auto_now=True)
leave_at = models.DateTimeField(auto_now=True)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class AppVersion(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
version = models.IntegerField('version', default=1)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["-version"]
class SMSNotifyRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
phone = models.CharField('phone', max_length=32)
added = models.DateField(auto_now_add=True)
notice_id = models.CharField('notice_id', max_length=64)
updated = models.DateTimeField(auto_now=True)
class VoiceNotifyRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
phone = models.CharField('phone', max_length=32)
added = models.DateField(auto_now_add=True)
notice_id = models.CharField('notice_id', max_length=64)
updated = models.DateTimeField(auto_now=True)
UNQUALIFIED_MEDIA_TYPE_CHOICES = (
(0, 'weixin'),
(1, 'weibo'),
(2, 'toutiao'),
(3, 'douyin'),
(4, 'qita')
)
class UnqualifiedMedia(models.Model):
'''
type:
weixin = 0
weibo = 1
douyin = 2
toutiao = 3
qita = 4
'''
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
media_id = models.UUIDField('media_id', null=True, blank=True)
name = models.CharField('name', max_length=256, null=True, blank=True)
unit = models.CharField('unit', max_length=256, null=True, blank=True)
type = models.IntegerField(
'Type', choices=UNQUALIFIED_MEDIA_TYPE_CHOICES, default=0)
province = models.CharField('', max_length=256, null=True, blank=True)
cities = models.CharField('', max_length=256, null=True, blank=True)
district = models.CharField('', max_length=256, null=True, blank=True)
result = models.TextField('result', null=True, blank=True)
cou = models.IntegerField('count of update', default=0)
dos = models.IntegerField('days of silent', default=0)
sdos = models.DateField('start date of silent', null=True, blank=True)
edos = models.DateField('end date of silent', null=True, blank=True)
version = models.IntegerField('version', default=0)
added = models.DateField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)