5.26 JLL--实习日志--Primary_Image+total_setting

debug for different Users

  1. primary_image 图片选取!!涉及到的函数是view_property 的change_file_image_flag和add_files的两个函数!
    首先在数据库层面的property里面的加一个列,并且设置默认的值是
    default_building_pic
image_path = models.CharField('图片路径',max_length=255, default="default_building_pic.jpg")

然后在add_files函数里面加上判定的步骤!,代码如下

def add_files(request, property_id):
    if request.method == 'POST': 
            form = UploadFileFormChoice(request.POST, request.FILES) # get post 
            if form.is_valid():
                file_type =request.POST['property_form_type_choice']#从form表单里面得到类型 
                now_time = datetime.datetime.utcnow()
                yesterdaytime = now_time + datetime.timedelta(days=-1)
                userid  = User.objects.filter(id=request.user.id).first().id
                #为什么这边要取第一个?
                property_instance=Property.objects.all().first()
                #request.user通过获取到对应的user_role_instance的对象
                user_role_instance = UserRole.objects.filter(user=request.user).first().userRole
                propertyfiles_count = PropertyFile.objects.filter(Q(Property_File_Timestamp__gt=yesterdaytime)&Q(Q(UploadBy=userid)|Q(DeleteBy=userid))).count()
                #用来判定是不是primary_image_instance是不是存在?判定的标准是photo类型的,并且没有过期的同时primary_image_flag是存在的PropertyFile类型的
                primary_image_instance=PropertyFile.objects.filter(Q(Property=property_instance) & Q(Property_File_Type = 'PHO') & Q(RecordEndDate="NULL") & Q(primary_image_flag="1"))
                #primary_image_instance primary image exist ?
                if(propertyfiles_count < 500):
                    if request.FILES.getlist('file'):
                        for file_item in request.FILES.getlist('file'):
                         #为什么这边是需要循环的?
                            property_instance = get_object_or_404(Property,pk=int(property_id))
                            file_name_original = file_item.name
                            file_name_extension = file_item.name.split('.')[-1].lower()
                            #file_name 拼接实现是为了存放在系统中固定的位置!
                            file_name = file_type + '_' + property_id + '_' + datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f") + '.' + file_name_extension
                            file_item.name = file_name
                            record_start_date=now_time
                            if primary_image_instance:
                            #如果存在那就把property_file 的image_flag设置为0,如果没有,那就还要判定传入进来的文件是不pho类型的如果是,那就把最新传进来的文件设置为primary_image!如果不是pho类型,那就仍然设置为0,感觉总是涉及到property类型的!
                                image_flag = '0'
                            elif file_type == 'PHO':
                                image_flag = '1'
                                #这边还需要加上first()??
                                property_instance.image_path = PropertyFile.objects.filter(Q(Property=property_instance) & Q(Property_File_Type = 'PHO') & Q(RecordEndDate="NULL") & Q(primary_image_flag="1")).first().Property_File_Path
                                property_instance.save()
                            else:
                                image_flag ='0'
                            #如果没有那就创建一个property_file 对象并且给里面赋值
                            property_file = PropertyFile(Property=property_instance, Property_File=file_item, Property_File_Path=file_name, Property_File_Name = file_name_original, Property_File_Type = file_type, Property_File_Subpart = file_name_extension, RecordStartDate=record_start_date, view_flag = user_role_instance, control_flag = user_role_instance, UploadBy = userid, primary_image_flag = image_flag)
                            #保存对象到数据库!
                            property_file.save()
                            views.write_operation_log(property_file, "Add", request)
                    return HttpResponseRedirect("/property/view_detail/%s?message=success&item=files" % property_id) 
                else:
                    return HttpResponseRedirect("/property/view_detail/%s?message=fail&item=files" % property_id)
            else:
                return HttpResponseRedirect("/property/view_detail/%s?message=fail&item=files" % property_id)
    else:
        return HttpResponseRedirect("/property/view_detail/%s?message=fail&item=files" % property_id)

在view_property 里面的两个函数需要添加default_image的步骤,给propety这个函数添加image_path的属性!
在add_files函数中,如果存在primary_image_instance对象的,那就会把新传入的对象的image_path属性设置为默认的,如果没有就用property_file里面的Property_File_Path赋值给property 里面的 image_path值!

primary_image_instance=PropertyFile.objects.filter(Q(Property=property_instance) & Q(Property_File_Type = 'PHO') & Q(RecordEndDate="NULL") & Q(primary_image_flag="1"))

2 change_file_image_flag改变显示图片的功能!同时去改变对应的 property_instance里面的image_path !

def change_file_image_flag(request, file_id):
    property_file_instance = get_object_or_404(PropertyFile,pk=int(file_id),RecordEndDate="NULL")
    property_instance = property_file_instance.Property#property_instance 
    property_id = property_instance.id
    if not can_control(property_file_instance, request): 
        return views.page_not_found(request)
    else:
        primary_images = PropertyFile.objects.filter(Q(Property=property_instance)&Q(RecordEndDate='NULL')&Q(primary_image_flag='1'))
        if primary_images:#exist primary_image,change other image flag and then change the instance for 1 
            for item in primary_images:
                item.primary_image_flag = '0'
                item.save()
        property_file_instance.primary_image_flag = '1'
        property_file_instance.save()
        property_instance.first().image_path = PropertyFile.objects.filter(Q(Property=property_instance.first()) & Q(Property_File_Type = 'PHO') & Q(RecordEndDate="NULL") & Q(primary_image_flag="1")).first().Property_File_Path
        property_instance.save()
        views.write_operation_log(property_file_instance, "Edit", request)
        return HttpResponseRedirect("/property/view_detail/%s?message=success&item=files" % property_id)

2. 增加image_path还可以是在改变primary_image的图片时候需要改动
功能设置的位置图片

具体实现的代码

def change_file_image_flag(request, file_id):
 #通过file_id和对象的类型找到property_file_instance,感觉传入的id 还是很重要的  
    property_file_instance = get_object_or_404(PropertyFile,pk=int(file_id),RecordEndDate="NULL")
    #因为file关联到property_instance上所以可以找到对应的 property_instance 
    property_instance = property_file_instance.Property
    property_id = property_instance.id
    if not can_control(property_file_instance, request): 
        return views.page_not_found(request)
    else:
    #通过判断Property,endDate,primary_image_flag是不是已经有primary_images
        primary_images_instance = PropertyFile.objects.filter(Q(Property=property_instance)&Q(RecordEndDate='NULL')&Q(primary_image_flag='1'))
        if primary_images:
        #exist primary_image,change other image flag and then change the instance for 1
            for item in primary_images:
                item.primary_image_flag = '0'
                item.save()
        property_file_instance.primary_image_flag = '1'
        property_file_instance.save()
        #同时改变property_instance对应的image_path里面的路径名称
        property_instance.image_path = PropertyFile.objects.filter(Q(Property=property_instance) & Q(Property_File_Type = 'PHO') & Q(RecordEndDate="NULL") & Q(primary_image_flag="1")).first().Property_File_Path
        property_instance.save()
        views.write_operation_log(property_file_instance, "Edit", request)
        return HttpResponseRedirect("/property/view_detail/%s?message=success&item=files" % property_id)

total_setting

对象的形式传过来!不是用string 的类型传过来!
这样的方式已经解决了! 使用的是一个列表的形式进行优化的!
total_setting是一个总的开关,达到的效果是指需要total_setting里面改,所有的setting都会变动!

OrderedDict 实现了一个有序list 的概念

from collections import OrderedDict

遇到的问题是没有办法传对象类型过来policy_jp_ind 和policy_xuhui_gov 这两个只能是以字符串的形式传过来!所以用的
以下的方式进行调整!

import total_setting
from collections import OrderedDict
# Load specific policy here

policy_list = OrderedDict()
policy_list[0]=policy_jp_ind
policy_list[1]=policy_xuhui_gov

policy = policy_list[total_setting.environment_setting]

系统部件的熟悉

static—-upload —上传文件的地方!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值