Example #1
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.user.data).first()
        if user:
            if try_login(user, str(form.password.data)):
                flash('Logged in successfully!', 'success')
                log.info("User %s logged in", user)
                login_user(user, remember=form.remember_me.data)
                return redirect(request.args.get("next") or url_for("index"))
            else:
                flash('Log in Error', 'danger')
                log.warn("Log in attempt to '%s' from IP %s",
                         user,
                         request.remote_addr)
                form.errors['password'] = [u"Incorrect Password"]
        else:
            log.warn("Log in attempt to '%s' from IP %s",
                     form.user.data,
                     request.remote_addr)
            flash('User not found!', 'danger')
            form.errors['user'] = [u"User not found"]
    return render_template('login.html',
                           title="Log In",
                           form=form)
Example #2
0
def mababa_spider(request):
    url_dict = {}
    for i in range(5):
        url = 'https://blog.lupf.cn/?p=' + str(i + 1)
        log.info(f'-------------------->> 抓取第{i + 1}页')
        url_sigle_dict = get_mababa_article_page(url)
        url_dict = dict(url_dict, **url_sigle_dict)
    return JsonResponse(url_dict)
Example #3
0
def article_spider(request):
    url_dict = {}
    for i in range(30):
        url = 'https://cloud.tencent.com/developer/column/5263/page-' + str(i +
                                                                            1)
        log.info(f'-------------------->> 抓取第{i + 1}页')
        url_sigle_dict = get_article_page(url)
        url_dict = dict(url_dict, **url_sigle_dict)
    return JsonResponse(url_dict)
Example #4
0
def new_post():
    """
    Create a new post.

    If :GET: present the form to submit a new post.
    If :POST:, if valid, submit the post and redirect to home.
    """
    form = PostBlogForm()
    if form.validate_on_submit():
        log.info("User %s submitted post %s",
                 current_user,
                 form.title.data)
        log.debug("CodeType: %s", form.code_type.data)
        if form.code_type.data is None:
            code = False
        else:
            code = True
        post = Post(title=form.title.data,
                    slug=form.slug.data,
                    content=form.text.data,
                    user=current_user,
                    code=code,
                    code_type=form.code_type.data,)
        db.session.add(post)
        try:
            db.session.commit()
        except IntegrityError as exc:
            if exc.message.endswith("not unique"):
                log.error("Post '%s' was not unique in field '%s'",
                          form.title.data,
                          exc.message
                          .split(" ", 2)[-1].rsplit(" ", 3)[0])
            else:
                log.critical("Unknown Integrity Error with post '%s'",
                             form.title.data,
                             exc_info=True)
            db.session.rollback()
            flash("Integrity Error!", 'danger')
            return render_template("post/make_post.html",
                                   title="Post Blog",
                                   form=form)
        except:
            log.critical("Exception on %s [%s]",
                         'page',
                         'method',
                         exc_info=True)
            db.session.rollback()
            abort(500)
        flash("{} submitted {}.".format(
            current_user.username,
            form.title.data), 'success')
        return redirect(url_for('index'))
    return render_template("post/make_post.html",
                           title="Post Blog",
                           form=form)
Example #5
0
def new_post():
    """
    Create a new post.

    If :GET: present the form to submit a new post.
    If :POST:, if valid, submit the post and redirect to home.
    """
    form = PostBlogForm()
    if form.validate_on_submit():
        log.info("User %s submitted post %s", current_user, form.title.data)
        log.debug("CodeType: %s", form.code_type.data)
        if form.code_type.data is None:
            code = False
        else:
            code = True
        post = Post(
            title=form.title.data,
            slug=form.slug.data,
            content=form.text.data,
            user=current_user,
            code=code,
            code_type=form.code_type.data,
        )
        db.session.add(post)
        try:
            db.session.commit()
        except IntegrityError as exc:
            if exc.message.endswith("not unique"):
                log.error("Post '%s' was not unique in field '%s'",
                          form.title.data,
                          exc.message.split(" ", 2)[-1].rsplit(" ", 3)[0])
            else:
                log.critical("Unknown Integrity Error with post '%s'",
                             form.title.data,
                             exc_info=True)
            db.session.rollback()
            flash("Integrity Error!", 'danger')
            return render_template("post/make_post.html",
                                   title="Post Blog",
                                   form=form)
        except:
            log.critical("Exception on %s [%s]",
                         'page',
                         'method',
                         exc_info=True)
            db.session.rollback()
            abort(500)
        flash(
            "{} submitted {}.".format(current_user.username, form.title.data),
            'success')
        return redirect(url_for('index'))
    return render_template("post/make_post.html", title="Post Blog", form=form)
Example #6
0
def get_article_page(url):
    r = requests.request('get', url=url)
    html = r.text
    soup = BeautifulSoup(html, 'lxml')
    ls = soup('h3', class_="com-article-panel-title")
    url_dict = {}
    for tag in ls:
        s = 'https://cloud.tencent.com' + tag.a['href']
        title = tag.a.string
        obj = spider_article.objects.filter(title=title).first()  # 查询文章是否已存在
        if obj == None:
            article = spider_article(title=title, linkage=s, tag="python")
            article.save()
            log.info(f'保存文章 --> {title}')
        else:
            log.info(f'已存在文章 --> {title}')
        url_dict[title] = s  # 返回文章的标题和链接
    return url_dict
Example #7
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.user.data).first()
        if user:
            if try_login(user, str(form.password.data)):
                flash('Logged in successfully!', 'success')
                log.info("User %s logged in", user)
                login_user(user, remember=form.remember_me.data)
                return redirect(request.args.get("next") or url_for("index"))
            else:
                flash('Log in Error', 'danger')
                log.warn("Log in attempt to '%s' from IP %s", user,
                         request.remote_addr)
                form.errors['password'] = [u"Incorrect Password"]
        else:
            log.warn("Log in attempt to '%s' from IP %s", form.user.data,
                     request.remote_addr)
            flash('User not found!', 'danger')
            form.errors['user'] = [u"User not found"]
    return render_template('login.html', title="Log In", form=form)
Example #8
0
def update_vistor():
    vistor_list = Vistor.objects.filter(count=3, country='')
    for i in vistor_list:
        if re.match(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
                    i.ip):
            url = f"http://www.ip-api.com/json/{i.ip}?lang=zh-CN"
            res = requests.get(url, timeout=5)
            ip_message = res.json()
            log.info(f'解析IP响应结果 --> {ip_message}')
            if ip_message.get('status') == 'success':
                i.country = ip_message.get('country')
                i.city = ip_message.get('city')
                i.ip_as = ip_message.get('as')
                i.isp = ip_message.get('isp')
                i.save()
            elif ip_message.get('status') == 'fail':
                log.error(f"IP解析失败 --> {i.ip}")
            else:
                log.error(f'解析异常 --> {i.ip}')
        else:
            log.error(f'无法解析异常IP --> {i.ip}')
Example #9
0
def login(request):
    # 不允许重复登录
    res = request.session.get('is_login')
    log.debug(f'request session  --> {res}')
    if request.session.get('is_login', None):
        return redirect('/')

    if request.method == "POST":
        login_form = UserForm(request.POST)
        message = "请检查填写的内容"
        if login_form.is_valid():
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']
            try:
                user = User.objects.get(user_name=username)
                if user.has_confirmed == False:
                    message = "用户还未邮件确认"
                    return render(request, 'login.html', locals())
                if user.password == md5_code(password):
                    # 往session字典内写入用户状态和数据
                    request.session['is_login'] = True
                    request.session['user_id'] = user.id
                    request.session['user_name'] = user.user_name
                    log.info(request.session['user_name'])
                    return redirect('/')
                else:
                    message = "密码不正确!"
            except:
                message = "用户不存在!"
        return render(request, 'login.html', locals())
    '''
    这里使用了一个小技巧,Python内置了一个locals()函数,它返回当前所有的本地变量字典,
    我们可以偷懒的将这作为render函数的数据字典参数值,就不用费劲去构造一个形如
    {'message':message, 'login_form':login_form}的字典了。
    这样做的好处当然是大大方便了我们,但是同时也可能往模板传入了一些多余的变量数据,造成数据冗余降低效率
    '''
    login_form = UserForm()
    return render(request, 'login.html', locals())
Example #10
0
def get_mababa_article_page(url):
    r = requests.get(url=url)
    html = r.text
    print(html)
    soup = BeautifulSoup(html, 'lxml')
    ls = soup('article', class_="post")
    url_dict = {}
    for tag in ls:
        title = tag.header.h2.a.string.strip()
        s = tag.header.h2.a["href"]
        obj = spider_article.objects.filter(title=title).first()  # 查询文章是否已存在
        if obj == None:
            article = spider_article(title=title, linkage=s, tag="java")
            try:
                article.save()
                log.info(f'保存文章 --> {title}')
            except:
                log.info(f'存表异常 --> {title}')
        else:
            log.info(f'已存在文章 --> {title}')
        url_dict[title] = s
    return url_dict