Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
refactory: 完善重构
Browse files Browse the repository at this point in the history
  • Loading branch information
ghosx committed Nov 30, 2019
1 parent cf4c76e commit d78cd29
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 61 deletions.
76 changes: 76 additions & 0 deletions SignIn/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Generated by Django 2.0.7 on 2019-11-30 01:58

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Sign',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, verbose_name='贴吧名')),
('fid', models.CharField(max_length=20, verbose_name='贴吧id')),
('is_sign', models.BooleanField(default=False, verbose_name='是否签到')),
('ststus', models.CharField(default='', max_length=100, verbose_name='签到状态')),
('retry_times', models.SmallIntegerField(default=0, verbose_name='重试次数')),
],
options={
'verbose_name': '签到',
'verbose_name_plural': '签到',
'db_table': 'sign',
},
),
migrations.CreateModel(
name='SignLog',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, verbose_name='贴吧名')),
('created_time', models.DateTimeField(auto_now_add=True, verbose_name='提交时间')),
('ret_log', models.TextField(verbose_name='签到日志')),
],
options={
'verbose_name': '签到日志',
'verbose_name_plural': '签到日志',
'db_table': 'sign_log',
},
),
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bduss', models.CharField(max_length=192, verbose_name='BDUSS')),
('username', models.CharField(editable=False, max_length=30, unique=True, verbose_name='贴吧用户名')),
('token', models.CharField(editable=False, max_length=200, unique=True, verbose_name='个人TOKEN')),
('created_time', models.DateTimeField(auto_now_add=True, verbose_name='提交时间')),
('flag', models.IntegerField(default=0, null=True, verbose_name='新用户')),
],
options={
'verbose_name': '用户',
'verbose_name_plural': '用户',
'db_table': 'user',
'ordering': ['created_time'],
'get_latest_by': 'created_time',
},
),
migrations.AddField(
model_name='signlog',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='SignIn.User', verbose_name='所属用户'),
),
migrations.AddField(
model_name='sign',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='SignIn.User', verbose_name='所属用户'),
),
migrations.AlterUniqueTogether(
name='sign',
unique_together={('name', 'fid', 'user')},
),
]
17 changes: 17 additions & 0 deletions SignIn/migrations/0002_auto_20191130_1027.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.0.7 on 2019-11-30 02:27

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
('SignIn', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='sign',
old_name='ststus',
new_name='status',
),
]
72 changes: 55 additions & 17 deletions SignIn/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,59 @@
from django.db import models

from SignIn.utils import utils
from constants import MAX_RETRY_TIMES, NOT_VALID_USER, ALREADY_UPDATE_USER, NEW_USER


class UserManager(models.Manager):

def new(self, bduss):
name = utils.get_name(bduss)
print(name)
token = str(uuid.uuid1())
obj, created = User.objects.update_or_create(username=name, defaults={"bduss": bduss, "token": token})
obj, created = User.objects.update_or_create(username=name,
defaults={"bduss": bduss, "token": token, "flag": NEW_USER})
return created

def need_update_like(self):
return User.objects.filter(flag=0)
@staticmethod
def need_update_like():
"""
返回需要更新关注贴吧的用户
:return:
"""
return User.objects.filter(flag=NEW_USER)

@staticmethod
def re_update_like():
"""
修改状态位,重新更新关注的贴吧
:return:
"""
print(time.time(), "重置所有用户的贴吧关注状态")
User.objects.filter(flag=ALREADY_UPDATE_USER).update(flag=NEW_USER).save()

def set_status_liking(self):
User.objects.filter(flag=0).update(flag=1)
@staticmethod
def set_status_liking():
"""
修改状态位,不需要更新关注的贴吧
:return:
"""
User.objects.filter(flag=NEW_USER).update(flag=ALREADY_UPDATE_USER)

@staticmethod
def check_all_user_valid():
users = User.objects.all()
for user in users:
if not user.valid_user():
print(time.time(), user.username, '失效')
user.flag = NOT_VALID_USER
user.save()


class User(models.Model):
bduss = models.CharField(max_length=192, verbose_name="BDUSS")
username = models.CharField(max_length=30, unique=True, editable=False, verbose_name="贴吧用户名")
token = models.CharField(max_length=200, unique=True, editable=False, verbose_name="个人TOKEN")
created_time = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="提交时间")
flag = models.IntegerField(null=True, default=0, verbose_name="新用户") # 默认0 已update1
flag = models.IntegerField(null=True, default=0, verbose_name="新用户") # 默认0 已update1 bduss失效2
objects = UserManager()

def __str__(self):
Expand All @@ -45,6 +74,9 @@ def like_callback(self, res):
Sign.objects.get_or_create(fid=i["id"], name=i["name"], user=self,
defaults={"fid": i["id"], "name": i["name"], "user": self})

def valid_user(self):
return utils.check_bduss(self.bduss)

class Meta:
get_latest_by = "created_time"
db_table = 'user'
Expand All @@ -55,10 +87,16 @@ class Meta:

class SignManager(models.Manager):

def need_sign(self):
obj = Sign.objects.filter(is_sign=False)
@staticmethod
def need_sign():
obj = Sign.objects.filter(is_sign=False, user__flag__exact=NOT_VALID_USER)
return obj

@staticmethod
def reset_sign_status():
print(time.time(), "重置所有贴吧的签到状态")
Sign.objects.filter(is_sign=True).update(is_sign=False, retry_times=0, status="").save()

def set_status_signing(self):
Sign.objects.filter(is_sign=False).update(is_sign=True)

Expand All @@ -67,7 +105,7 @@ class Sign(models.Model):
name = models.CharField(max_length=100, verbose_name="贴吧名")
fid = models.CharField(max_length=20, verbose_name="贴吧id")
is_sign = models.BooleanField(default=False, verbose_name="是否签到")
ststus = models.CharField(max_length=100, verbose_name="签到状态", default="")
status = models.CharField(max_length=100, verbose_name="签到状态", default="")
retry_times = models.SmallIntegerField(verbose_name="重试次数", default=0)
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="所属用户")
objects = SignManager()
Expand All @@ -86,15 +124,15 @@ def sign_callback(self, res):
sign = result["sign"]
# 日志记录
SignLog.objects.log(sign, res)
# 签到状态判断
if res['error_code'] != 0:
# 如果尝试签到3次还未成功,则不再尝试
if self.retry_times >= MAX_RETRY_TIMES or res['error_code'] == '0':
self.is_sign = True
self.status = "签到成功"
else:
# 签到状态判断
self.is_sign = False
self.retry_times += 1
# 如果尝试签到5次还未成功,则不再尝试
# todo: 修改最大尝试次数
if self.retry_times == 2:
self.is_sign = True
self.ststus = res['error_msg']
self.status = res['error_msg']
self.save()

class Meta:
Expand Down
1 change: 0 additions & 1 deletion SignIn/static/css/app.0c34b03a.css

This file was deleted.

1 change: 1 addition & 0 deletions SignIn/static/css/app.91f63cb1.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions SignIn/static/js/app.323a3a57.js

This file was deleted.

Loading

0 comments on commit d78cd29

Please sign in to comment.