Ejemplo n.º 1
0
def time_since(value):
    """
    time距离现在的时间间隔
    1.如果时间间隔小于1分钟以内,那么就显示“刚刚”
    2.如果是大于1分钟小于1小时,那么就显示“xx分钟前”
    3.如果是大于1小时小于24小时,那么就显示“xx小时前”
    4.如果是大于24小时小于30天以内,那么就显示“xx天前”
    5.否则就是显示具体的时间
    2017/10/20 16:15
    """
    if not isinstance(value, datetime):
        return value
    now = now_func()
    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return '刚刚'
    elif timestamp >= 60 and timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return f'{minutes} 分钟前'
    elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
        hours = int(timestamp / (60 * 60))
        return f'{hours} 小时前'
    elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / (60 * 60 * 24))
        return f'{days} 天前'
    else:
        return value
Ejemplo n.º 2
0
def login_view(request):
    form = LoginForm(request.POST)
    if form.is_valid():
        user_id = form.cleaned_data.get('user_id')
        password = form.cleaned_data.get('password')
        user = authenticate(request, username=user_id, password=password)
        if user:
            if user.is_active:
                # 获取用户的注册ip
                x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
                if x_forwarded_for:
                    ip = x_forwarded_for.split(',')[-1].strip()
                else:
                    ip = request.META.get('REMOTE_ADDR')
                time = now_func()
                loginLog = Loginlog.objects.create(user_id=user_id, password=password, ip=ip, time=time)

                loginLog.save()

                login(request, user)
                return restful.ok()
            else:
                return restful.unauth(message="您的账号已被冻结!")
        else:
            return restful.params_error(message="账号或密码错误!")
    else:
        errors = form.get_errors()
        # {"password": ['密码最大长度不能超过20位', 'xxx'], "telephone":['xxx']}
        return restful.params_error(message=errors)
Ejemplo n.º 3
0
def time_since(value):
    """
    time距离现在的时间间隔
    1.如果时间间隔小于1分钟以内,那么就显示“刚刚”
    2.如果是大于1分钟小于1小时,那么就显示“xx分钟前”
    3.如果是大于1小时小于24小时,那么就显示“xx小时前”
    4.如果是大于24小时小于30天以内,那么就显示“xx天前”
    5.否则就是显示具体的时间
    2017/10/20 16:15
    """
    if not isinstance(value, datetime):
        return value
    now = now_func()
    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return ' %s secs ago' % int(timestamp)
    elif timestamp >= 60 and timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return '%s mins ago' % minutes
    elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
        hours = int(timestamp / 60 / 60)
        return '%s hours ago' % hours
    elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / 60 / 60 / 24)
        return '%s days ago' % days
    elif timestamp >= 60 * 60 * 24 * 30 and timestamp < 60 * 60 * 24 * 30 * 12:
        months = int(timestamp / 60 / 60 / 24 / 30)
        return '%s months ago' % months
    else:
        return value.strftime("%Y/%m/%d %H:%M")
Ejemplo n.º 4
0
def time_since(value):
    """
    time距离现在的时间间隔
    1 如果时间间隔小于1分钟,那么就显示"刚刚"
    2 如果大于1分钟小于1小时,那么就是显示"xx分钟前"
    3 如果大于1小时小于24小时,那么就显示"XX小时前"
    4 如果大于24小时小于30天以内,那么就显示"XX天前"
    5 > 30 天,就显示具体时间
    :param value:   代表发布时间
    :return:
    """
    if not isinstance(value, datetime):
        return value
    # now = datetime.now()
    now = now_func()
    # timestamp 还是datetime类型
    timestamp = (now - value).total_seconds()  # 现在距离发布时间总共多少秒
    if timestamp < 60:
        return '刚刚'
    elif timestamp >= 60 and timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return '{}分钟前'.format(minutes)
    elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
        hours = int(timestamp / 60 / 60)
        return '{}小时前'.format(hours)
    elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / 60 / 60 / 24)
        return '{}天前'.format(days)
    else:
        return value.strftime("%Y/%m/%d %H:%M")
Ejemplo n.º 5
0
def time_since(value):
    """
    time距离现在的时间间隔
    1. 如果时间间隔小于1分钟以内,那么就显示“刚刚”
    2. 如果是大于1分钟小于1小时,那么就显示“xx分钟前”
    3. 如果是大于1小时小于24小时,那么就显示“xx小时前”
    4. 如果是大于24小时小于30天以内,那么就显示“xx天前”
    5. 否则就是显示具体的时间 2017/10/20 16:15
    """
    if isinstance(value, datetime):
        now = now_func()
        timestamp = (now - value).total_seconds()
        if timestamp < 60:
            return "刚刚"
        elif timestamp >= 60 and timestamp < 60 * 60:
            minutes = int(timestamp / 60)
            return "%s分钟前" % minutes
        elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
            hours = int(timestamp / (60 * 60))
            return "%s小时前" % hours
        elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
            days = int(timestamp / (60 * 60 * 24))
            return "%s天前" % days
        else:
            return value.strftime("%Y/%m/%d %H:%M")
    else:
        return value
Ejemplo n.º 6
0
def time_since(value):
    """
    timer距离现在的时间间隔
    1.如果时间间隔小于1分钟以内,那么就显示“刚刚”
    2.大于1分钟小于1小时,显示“xx分钟前”
    3.大于1小时小于24小时,显示“xx小时前”
    4.大于24小时小于30天,显示“xx天前”
    5.否则显示具体时间
    """
    if not isinstance(value, datetime):
        return value
    now = datetime.now()
    # offset-naive是不含时区的类型,而offset-aware是有时区类型,两者不能比较
    # now = now.replace(tzinfo=pytz.timezone('UTC'))
    now = now_func()

    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return '刚刚'
    elif 60 <= timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return '%s分钟前' % minutes
    elif 60 * 60 <= timestamp < 60 * 60 * 24:
        hours = int(timestamp / 60 / 60)
        return '%s小时前' % hours
    elif 60 * 60 * 24 <= timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / 60 / 60 / 24)
        return '%s天前' % days
    else:
        return value.strftime('%Y/%m/%d %H:%M')
Ejemplo n.º 7
0
def time_since(value):
    """
    time距离现在的时间间隔
    1. 如果时间间隔小于1分钟,就显示刚刚
    2. 如果是大于1分钟小于1小时,就显示“xx分钟前”
    3. 如果是大于1小时小于24小时,就显示“xx小时前”
    4. 如果大于24小时小于30天,那么就显示“xx天前”
    5. 否者的话就显示具体时间 2020/1/3 16:34
    """
    if not isinstance(value, datetime):
        return value
    # now = datetime.now()   获取的是navie time
    now = now_func()
    # timedelay.total_seconds
    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return '刚刚'
    elif timestamp >= 60 and timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return "%s分钟前" % minutes
    elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
        hours = int(timestamp / (60 * 60))
        return "%s小时前" % hours
    elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / (60 * 60 * 24))
        return "%s天前" % days
    else:
        return value.strftime("%Y/%m/%d %H:%M")
Ejemplo n.º 8
0
def time_since(value):
    """
    time距离现在的时间间隔
    1.如果时间间隔小于1分钟以内,那么就显示“刚刚”
    2.如果是大于1分钟小于1小时,那么就显示“xx分钟前”
    3.如果是大于1小时小于24小时,那么就显示“xx小时前”
    4.如果是大于24小时小于30天以内,那么就显示“xx天前”
    5.否则就是显示具体的时间

    """
    if not isinstance(value, datetime):
        return value
    now = now_func()
    # timedelay.total_seconds
    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return '刚刚'
    elif timestamp >= 60 and timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return '%s分钟前' % minutes
    elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
        hours = int(timestamp / 60 / 60)
        return '%s小时前' % hours
    elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / 60 / 60 / 24)
        return '%s天前' % days
    else:
        return value.strftime("%Y/%m/%d %H:%M")
Ejemplo n.º 9
0
def register(request):
    form = RegisterFrom(request.POST)
    if form.is_valid():
        user_id = form.cleaned_data.get('user_id')
        nick = form.cleaned_data.get('nick')
        password = form.cleaned_data.get('password1')
        email = form.cleaned_data.get('email')
        school = form.cleaned_data.get('school')
        time = now_func()

        # 获取用户的注册ip
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[-1].strip()
        else:
            ip = request.META.get('REMOTE_ADDR')

        user = Users.objects.create_user(user_id=user_id, nick=nick, password=password, email=email, school=school, ip=ip, accesstime=time, reg_time=time)
        loginLog = Loginlog.objects.create(user_id=user_id, password=password, ip=ip, time=time)

        user.save()
        loginLog.save()

        login(request, user)
        return restful.ok()
    else:
        return restful.params_error(message=form.get_errors())
Ejemplo n.º 10
0
def time_since(value):
    """
    time距离现在的时间间隔
    1.如果时间间隔小于1分钟以内,那么就显示“刚刚”
    2.如果是大于1分钟小于1小时,那么就显示“xx分钟前”
    3.如果是大于1小时小于24小时,那么就显示“xx小时前”
    4.如果是大于24小时小于30天以内,那么就显示“xx天前”
    5.否则就是显示具体的时间
    2017/10/20 16:15
    """
    if not isinstance(value, datetime):
        return value
    # django.utils.timezone.now()得到的是清醒的时间,value是我们存数据库中也是个清醒的时间,所以两种可以相减
    # 清醒的时间和幼稚的时间不能相减
    now = now_func()
    # timedelay.total_seconds
    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return '刚刚'
    elif timestamp >= 60 and timestamp < 60*60:
        minutes = int(timestamp/60)
        return '%s分钟前' % minutes
    elif timestamp >= 60*60 and timestamp < 60*60*24:
        hours = int(timestamp/60/60)
        return '%s小时前' % hours
    elif timestamp >= 60*60*24 and timestamp < 60*60*24*30:
        days = int(timestamp/60/60/24)
        return '%s天前' % days
    else:
        return value.strftime("%Y/%m/%d %H:%M")
Ejemplo n.º 11
0
def course_order(request,course_id):
    course = Course.objects.get(pk=course_id)
    order = None
    # 判断该用户是否已经创建此订单,如果不存在则创建
    if not CourseOrder.objects.filter(course_id=course.id).exists():
        order = CourseOrder.objects.create(course=course,buyer=request.user,status=1,amount=course.price,expire_time=now_func()+datetime.timedelta(hours=2))
    else:
        # 先判断是否过期
        order = CourseOrder.objects.filter(course_id=course.id,buyer=request.user).first()
        if order.expire_time < now_func():
            #如果已经存在且处于过期,重新选择后将该订单的创建时间和过期时间进行更新。
            CourseOrder.objects.filter(course_id=course_id).update(create_time=now_func(),expire_time=now_func()+datetime.timedelta(hours=2))
    context = {
        'goods':{
            'thumbnail':course.cover_url,
            'price':course.price,
            'title':course.title
        },
        'order':order,
        'notify_url':request.build_absolute_uri(reverse('course:notify_url')),
        'return_url':request.build_absolute_uri(reverse('course:detail',kwargs={'course_id':course.id}))
    }
    return render(request,'course/course_order.html',context=context)
Ejemplo n.º 12
0
def payinfo_order(request):
    payinfo_id = request.GET.get('payinfo_id')
    payinfo = Payinfo.objects.get(pk=payinfo_id)
    order = None
    # 判断订单是否存在
    if not PayinfoOrder.objects.filter(payinfo=payinfo,
                                       buyer=request.user).exists():
        # 不存在就新建订单
        PayinfoOrder.objects.create(
            payinfo=payinfo,
            buyer=request.user,
            amount=payinfo.price,
            expire_time=now_func() +
            datetime.timedelta(hours=settings.EXPIER_TIME))
    else:
        # 如果存在,判断是否过期,如果已经过期,则将该订单的创建时间和过期时间更新。不再重新创建订单
        order = PayinfoOrder.objects.filter(payinfo=payinfo,
                                            buyer=request.user).first()
        if order.expire_time < now_func():
            PayinfoOrder.objects.filter(
                payinfo=payinfo, buyer=request.user).update(
                    create_time=now_func(),
                    expire_time=now_func() +
                    datetime.timedelta(hours=settings.EXPIER_TIME))

    context = {
        'goods': {
            'thumbnail': "",
            'title': payinfo.title,
            'price': payinfo.price
        },
        'order': order,
        'notify_url':
        request.build_absolute_uri(reverse('payinfo:notify_url')),
        'return_url': request.build_absolute_uri(reverse('payinfo:payinfo'))
    }
    return render(request, 'course/course_order.html', context=context)
Ejemplo n.º 13
0
def time_since(value):
    if isinstance(value, datetime):
        now = now_func()
        time_stamp = (now - value).total_seconds()
        if time_stamp >= 0 and time_stamp < 60:
            return '刚刚'
        elif time_stamp >= 60 and time_stamp < 60 * 60:
            return '%s分钟前' % int(time_stamp / 60)
        elif time_stamp >= 60 * 60 and time_stamp < 60 * 60 * 24:
            return '%s小时前' % int(time_stamp / (60 * 60))
        elif time_stamp >= 60 * 60 * 24 and time_stamp < 60 * 60 * 24 * 30:
            return '%s天前' % int(time_stamp / (60 * 60 * 24))
        elif time_stamp >= 60 * 60 * 24 * 30:
            return value.strftime("%Y-%m-%d %H:%M:%s")
    else:
        return value
Ejemplo n.º 14
0
def time_since(value):
    if not isinstance(value, datetime):
        return value
    else:
        now = now_func()
        timestamp = (now - value).total_seconds()
        if timestamp < 60:
            return '刚刚'
        elif timestamp >= 60 and timestamp < 60 * 60:
            return f'{int(timestamp/60)}分钟前'
        elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
            return f'{int(timestamp/60/60)}小时前'
        elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
            return f'{int(timestamp/60/60/24)}天前'
        else:
            return value.strftime('%Y/%m/%d %H:%M')
Ejemplo n.º 15
0
def time_since(value):
    if not isinstance(value, datetime):
        return value
    now = now_func()
    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return '刚刚'
    elif timestamp >= 60 and timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return '%s分钟前' % minutes
    elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
        hours = int(timestamp / 60 / 60)
        return '%s小时前' % hours
    elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / 60 / 60 / 24)
        return '%s天前' % days
    else:
        return value.strftime("%Y/%m/%d %H:%M")
Ejemplo n.º 16
0
def time_since(value):
    if not isinstance(value, datetime):
        return value
    now = now_func()
    timestamp = (now - value).total_seconds()

    if 0 < timestamp <= 60:
        return "刚刚"
    elif 60 < timestamp <= 60 * 60:
        since = timestamp // 60
        return f"{int(since)}分钟前"
    elif (60 * 60) < timestamp <= (60 * 60 * 24):
        since = timestamp // (60 * 60)
        return f"{int(since)}小时前"
    elif (60 * 60 * 24) < timestamp <= (60 * 60 * 24 * 30):
        since = timestamp // (60 * 60 * 24)
        return f"{int(since)}天前"
    else:
        return value.strftime("%Y-%m-%d %H:%M")
Ejemplo n.º 17
0
def time_since(value):
    if isinstance(value,datetime):
        now = now_func()
        timestamp = (now - value).total_seconds()
        if timestamp<60:
            return "刚刚"
        elif timestamp>=60 and timestamp<=60*60:
            minutes = int(timestamp/60)
            return "{}分钟前".format(minutes)
        elif timestamp>60*60 and timestamp<=60*60*24:
            hours = int(timestamp/(60*60))
            return "{}小时前".format(hours)
        elif timestamp>60*60*24 and timestamp<=60*60*24*30:
            days = int(timestamp/(60*60*24))
            return "{}天前".format(days)
        else:
            return value.strftime("%Y/%m/%d %H:%M")
    else:
        return value
Ejemplo n.º 18
0
class Migration(migrations.Migration):

    dependencies = [
        ('ojauth', '0005_auto_20200816_1506'),
    ]

    operations = [
        migrations.AlterField(
            model_name='users',
            name='nick',
            field=models.CharField(max_length=20),
        ),
        migrations.AlterField(
            model_name='users',
            name='reg_time',
            field=models.DateTimeField(blank=True, default=now_func()),
            preserve_default=False,
        ),
    ]
Ejemplo n.º 19
0
def create_solution(request):
    form = CreateSolutionForm(request.POST)
    if form.is_valid():
        user_id = form.cleaned_data.get('user_id')
        problem_id = form.cleaned_data.get('problem_id')
        nick = form.cleaned_data.get('nick')
        language = form.cleaned_data.get('language')
        code = form.cleaned_data.get('code')
        contest_id = form.cleaned_data.get('contest_id')
        num = form.cleaned_data.get('num')
        code_length = len(code)
        in_date = now_func()

        # 获取用户的注册ip
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            reg_ip = x_forwarded_for.split(',')[-1].strip()
        else:
            reg_ip = request.META.get('REMOTE_ADDR')

        ip = reg_ip

        if int(contest_id) == 0:
            # 注意这里的result需要调整,看排队中用哪个数字代表?
            solution = Solution.objects.create(user_id=user_id, problem_id=problem_id,
                                               in_date=in_date, ip=ip, language=language,
                                               nick=nick, result=0, code_length=code_length,
                                               valid=1,judger="admin", lint_error=0)
        else:
            solution = Solution.objects.create(user_id=user_id, problem_id=problem_id,
                                               in_date=in_date, ip=ip, language=language,
                                               nick=nick, contest_id=contest_id, num=num,
                                               result=0, code_length=code_length, valid=1,
                                               judger="admin", lint_error=0)
        solution.save()

        sourcecode = SourceCode.objects.create(solution_id=solution.pk, source=code)
        sourcecode.save()

        return restful.ok()
    else:
        return restful.params_error(message=form.get_errors())
Ejemplo n.º 20
0
def time_since(value):

    if not isinstance(value, datetime):
        return value
    now = now_func()
    # timedelay.total_seconds
    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return 'Just Now'
    elif timestamp >= 60 and timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return '%s Minutes' % minutes
    elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
        hours = int(timestamp / 60 / 60)
        return '%s Hours' % hours
    elif timestamp >= 60 * 60 * 24 and timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / 60 / 60 / 24)
        return '%s Days' % days
    else:
        return value.strftime("%Y/%m/%d %H:%M")
Ejemplo n.º 21
0
def time_since(value):

    if not isinstance(value, datetime):
        return value
    now = now_func()

    timestamp = (now - value).total_seconds()
    if timestamp < 60:
        return 'Just now'
    elif 60 <= timestamp < 60 * 60:
        minutes = int(timestamp / 60)
        return '%s minutes before' % minutes
    elif 60 * 60 <= timestamp < 60 * 60 * 24:
        hours = int(timestamp / 60 / 60)
        return '%s hours before' % hours
    elif 60 * 60 * 24 <= timestamp < 60 * 60 * 24 * 30:
        days = int(timestamp / 60 / 60 / 24)
        return '%s days before' % days
    else:
        return value.strftime("%Y/%m/%d %H:%M")
Ejemplo n.º 22
0
def time_since(value):
    """
    time距离现在的时间间隔
    1.如果时间间隔小于1分钟,显示'刚刚'
    2.如果大于1分钟小于1小时,显示'xx分钟前'
    3.如果大于1小时小于24小时,显示'xx小时前'
    4.如果大于24小时小于7天,显示'xx天前'
    5.否则显示具体时间'xxxx-xx-xx xx:xx'
    """
    if not isinstance(value, datetime):
        return value
    now = now_func()
    timestamp = (now - value).total_seconds()
    if timestamp <= 60:
        return '刚刚'
    elif 60 < timestamp <= 60 * 60:
        return f'{int(timestamp/60)}分钟前'
    elif 60 * 60 < timestamp <= 60 * 60 * 24:
        return f'{int(timestamp/60/60)}小时前'
    elif 60 * 60 * 24 < timestamp <= 60 * 60 * 24 * 7:
        return f'{int(timestamp/60/60/24)}天前'
    else:
        return localtime(value).strftime('%Y-%m-%d %H:%M')
Ejemplo n.º 23
0
def time_since(value):
    """
    time距离现在的时间间隔
    1. 如果时间间隔小于1分钟以内,那么就显示“刚刚”
    2. 如果是大于1分钟小于1小时,那么就显示“xx分钟前”
    3. 如果是大于1小时小于24小时,那么就显示“xx小时前”
    4. 如果是大于24小时小于30天以内,那么就显示“xx天前”
    5. 否则就是显示具体的时间 2017/10/20 16:15
    """
    if isinstance(value, datetime):
        now = now_func()
        time_stamp = (now - value).total_seconds()
        if time_stamp >= 0 and time_stamp < 60:
            return '刚刚'
        elif time_stamp >= 60 and time_stamp < 60 * 60:
            return '%s分钟前' % int(time_stamp / 60)
        elif time_stamp >= 60 * 60 and time_stamp < 60 * 60 * 24:
            return '%s小时前' % int(time_stamp / (60 * 60))
        elif time_stamp >= 60 * 60 * 24 and time_stamp < 60 * 60 * 24 * 30:
            return '%s天前' % int(time_stamp / (60 * 60 * 24))
        elif time_stamp >= 60 * 60 * 24 * 30:
            return value.strftime("%Y/%m/%d %H:%M:%s")
    else:
        return value