ngingx配置文件更新代码,和展示

Python代码

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib import auth
from django.contrib.auth.decorators import login_required
import difflib
import os, sys
import requests 
import re
import urllib, urllib2
from django.core.paginator import Paginator
import commands
import MySQLdb
import time, datetime
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.template import RequestContext
from webip.models import AccessDizhi
from nginxet.models import NginxET, NginxEThistory
from django.shortcuts import get_object_or_404
from nginxet.form import HostsListForm
@csrf_exempt
def nginxfile(request):
    if request.method == 'POST':
        date1 = datetime.datetime.now()
        date2 = date1 + datetime.timedelta(hours=14)
        date = date2.strftime('%Y-%m-%d %H:%M:%S')
        all_server1 = NginxET.objects.all().values_list('dirname','filename')
        c = []
        for i in all_server1:
            c.append(i)
        for root,dirs,files in os.walk('/data/nginxet/'):
            dir_name = root.split('/')[3]
            if dir_name:
               for filed in files:
                   if filed:
                      filed3 = (u'%s' %dir_name, u'%s' %filed)
                      abc = c.count(filed3)
                      if abc > 0:
                          pass
                      else:
                          filedd = root+'/'+filed
                          filed1 = open(filedd, 'r')
                          messages = filed1.read()
                          NginxET.objects.create(dirname=dir_name,filename=filed,message=messages,onlinetime=date,updatemessage=1,updatetime=date)
                          filed1.close()
                   else:
                      pass  
        all_server = NginxET.objects.all()
        paginator = Paginator(all_server,20)
        try:
            page = int(request.GET.get('page','1'))
        except ValueError:
            page = 1
        try:
            all_server = paginator.page(page)
        except :
            all_server = paginator.page(paginator.num_pages)
        return render_to_response('nginxet.html',
                      {'all_host_list': all_server, 'page': page, 'paginator':paginator},context_instance=RequestContext(request))
    else:
        all_server = NginxET.objects.all()
        paginator = Paginator(all_server,20)
    
        try:
            page = int(request.GET.get('page','1'))
        except ValueError:
            page = 1
    
        try:
            all_server = paginator.page(page)
        except :
            all_server = paginator.page(paginator.num_pages)
        return render_to_response('nginxet.html',
                          {'all_host_list': all_server, 'page': page, 'paginator':paginator},context_instance=RequestContext(request))
models代码
# -*- coding: utf-8 -*-
from django.db import models
class NginxET(models.Model):
    dirname = models.CharField(max_length=50, verbose_name=u'目标地址')
    filename = models.CharField(max_length=100, verbose_name=u'文件名')
    message = models.TextField(verbose_name=u'nginx内容')
    updatemessage = models.TextField(verbose_name=u'更新内容')
    onlinetime = models.DateTimeField(max_length=100, blank=True, verbose_name=u'上线时间')
    updatetime = models.DateTimeField(max_length=100, blank=True, verbose_name=u'更新时间')
    def __unicode__(self):
        return u'%s - %s - %s - %s' %(self.dirname, self.filename, self.message, self.updatemessage, self.updatetime)
    class Meta:
        verbose_name = u'目标地址'
        verbose_name = u'文件名'
form代码
# -*- coding: utf-8 -*-
from django import forms
from nginxet.models import NginxET
class HostsListForm(forms.ModelForm):
#class HostsListForm(forms.Form):
    class Meta:
        model = NginxET
        fields = "__all__"
        widgets = {
          'dirname': forms.TextInput(attrs={'class': 'form-control'}),
          'filename': forms.TextInput(attrs={'class': 'form-control'}),
#          'message': forms.CharField(widget=forms.Textarea),
          'message': forms.TextInput(),
          'updatemessage': forms.TextInput(attrs={'class': 'form-control'}),
          'onlinetime': forms.TextInput(attrs={'class': 'form-control'}),
          'updatetime': forms.TextInput(attrs={'class': 'form-control'}),
        }

html代码

{% extends "index.html" %}
{% block title %}OMS{% endblock %}
{% block css %}


{% endblock %}
{% block content %}
      
      
            
                 
                      
                           {% if all_host_list %}
                           
                               
                                   
                                      ID
                                      IP地址
                                      文件名
                                      上线时间
                                      更新时间
                                   
                               

                               
                               {% for all_host in all_host_list %}
                                   
                                      {{ forloop.counter }}
                                      {{ all_host.dirname }}
                                      {{ all_host.filename }}
                                      {{ all_host.onlinetime }}
                                      {{ all_host.updatetime }}
                                      内容展示
                                          历史
                                          最近一次更改比较
                                          推送
                                   
                               {% endfor %}
                               
                            
                           {% endif %}