views.py 60.3 KB
Newer Older
Muhamad's avatar
Muhamad committed
1 2
from django.http import response
from API.views import jalan
Muhamad's avatar
Muhamad committed
3
from django.contrib.auth.models import User, Group, Permission
Manggar Mahardhika's avatar
Manggar Mahardhika committed
4 5 6
from django.shortcuts import render, redirect
# from django.views.generic import ListView, DetailView,  DeleteView, UpdateView, CreateView
from django.views import View, generic
Muhamad's avatar
Muhamad committed
7 8
from django.contrib.auth.decorators import login_required, user_passes_test
from django.utils.decorators import method_decorator
Manggar Mahardhika's avatar
Manggar Mahardhika committed
9
from django.db import connection as conn
Muhamad's avatar
Muhamad committed
10 11
from django.core.mail import send_mail
from django.conf import settings
Muhamad's avatar
Muhamad committed
12
# from edit import *
Manggar Mahardhika's avatar
Manggar Mahardhika committed
13 14 15

class Dashboard(generic.TemplateView):
    template_name = "layout/layout.html"
Muhamad's avatar
Muhamad committed
16
    # @method_decorator(login_required(login_url='/login/'))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
17
    def get(self, request):
Muhamad's avatar
Muhamad committed
18
        # print(Group.objects.get(user=request.user))
19 20 21
        with conn.cursor() as all_:
            all_.execute("SELECT * from global_oku")
            all = all_.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
22
        
23 24 25
        # with conn.cursor() as kel:
        #     kel.execute("SELECT json_build_object('type', 'Feature', 'administrasi', (kode_desa, desa, kecamatan, kab_kota, provinsi, jumlah_pen, jumlah_kk, luas_desa, gambar),'penduduk', (pria, wanita, belum_kawin, kawin, cerai_hidup, cerai_mati, wajib_ktp, islam, kristen, khatolik, hindu, budha, konghucu, kepercayaan_lain, u0, u5, u10, u15, u20, u25, u30, u35, u40, u45, u50, u55, u60, u65, u70, u75),'pekerjaan_pendidikan', (tidak_sekolah, belum_tamat, tamat_sd, sltp, slta, diploma_i, diploma_ii, diploma_iv, strata_ii, strata_iii, tidak_bekerja, aparatur_pemerintah, tenaga_pendidik, wiraswasta, pertanian, tenaga_kesehatan, pensiunan, pegawai, tentara, kepolisian, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, dosen, guru, pilot, pengacara, notaris, arsitek, akuntan, konsultan, dokter, bidan, perawat, psikiater, sopir, lainnya), 'poi',(jml_puskesma, jml_sekolah, jml_sarib),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kelurahan")
        #     kel_res_ = kel.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
26
        
27 28 29
        # with conn.cursor() as kec:
        #     kec.execute("SELECT kecamatan, json_build_object('type', 'Feature', 'administrasi', (id, kecamatan),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kecamatan")
        #     kec_res_ = kec.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
30
        
31 32 33
        # kec_res = []
        # for i in kec_res_:
        #     kec_res.append(i[1])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
34
        
35 36 37
        # kel_res = []
        # for i in kel_res_:
        #     kel_res.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
38
        
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        # with conn.cursor() as kantor_administrasi:
        #     kantor_administrasi.execute("""SELECT
        #                                     json_build_object (
        #                                         'type',
        #                                         'Feature',
        #                                         'administrasi',
        #                                         (
        #                                             A.namobj,
        #                                             A.remark,
        #                                             A.alamat,
        #                                             A.luas_bangunan,
        #                                             A.luas_tanah,
        #                                             A.satuan_luas_bangunan,
        #                                             A.satuan_luas_tanah,
        #                                             A.gambar,
        #                                             A.tinggi_bangunan,
        #                                             A.perangkat,
        #                                             A.tipe_gambar,
        #                                             b.kabupaten,
        #                                             c.kecamatan,
        #                                             d.desa,
        #                                             a.id,
        #                                             (
        #                                                 SELECT ARRAY_AGG
        #                                                     ( '' || ID || ',' || tgl_perbaikan || ',' || jenis_perbaikan || '' ORDER BY tgl_perbaikan DESC ) 
        #                                                 FROM
        #                                                     tbl_perbaikan 
        #                                                 WHERE
        #                                                     id_object = A.ID 
        #                                                     AND type_id = 1 
        #                                             )
        #                                         ),
        #                                         'geometry',
        #                                         ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                                     ) 
        #                                 FROM
        #                                     geo_kantor_administrasi A 
        #                                     JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                                     JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                                     join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                                 WHERE a.is_deleted = 0""")
        #     kantor_administrasi_res = kantor_administrasi.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
81
            
82 83 84 85 86
        # kantor_administrasi_result = []
        # for i in kantor_administrasi_res:
        #     if i[0]['administrasi']['f16'] == None:
        #         i[0]['administrasi']['f16'] = "None"
        #     kantor_administrasi_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
87
            
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        # with conn.cursor() as layanan_kesehatan:
        #     layanan_kesehatan.execute("""SELECT
        #                                     json_build_object (
        #                                         'type',
        #                                         'Feature',
        #                                         'administrasi',
        #                                         (
        #                                             A.namobj,
        #                                             A.remark,
        #                                             A.alamat,
        #                                             A.luas_bangunan,
        #                                             A.luas_tanah,
        #                                             A.satuan_luas_bangunan,
        #                                             A.satuan_luas_tanah,
        #                                             A.gambar,
        #                                             A.tinggi_bangunan,
        #                                             A.perangkat,
        #                                             A.tipe_gambar,
        #                                             b.kabupaten,
        #                                             c.kecamatan,
        #                                             d.desa,
        #                                             a.id
        #                                         ),
        #                                         'geometry',
        #                                         ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                                     ) 
        #                                 FROM
        #                                     geo_layanan_kesehatan A 
        #                                     JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                                     JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                                     join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                                 WHERE a.is_deleted = 0""")
        #     layanan_kesehatan_res = layanan_kesehatan.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
121
        
122 123 124
        # layanan_kesehatan_result = []
        # for i in layanan_kesehatan_res:
        #     layanan_kesehatan_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
125
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
126
            
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        # with conn.cursor() as puskesmas:
        #     puskesmas.execute("""SELECT
        #                             json_build_object (
        #                                 'type',
        #                                 'Feature',
        #                                 'administrasi',
        #                                 (
        #                                     A.namobj,
        #                                     A.remark,
        #                                     A.alamat,
        #                                     A.luas_bangunan,
        #                                     A.luas_tanah,
        #                                     A.satuan_luas_bangunan,
        #                                     A.satuan_luas_tanah,
        #                                     A.gambar,
        #                                     A.tinggi_bangunan,
        #                                     A.perangkat,
        #                                     A.tipe_gambar,
        #                                     b.kabupaten,
        #                                     c.kecamatan,
        #                                     d.desa,
        #                                     a.id
        #                                 ),
        #                                 'geometry',
        #                                 ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                             ) 
        #                         FROM
        #                             geo_puskesmas A 
        #                             JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                             JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                             join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                         WHERE a.is_deleted = 0""")
        #     puskesmas_res = puskesmas.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
160
        
161 162 163
        # puskesmas_result = []
        # for i in puskesmas_res:
        #     puskesmas_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
164
            
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        # with conn.cursor() as rumah_sakit:
            # rumah_sakit.execute("""SELECT
            #                             json_build_object (
            #                                 'type',
            #                                 'Feature',
            #                                 'administrasi',
            #                                 (
            #                                     A.namobj,
            #                                     A.remark,
            #                                     A.alamat,
            #                                     A.luas_bangunan,
            #                                     A.luas_tanah,
            #                                     A.satuan_luas_bangunan,
            #                                     A.satuan_luas_tanah,
            #                                     A.gambar,
            #                                     A.tinggi_bangunan,
            #                                     A.perangkat,
            #                                     A.tipe_gambar,
            #                                     b.kabupaten,
            #                                     c.kecamatan,
            #                                     d.desa,
            #                                     a.id
            #                                 ),
            #                                 'geometry',
            #                                 ST_AsGeoJSON ( A.geom :: geometry ) :: json 
            #                             ) 
            #                         FROM
            #                             geo_rumah_sakit A 
            #                             JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
            #                             JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
            #                             join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
            #                         WHERE a.is_deleted = 0""")
        #     rumah_sakit_res = rumah_sakit.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
198
        
199 200 201
        # rumah_sakit_result = []
        # for i in rumah_sakit_res:
        #     rumah_sakit_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
202
            
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        # with conn.cursor() as sarana_ibadah:
        #     sarana_ibadah.execute("""SELECT
        #                                 json_build_object (
        #                                     'type',
        #                                     'Feature',
        #                                     'administrasi',
        #                                     (
        #                                         A.namobj,
        #                                         A.remark,
        #                                         A.alamat,
        #                                         A.luas_bangunan,
        #                                         A.luas_tanah,
        #                                         A.satuan_luas_bangunan,
        #                                         A.satuan_luas_tanah,
        #                                         A.gambar,
        #                                         A.tinggi_bangunan,
        #                                         A.perangkat,
        #                                         A.tipe_gambar,
        #                                         b.kabupaten,
        #                                         c.kecamatan,
        #                                         d.desa,
        #                                         a.id
        #                                     ),
        #                                     'geometry',
        #                                     ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                                 ) 
        #                             FROM
        #                                 geo_sarana_ibadah A 
        #                                 JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                                 JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                                 join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                             WHERE a.is_deleted = 0""")
        #     sarana_ibadah_res = sarana_ibadah.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
236
        
237 238 239 240 241
        # sarana_ibadah_result = []
        # for i in sarana_ibadah_res:
        #     if i[0]["administrasi"]['f1'] == None:
        #         i[0]["administrasi"]['f1'] = "None"
        #     sarana_ibadah_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
242
            
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        # with conn.cursor() as sarana_pendidikan:
        #     sarana_pendidikan.execute("""SELECT
        #                                     json_build_object (
        #                                         'type',
        #                                         'Feature',
        #                                         'administrasi',
        #                                         (
        #                                             A.namobj,
        #                                             A.remark,
        #                                             A.alamat,
        #                                             A.luas_bangunan,
        #                                             A.luas_tanah,
        #                                             A.satuan_luas_bangunan,
        #                                             A.satuan_luas_tanah,
        #                                             A.gambar,
        #                                             A.tinggi_bangunan,
        #                                             A.perangkat,
        #                                             A.tipe_gambar,
        #                                             b.kabupaten,
        #                                             c.kecamatan,
        #                                             d.desa,
        #                                             a.id
        #                                         ),
        #                                         'geometry',
        #                                         ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                                     ) 
        #                                 FROM
        #                                     geo_sarana_pendidikan A 
        #                                     JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                                     JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                                     join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                                 WHERE a.is_deleted = 0""")
        #     sarana_pendidikan_res = sarana_pendidikan.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
276
        
277 278 279 280 281
        # sarana_pendidikan_result = []
        # for i in sarana_pendidikan_res:
        #     if i[0]["administrasi"]['f1'] == None:
        #         i[0]["administrasi"]['f1'] = "None"
        #     sarana_pendidikan_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
282
            
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        # with conn.cursor() as spbu:
        #     spbu.execute("""SELECT
        #                         json_build_object (
        #                             'type',
        #                             'Feature',
        #                             'administrasi',
        #                             (
        #                                 A.namobj,
        #                                 A.remark,
        #                                 A.alamat,
        #                                 A.luas_bangunan,
        #                                 A.luas_tanah,
        #                                 A.satuan_luas_bangunan,
        #                                 A.satuan_luas_tanah,
        #                                 A.gambar,
        #                                 A.tinggi_bangunan,
        #                                 A.perangkat,
        #                                 A.tipe_gambar,
        #                                 b.kabupaten,
        #                                 c.kecamatan,
        #                                 d.desa,
        #                                 a.id
        #                             ),
        #                             'geometry',
        #                             ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                         ) 
        #                     FROM
        #                         geo_spbu A 
        #                         JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                         JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                         join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                     WHERE a.is_deleted = 0""")
        #     spbu_res = spbu.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
316
        
317 318 319
        # spbu_result = []
        # for i in spbu_res:
        #     spbu_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
320
            
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        # with conn.cursor() as stasiun:
        #     stasiun.execute("""SELECT
        #                             json_build_object (
        #                                 'type',
        #                                 'Feature',
        #                                 'administrasi',
        #                                 (
        #                                     A.namobj,
        #                                     A.remark,
        #                                     A.alamat,
        #                                     A.luas_bangunan,
        #                                     A.luas_tanah,
        #                                     A.satuan_luas_bangunan,
        #                                     A.satuan_luas_tanah,
        #                                     A.gambar,
        #                                     A.tinggi_bangunan,
        #                                     A.perangkat,
        #                                     A.tipe_gambar,
        #                                     b.kabupaten,
        #                                     c.kecamatan,
        #                                     d.desa,
        #                                     a.id
        #                                 ),
        #                                 'geometry',
        #                                 ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                             ) 
        #                         FROM
        #                             geo_stasiun A 
        #                             JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                             JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                             join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                         WHERE a.is_deleted = 0""")
        #     stasiun_res = stasiun.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
354
        
355 356 357
        # stasiun_result = []
        # for i in stasiun_res:
        #     stasiun_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
358
            
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        # with conn.cursor() as terminal:
        #     terminal.execute("""SELECT
        #                             json_build_object (
        #                                 'type',
        #                                 'Feature',
        #                                 'administrasi',
        #                                 (
        #                                     A.namobj,
        #                                     A.remark,
        #                                     A.alamat,
        #                                     A.luas_bangunan,
        #                                     A.luas_tanah,
        #                                     A.satuan_luas_bangunan,
        #                                     A.satuan_luas_tanah,
        #                                     A.gambar,
        #                                     A.tinggi_bangunan,
        #                                     A.perangkat,
        #                                     A.tipe_gambar,
        #                                     b.kabupaten,
        #                                     c.kecamatan,
        #                                     d.desa,
        #                                     a.luas,
        #                                     a.id
        #                                 ),
        #                                 'geometry',
        #                                 ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                             ) 
        #                         FROM
        #                             geo_terminal_bus A 
        #                             JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                             JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                             join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                         WHERE a.is_deleted = 0""")
        #     terminal_res = terminal.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
393
        
394 395 396
        # terminal_result = []
        # for i in terminal_res:
        #     terminal_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
397
            
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        # with conn.cursor() as cagar_budaya:
        #     cagar_budaya.execute("""SELECT
        #                                 json_build_object (
        #                                     'type',
        #                                     'Feature',
        #                                     'administrasi',
        #                                     (
        #                                         A.namobj,
        #                                         A.remark,
        #                                         A.alamat,
        #                                         A.gambar,
        #                                         A.perangkat,
        #                                         A.tipe_gambar,
        #                                         b.kabupaten,
        #                                         c.kecamatan,
        #                                         d.desa,
        #                                         a.luas,
        #                                         a.id
        #                                     ),
        #                                     'geometry',
        #                                     ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                                 ) 
        #                             FROM
        #                                 geo_cagar_budaya A 
        #                                 JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                                 JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                                 join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                             WHERE a.is_deleted = 0""")
        #     cagar_budaya_res = cagar_budaya.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
427
        
428 429 430
        # cagar_budaya_result = []
        # for i in cagar_budaya_res:
        #     cagar_budaya_result.append(i[0])
Manggar Mahardhika's avatar
Manggar Mahardhika committed
431
            
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        # with conn.cursor() as gardu_listrik:
        #     gardu_listrik.execute("""SELECT
        #                                 json_build_object (
        #                                     'type',
        #                                     'Feature',
        #                                     'administrasi',
        #                                     (
        #                                         A.namobj,
        #                                         A.remark,
        #                                         A.alamat,
        #                                         A.luas_bangunan,
        #                                         A.luas_tanah,
        #                                         A.satuan_luas_bangunan,
        #                                         A.satuan_luas_tanah,
        #                                         A.gambar,
        #                                         A.tinggi_bangunan,
        #                                         A.perangkat,
        #                                         A.tipe_gambar,
        #                                         b.kabupaten,
        #                                         c.kecamatan,
        #                                         d.desa,
        #                                         a.id
        #                                     ),
        #                                     'geometry',
        #                                     ST_AsGeoJSON ( A.geom :: geometry ) :: json 
        #                                 ) 
        #                             FROM
        #                                 geo_gardu_listrik A 
        #                                 JOIN geo_prov_kab b ON st_intersects ( A.geom, b.geom )
        #                                 JOIN geo_data_kecamatan c ON st_intersects(A.geom, c.geom)
        #                                 join geo_data_kelurahan d on st_intersects(A.geom, d.geom)
        #                             WHERE a.is_deleted = 0""")
        #     gardu_listrik_res = gardu_listrik.fetchall()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
465
        
466 467 468
        # gardu_listrik_result = []
        # for i in gardu_listrik_res:
        #     gardu_listrik_result.append(i[0])
469

Muhamad's avatar
Muhamad committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
        # with conn.cursor() as data_per:
        #     data_per.execute("""
        #                     SELECT
        #                         gl.NAME,
        #                         tp.id_object,
        #                         tp.NAME,
        #                         tp.TYPE,
        #                         tp.tgl_perbaikan,
        #                         tp.anggaran,
        #                         tp.tgl_selesai,
        #                         gdl.desa,
        #                         gdc.kecamatan
        #                     FROM
        #                         ( SELECT tbl_perbaikan.*, ROW_NUMBER ( ) OVER ( PARTITION BY id_object ORDER BY tgl_selesai DESC ) AS rn FROM tbl_perbaikan ) tp
        #                         JOIN geo_label gl ON gl.ID = tp.type_id 
        #                         JOIN geo_data_kelurahan gdl on st_intersects(st_setsrid(gdl.geom, 4326), st_setsrid(tp.geom, 4326))
        #                         JOIN geo_data_kecamatan gdc ON st_intersects ( st_setsrid ( gdc.geom, 4326 ), st_setsrid ( tp.geom, 4326 ) )
        #                     """)
        #     res_perbaikan = data_per.fetchall()
489
        
Muhamad's avatar
Muhamad committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503
        # res_all = []
        # for j in res_perbaikan:
        #     data_all = {
        #         'name_perbaikan':j[0],
        #         'id_object':j[1],
        #         'name':j[2],
        #         'type':j[3],
        #         'tgl_perbaikan':j[4],
        #         'anggaran':j[5],
        #         'tgl_selesai':j[6],
        #         'desa':j[7],
        #         'kecamatan':j[8],
        #     }
        #     res_all.append(data_all)
Muhamad's avatar
x  
Muhamad committed
504
        
505 506 507 508
        perm_group = []
        if request.user.is_authenticated:
            current_user = request.user
            thisUser     = current_user.id
Muhamad's avatar
Muhamad committed
509

510 511
            for xx in request.user.get_group_permissions():
                perm_group.append(xx.split(".")[1])
Muhamad's avatar
Muhamad committed
512

513 514 515 516 517 518 519 520
            if thisUser == None:
                user_data = {"filepath":'placeholder.jpg'}
            else:
            # print()
                sql = "select * from auth_user where id = "+str(thisUser)
                with conn.cursor() as userprop:
                    userprop.execute(sql)
                    us = userprop.fetchall()
Muhamad's avatar
Muhamad committed
521
                
522 523 524 525 526 527 528 529 530 531 532
                user_data = []
                for row in us:
                    datas = {
                        "id": row[0],
                        "username": row[4],
                        "firstname": row[5],
                        "lastname": row[6],
                        "email"   : row[7],
                        "filepath": row[11],
                        # "password": row[1]
                    }
Muhamad's avatar
Muhamad committed
533

534
                    user_data.append(datas)
Muhamad's avatar
Muhamad committed
535
                    
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
            with conn.cursor() as userlist_:
                if request.user.is_superuser or request.user.is_staff:
                    # print("is superuser")
                    sql = """
                            SELECT
                            au.id,
                            au.username,
                            au.first_name,
                            au.last_name,
                            au.is_active,
                            au.last_login,
                            au.email,
                            ag.name,
                            au.is_staff,
                            au.is_superuser,
                            au.file_path
                        FROM
                            auth_user au,
                            auth_user_groups aug,
                            auth_group ag
                        WHERE
                            au.id = aug.user_id AND
                            ag.id = aug.group_id
                        ORDER BY 
                            au.username ASC"""
                else:
                    sql = """
                        SELECT
                        au.id,
                        au.username,
                        au.first_name,
                        au.last_name,
                        au.is_active,
                        au.last_login,
                        au.email,
                        ag.name,
                        au.is_staff,
                        au.is_superuser,
                        au.file_path
                    FROM
                        auth_user au,
                        auth_user_groups aug,
                        auth_group ag
                    WHERE
                        au.id = aug.user_id AND
                        ag.id = aug.group_id
                    ORDER BY 
                        au.username ASC"""
                userlist_.execute(sql)
                allUser = userlist_.fetchall()
Muhamad's avatar
Muhamad committed
586
            
587 588 589 590 591 592 593 594 595 596 597 598 599
            user_res = []
            for rows in allUser:
                datas = {
                    "id": rows[0],
                    "username": rows[1],
                    "name": rows[2] +" "+ rows[3],
                    "email": rows[6],
                    "isactive": rows[4],
                    "last": rows[5],
                    "group": rows[7],
                    "filepath": rows[10]
                }
                user_res.append(datas)
Muhamad's avatar
Muhamad committed
600
                
601 602 603
        else:
            user_data   = [{"id":"Not Login", "username":"Not Login", "firstname":"Not Login", "lastname":"Not Login","email":"Not Login","filepath":"Not Login"}]
            user_res    = [{"id":"Not Login", "username":"Not Login", "name":"Not Login", "isactive":"Not Login","email":"Not Login","last":"Not Login", "group":"Not Login", "filepath":"Not Login"}]
Muhamad's avatar
Muhamad committed
604 605

            
Muhamad's avatar
Muhamad committed
606
            
Manggar Mahardhika's avatar
Manggar Mahardhika committed
607
        context = {
Muhamad's avatar
Muhamad committed
608
        'title':'Dashboard',
609 610 611 612 613 614 615 616 617 618 619 620 621
        # 'kelurahan':kel_res,
        # 'kecamatan':kec_res,
        # 'kantor_administrasi':kantor_administrasi_result,
        # 'layanan_kesehatan':layanan_kesehatan_result,
        # 'puskesmas':puskesmas_result,
        # 'rumah_sakit':rumah_sakit_result,
        # 'sarana_ibadah':sarana_ibadah_result,
        # 'sarana_pendidikan':sarana_pendidikan_result,
        # 'spbu':spbu_result,
        # 'stasiun':stasiun_result,
        # 'terminal':terminal_result,
        # 'cagar_budaya':cagar_budaya_result,
        # 'gardu_listrik':gardu_listrik_result,
622
        'global':all[0],
Muhamad's avatar
Muhamad committed
623
        # 'res_all':res_all,
624 625 626
        'thisUser' : user_data,
        'perm_group' : perm_group,
        'user_res':user_res
Manggar Mahardhika's avatar
Manggar Mahardhika committed
627
        }
Manggar Mahardhika's avatar
Manggar Mahardhika committed
628 629
        return render (request, self.template_name, context)
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
630

Muhamad's avatar
Muhamad committed
631 632 633 634
from django import template
from django.contrib.auth.models import Group
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.response import Response
Manggar Mahardhika's avatar
Manggar Mahardhika committed
635 636


Muhamad's avatar
Muhamad committed
637
register = template.Library()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
638

Muhamad's avatar
Muhamad committed
639 640 641 642
@register.filter(name='has_group')
def has_group(user, group_name):
    group = Group.objects.get(name=group_name)
    return True if group in user.groups.all() else False
Manggar Mahardhika's avatar
Manggar Mahardhika committed
643

Manggar Mahardhika's avatar
Manggar Mahardhika committed
644 645 646 647
def saver():
    # feses = '/var/www/django/oku.khansia.co.id/static/media/'
    feses = 'static/media/'
    return feses
reynaldi adriantama's avatar
reynaldi adriantama committed
648

Muhamad's avatar
Muhamad committed
649
def perbaikanrenovasi(request):
Muhamad's avatar
Muhamad committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    with conn.cursor() as data_per:
        # data_per.execute("""
        #                  SELECT
        #                     gl.NAME,
        #                     tp.name,
        #                     tp.type,
        #                     st_length ( gj.geom :: geography ),
        #                     tp.tgl_pembuatan,
        #                     tp.tgl_perbaikan,
        #                     tp.anggaran,
        #                     tp.proposal,
        #                     tp.pj,
        #                     tp.pj_lapangan,
        #                     tp.kontraktor,
        #                     ST_AsGeoJSON ( tp.geom :: geometry ) :: json,
        #                     tp.pjg_perbaikan,
        #                     tp.lbr_perbaikan,
        #                     tp.id_object
        #                 FROM
        #                     tbl_perbaikan tp
        #                     JOIN geo_label gl ON gl.ID = tp.type_id
        #                     JOIN geo_jalan gj ON gj.ID = tp.id_object
        #                  """)
        data_per.execute("""
                         SELECT
                            gl.NAME,
                            tp.id_object,
                            tp.NAME,
                            tp.TYPE,
                            tp.tgl_perbaikan,
                            tp.anggaran,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
681 682 683
                            tp.tgl_selesai,
                            gdl.desa,
                            gdc.kecamatan
Muhamad's avatar
Muhamad committed
684 685 686
                        FROM
                            ( SELECT tbl_perbaikan.*, ROW_NUMBER ( ) OVER ( PARTITION BY id_object ORDER BY tgl_selesai DESC ) AS rn FROM tbl_perbaikan ) tp
                            JOIN geo_label gl ON gl.ID = tp.type_id 
Manggar Mahardhika's avatar
Manggar Mahardhika committed
687
                            JOIN geo_data_kelurahan gdl on st_intersects(st_setsrid(gdl.geom, 4326), st_setsrid(tp.geom, 4326))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
688
                            JOIN geo_data_kecamatan gdc ON st_intersects ( st_setsrid ( gdc.geom, 4326 ), st_setsrid ( tp.geom, 4326 ))
Muhamad's avatar
Muhamad committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
                         """)
        res_perbaikan = data_per.fetchall()
    
    res_all = []
    for j in res_perbaikan:
        print(j[0])
        # if j[0] == 'jalan':
        data_all = {
            'name_perbaikan':j[0],
            'id_object':j[1],
            'name':j[2],
            'type':j[3],
            'tgl_perbaikan':j[4],
            'anggaran':j[5],
            'tgl_selesai':j[6],
Manggar Mahardhika's avatar
Manggar Mahardhika committed
704 705
            'desa':j[7],
            'kecamatan':j[8],
Muhamad's avatar
Muhamad committed
706 707 708 709 710 711 712 713 714
        }
        res_all.append(data_all)
            
    context = {'res_all':res_all}
    return render(request, 'maps/perbaikan.html', context)

@api_view(('POST',))
def loadDetail(request):
    id_object = request.POST.get('id')
Muhamad's avatar
Muhamad committed
715 716 717 718 719
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            gl.NAME,
                            tp.name,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
720 721
                            tp.type,
                            st_length ( tp.geom :: geography ),
Muhamad's avatar
Muhamad committed
722 723
                            tp.tgl_pembuatan,
                            tp.tgl_perbaikan,
Muhamad's avatar
Muhamad committed
724
                            tp.tgl_selesai,
Muhamad's avatar
Muhamad committed
725 726 727 728 729 730 731
                            tp.anggaran,
                            tp.pj,
                            tp.pj_lapangan,
                            tp.kontraktor,
                            ST_AsGeoJSON ( tp.geom :: geometry ) :: json,
                            tp.pjg_perbaikan,
                            tp.lbr_perbaikan,
Muhamad's avatar
Muhamad committed
732 733 734
                            tp.id_object,
                            tp.id,
                            tp.proposal,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
735 736
                            tp.bukti_selesai,
                            tp.jenis_perbaikan
Muhamad's avatar
Muhamad committed
737 738 739
                        FROM
                            tbl_perbaikan tp
                            JOIN geo_label gl ON gl.ID = tp.type_id
Muhamad's avatar
Muhamad committed
740
                        where tp.id_object = %s"""% id_object)
Muhamad's avatar
Muhamad committed
741
        res_perbaikan = data_per.fetchall()
Muhamad's avatar
Muhamad committed
742 743
        
    res_all = []
Muhamad's avatar
Muhamad committed
744
    for j in res_perbaikan:
Muhamad's avatar
Muhamad committed
745
        data_all = {
Manggar Mahardhika's avatar
Manggar Mahardhika committed
746
                'jenis':j[0],
Muhamad's avatar
Muhamad committed
747 748 749 750 751
                'name':j[1],
                'type':j[2],
                'panjang':j[3],
                'tgl_pembuatan':j[4],
                'tgl_perbaikan':j[5],
Muhamad's avatar
Muhamad committed
752 753
                'tgl_selesai':j[6],
                'anggaran':j[7],
Muhamad's avatar
Muhamad committed
754 755 756 757 758 759
                'pj':j[8],
                'pj_lapangan':j[9],
                'kontraktor':j[10],
                'geometry':j[11],
                'pjg_jln_reno':j[12],
                'lbr_jln_reno':j[13],
Muhamad's avatar
Muhamad committed
760 761 762
                'id_object':j[14],
                'id':j[15],
                'proposal':j[16],
Manggar Mahardhika's avatar
Manggar Mahardhika committed
763 764
                'bukti_selesai':j[17],
                'jenis_perbaikan':j[18]
Muhamad's avatar
Muhamad committed
765
            }
Muhamad's avatar
Muhamad committed
766 767 768 769
        res_all.append(data_all)

    respon =res_all
    return Response(respon)
Muhamad's avatar
Muhamad committed
770

Muhamad's avatar
Muhamad committed
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
@api_view(('GET',))
def inpuperbaikan(request):
    id = request.GET.get('id_jalan')
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            namrjl,
                            remark,
                            st_length ( geom :: geography ),
                            json_build_object (
                                        'type', 'Feature',
                                        'geometry', ST_AsGeoJSON ( geom :: geometry ) :: json ) ,
                            id,
                            type_id
                        FROM
                            geo_jalan
                        where id = %s
                         """% id)
        res_data = data_per.fetchall()
    
    respon = res_data[0]
    return Response(respon)

Manggar Mahardhika's avatar
Manggar Mahardhika committed
794 795 796 797 798 799 800
@api_view(('GET',))
def inputperbaikanbangunan(request):
    id = request.GET.get('id_bangunan')
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            name,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
801
                            type_id,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
802
                            json_build_object ( 'type', 'Feature', 'geometry', ST_AsGeoJSON ( geom :: geometry ) :: json ),
Manggar Mahardhika's avatar
Manggar Mahardhika committed
803 804 805 806
                            ID,
                            remark,
                            address_1,
                            address_2
Manggar Mahardhika's avatar
Manggar Mahardhika committed
807 808 809 810 811 812 813 814 815 816
                        FROM
                            geo_bangunan 
                        WHERE
                            ID = %s
                         """% id)
        res_data = data_per.fetchall()
    
    respon = res_data[0]
    return Response(respon)

Muhamad's avatar
Muhamad committed
817 818 819 820
from django.core.files.storage import FileSystemStorage
import random
import re
import json
Muhamad's avatar
Muhamad committed
821
@api_view(('POST',))
Muhamad's avatar
Muhamad committed
822
def saveperbaikan(request):
823 824
    # type_id         = request.POST.get("type_id")
    type_id         = 2
Muhamad's avatar
Muhamad committed
825 826 827 828 829 830 831 832 833 834 835 836 837
    id_object       = request.POST.get("object_id")
    name            = request.POST.get("name")
    type            = request.POST.get("type")
    tgl_pembuatan   = request.POST.get("tgl_pembuatan")
    tgl_perbaikan   = request.POST.get("tgl_perbaikan")
    tgl_disetujui   = request.POST.get("tgl_disetujui")
    anggaran        = request.POST.get("anggaran")
    
    
    pj              = request.POST.get("pj")
    pj_lapangan     = request.POST.get("pj_lapangan")
    kontraktor      = request.POST.get("kontraktor")
    pjg_perbaikan   = request.POST.get("pjg_perbaikan")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
838
    sumber_dana     = request.POST.get("sumber_dana")
Muhamad's avatar
Muhamad committed
839 840
    lbr_perbaikan   = request.POST.get("lbr_perbaikan")
    jenis_perbaikan = request.POST.get("jenis_perbaikan")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
841
    # luas            = request.POST.get("luas")
Muhamad's avatar
Muhamad committed
842 843 844 845 846 847 848
    coor            = request.POST.get("coord")
    tgl_selesai     = request.POST.get("tgl_selesai")
    
    proposal        = request.FILES.get("proposal")
    bukti_selesai   = request.FILES.get("bukti_selesai")
    print(coor, proposal)
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
849 850 851
    save = saver()
    fss_p = FileSystemStorage(location=str(str(save)+'proposal/'))
    fss_b = FileSystemStorage(location=str(str(save)+'bukti_penyelesaian/'))
Muhamad's avatar
Muhamad committed
852
    
853
    # coord = json.loads(str(coor))
Muhamad's avatar
Muhamad committed
854 855 856
    
    pjg_perbaikan = re.sub('[^0-9.]+', '', str(pjg_perbaikan))
    lbr_perbaikan = re.sub('[^0-9.]+', '', str(lbr_perbaikan))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
857
    # luas = re.sub('[^0-9.]+', '', str(luas))
Muhamad's avatar
Muhamad committed
858
    
859 860 861 862 863 864 865 866 867 868
    # if coord['features'][0]['geometry']['type'] == 'LineString':
    #     print("oke")
    #     for co in coord['features']:
    #         ll = str(co["geometry"]['coordinates'])
    #         ll += ll
    #     ll = ll.replace(", "," ")
    #     ll = ll.replace("]][[","),(")
    #     ll = ll.replace("] [",", ")
    #     ll = ll.replace("]]",")")
    #     ll = ll.replace("[[","(")
Muhamad's avatar
Muhamad committed
869

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
    #     coordinat = f'MULTILINESTRING({ll})'
    #     print(ll)
    # if coord['features'][0]['geometry']['type'] == 'Polygon':
    #     print('tidakoke')
    
    yak = str(coor)
    suk = yak.replace("""{"type":"Feature","properties":{},"geometry":{"type":"LineString","coordinates":""","")
    sik = suk.replace("""}}""","")
    cor = sik.replace("""[[[""","(")
    coran = cor.replace("""]]]""",")")
    ll = coran.replace(","," ")
    ii = ll.replace("] [",",")
    mm = ii.replace('{"type":"FeatureCollection" "features":', '')
    nn = mm.replace('}', '')
    
    corcoran = f'MULTILINESTRING({nn})'
    
    format = (proposal.name).split(".")
    num = random.randrange(1, 10000000000000)
    nproposal = f"{name}_{num}.{format[-1:][0]}"
    nproposal = nproposal.replace(" ","-")
    filename_p = fss_p.save(nproposal, proposal)
    url_p = fss_p.url(filename_p)
Muhamad's avatar
Muhamad committed
893

894 895 896 897 898 899 900 901 902 903 904
    format_ = (bukti_selesai.name).split(".")
    nbukti = f"{name}_{num}.{format_[-1:][0]}"
    nbukti = nbukti.replace(" ","-")
    filename_b = fss_b.save(nbukti, bukti_selesai)
    url_b = fss_b.url(filename_b)
    
    print("-------------------")
    print(str(corcoran))
    print("-------------------")
    print(str(type_id),("-"),str(id_object),("-"),str(name),("-"),str(type),("-"),str(tgl_selesai),("-"),str(tgl_perbaikan),("-"),str(tgl_disetujui),("-"),str(anggaran),("-"),str(pj),("-"),str(pj_lapangan),("-"),str(kontraktor),("-"),str(pjg_perbaikan),("-"),str(lbr_perbaikan),("-"),str(jenis_perbaikan),("-"),str(nproposal),("-"),str(nbukti))
    print("-------------------")
Muhamad's avatar
Muhamad committed
905
        
906
    try:
Muhamad's avatar
Muhamad committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
        with conn.cursor() as data_per:
            data_per.execute("""
                        INSERT INTO tbl_perbaikan (
                            type_id,
                            id_object,
                            name,
                            type,
                            tgl_selesai,
                            tgl_perbaikan,
                            tgl_disetujui,
                            anggaran,
                            proposal,
                            bukti_selesai,
                            pj,
                            pj_lapangan,
                            kontraktor,
                            pjg_perbaikan,
                            lbr_perbaikan,
                            jenis_perbaikan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
926
                            sumber_dana,
Muhamad's avatar
Muhamad committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
                            geom
                        )
                        VALUES
                            (
                                %s,
                                %s,
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                %s,
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                '%s',
                                %s,
                                %s,
                                '%s',
Manggar Mahardhika's avatar
Manggar Mahardhika committed
947
                                '%s',
948
                                ST_GeomFromText('%s')                            
Muhamad's avatar
Muhamad committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
                            )
                            """% (
                                int(type_id),
                                int(id_object),
                                name,
                                type,
                                tgl_selesai,
                                tgl_perbaikan,
                                tgl_disetujui,
                                int(anggaran),
                                nproposal,
                                nbukti,
                                pj,
                                pj_lapangan,
                                kontraktor,
                                float(pjg_perbaikan),
                                float(lbr_perbaikan),
                                jenis_perbaikan,
Manggar Mahardhika's avatar
Manggar Mahardhika committed
967
                                sumber_dana,
968
                                corcoran))
Muhamad's avatar
Muhamad committed
969
        conn.commit()
Manggar Mahardhika's avatar
Manggar Mahardhika committed
970
        data = "Sukses"
Muhamad's avatar
Muhamad committed
971 972 973 974 975 976 977 978 979 980
        info = "Data Telah Berhasil di Perbaharui"
        code = 0

    except:
        data = "Pastikan Semua Data Terisi Termasuk Proposal dan Bukti Selesai"
        info = "Oops"
        code = 1
    respon = {"data":data,"info":info,"code":code}
    return Response(respon)

Manggar Mahardhika's avatar
Manggar Mahardhika committed
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
# from django.core.files.storage import FileSystemStorage
# import random
# import re
# import json
@api_view(('POST',))
def saveperbaikanbangunan(request):
    type_id         = '1'
    id_object       = request.POST.get("object_idbang")
    name            = request.POST.get("namebang")
    type            = request.POST.get("typebang")
    # tgl_pembuatan   = request.POST.get("tgl_pembuatanbang")
    tgl_perbaikan   = request.POST.get("tgl_perbaikanbang")
    tgl_disetujui   = request.POST.get("tgl_disetujuibang")
    anggaran        = request.POST.get("anggaranbang")
    
    
    pj              = request.POST.get("pjbang")
    pj_lapangan     = request.POST.get("pj_lapanganbang")
    kontraktor      = request.POST.get("kontraktorbang")
    jenis_perbaikan = request.POST.get("jenis_perbaikanbang")
    coor            = request.POST.get("coordbang")
    
    # for co in coor['features']:
    # ll = coor['geometry']['coordinates']
    # ll += ll
    # ll = ll.replace(", "," ")
    # ll = ll.replace("]][[","),(")
    # ll = ll.replace("] [",", ")
    # ll = ll.replace("]]",")")
    # ll = ll.replace("[[","(")
    # corcoran = ll
    # p_create = coor.replace('"','')
    # po_create = p_create.replace(', ','],[')
    # cood_edit = json.loads(coor)
    # co_edit = cood_edit['geometry']['coordinates']
    
    yak = str(coor)
    suk = yak.replace("""{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":""","")
    sik = suk.replace("""}}""","")
    cor = sik.replace("""[[[""","((")
    coran = cor.replace("""]]]""","))")
    ll = coran.replace(","," ")
    ii = ll.replace("] [",",")
    
    corcoran = f'MULTIPOLYGON({ii})'
    print("-------------------")
    print(str(corcoran))
    print("-------------------")
    # print(yak)
    tgl_selesai     = request.POST.get("tgl_selesaibang")
    
    proposal        = request.FILES.get("proposal_bang")
    bukti_selesai   = request.FILES.get("bukti_selesai_bang")
1034 1035
    print((proposal.name).split("."))
    print((bukti_selesai.name).split("."))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1036
    
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1037 1038 1039
    save = saver()
    fss_p = FileSystemStorage(location=str(str(save)+'proposal/'))
    fss_b = FileSystemStorage(location=str(str(save)+'bukti_penyelesaian/'))
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1040 1041

    try:
1042
        format1 = (proposal.name).split(".")
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1043
        num = random.randrange(1, 10000000000000)
1044
        nproposal = f"{name}_{num}.{format1[-1:][0]}"
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1045 1046 1047 1048
        nproposal = nproposal.replace(" ","-")
        filename_p = fss_p.save(nproposal, proposal)
        url_p = fss_p.url(filename_p)

1049 1050
        format2 = (bukti_selesai.name).split(".")
        nbukti = f"{name}_{num}.{format2[-1:][0]}"
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
        nbukti = nbukti.replace(" ","-")
        filename_b = fss_b.save(nbukti, bukti_selesai)
        url_b = fss_b.url(filename_b)
        
        with conn.cursor() as data_per:
            data_per.execute("""
                            INSERT INTO tbl_perbaikan (
                                type_id,
                                id_object,
                                name,
                                type,
                                tgl_selesai,
                                tgl_perbaikan,
                                tgl_disetujui,
                                anggaran,
                                proposal,
                                bukti_selesai,
                                pj,
                                pj_lapangan,
                                kontraktor,
                                jenis_perbaikan,
                                geom
                            )
                            VALUES
                                (
                                    %s,
                                    %s,
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    %s,
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    '%s',
                                    ST_GeomFromText('%s')
                                )
                                """% (
                                    int(type_id),
                                    int(id_object),
                                    name,
                                    type,
                                    tgl_selesai,
                                    tgl_perbaikan,
                                    tgl_disetujui,
                                    int(anggaran),
                                    nproposal,
                                    nbukti,
                                    pj,
                                    pj_lapangan,
                                    kontraktor,
                                    jenis_perbaikan,
                                    corcoran))
        conn.commit()
        data = "Sukses"
        info = "Data Telah Berhasil di Perbaharui"
        code = 0

    except:
        data = "Pastikan Semua Data Terisi Termasuk Proposal dan Bukti Selesai"
        info = "Oops"
        code = 1
    respon = {"data":data,"info":info,"code":code}
    return Response(respon)

Muhamad's avatar
Muhamad committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
@api_view(('GET',))
def loadbyiddetail(request):
    id = request.GET.get('id')
    id_jl = request.GET.get('id_jl')
    with conn.cursor() as detail_maps:
        detail_maps.execute("""
                            SELECT
                                json_build_object (
                                        'type', 'Feature',
                                        'geometry', ST_AsGeoJSON ( a.geom :: geometry ) :: json ),
                                json_build_object (
                                        'type', 'Feature',
                                        'geometry', ST_AsGeoJSON ( b.geom :: geometry ) :: json )
                            from 
                                tbl_perbaikan a, geo_jalan b
                            where
                                a.id = %s and b.id = %s
                            """% (id, id_jl))
        detail_ = detail_maps.fetchone()
    print(detail_)
    respon=detail_
    return Response(respon)

@api_view(('GET',))
def getjalanperbaikan(request):
    id = request.GET.get('id')

    with conn.cursor() as detail_maps:
        detail_maps.execute("""
                            SELECT
                                json_build_object (
                                        'type', 'Feature',
                                        'geometry', ST_AsGeoJSON ( a.geom :: geometry ) :: json )
                            from 
                                tbl_perbaikan a
                            where
                                a.id = %s
                            """% id)
        detail_ = detail_maps.fetchone()
    print(detail_)
    respon=detail_
    return Response(respon)

@api_view(('GET',))
def getdetailperbaikan(request):
    id_object = request.GET.get('id')
Muhamad's avatar
Muhamad committed
1166 1167 1168 1169 1170 1171 1172 1173 1174
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            gl.NAME,
                            tp.name,
                            gj.remark,
                            st_length ( gj.geom :: geography ),
                            tp.tgl_pembuatan,
                            tp.tgl_perbaikan,
Muhamad's avatar
Muhamad committed
1175
                            tp.tgl_selesai,
Muhamad's avatar
Muhamad committed
1176 1177 1178 1179 1180 1181 1182
                            tp.anggaran,
                            tp.pj,
                            tp.pj_lapangan,
                            tp.kontraktor,
                            ST_AsGeoJSON ( tp.geom :: geometry ) :: json,
                            tp.pjg_perbaikan,
                            tp.lbr_perbaikan,
Muhamad's avatar
Muhamad committed
1183 1184 1185 1186
                            tp.id_object,
                            tp.id,
                            tp.proposal,
                            tp.bukti_selesai
Muhamad's avatar
Muhamad committed
1187 1188 1189 1190
                        FROM
                            tbl_perbaikan tp
                            JOIN geo_label gl ON gl.ID = tp.type_id
                            JOIN geo_jalan gj ON gj.ID = tp.id_object
Muhamad's avatar
Muhamad committed
1191
                        where tp.id = %s"""% id_object)
Muhamad's avatar
Muhamad committed
1192
        res_perbaikan = data_per.fetchall()
Muhamad's avatar
Muhamad committed
1193
                
Muhamad's avatar
Muhamad committed
1194 1195 1196 1197 1198 1199 1200 1201
    res_all = []
    for j in res_perbaikan:
        data_all = {
                'name':j[1],
                'type':j[2],
                'panjang':j[3],
                'tgl_pembuatan':j[4],
                'tgl_perbaikan':j[5],
Muhamad's avatar
Muhamad committed
1202 1203
                'tgl_selesai':j[6],
                'anggaran':j[7],
Muhamad's avatar
Muhamad committed
1204 1205 1206 1207 1208 1209
                'pj':j[8],
                'pj_lapangan':j[9],
                'kontraktor':j[10],
                'geometry':j[11],
                'pjg_jln_reno':j[12],
                'lbr_jln_reno':j[13],
Muhamad's avatar
Muhamad committed
1210 1211 1212 1213
                'id_object':j[14],
                'id':j[15],
                'proposal':j[16],
                'bukti_selesai':j[17]
Muhamad's avatar
Muhamad committed
1214 1215 1216 1217
            }
        res_all.append(data_all)

    respon =res_all
Muhamad's avatar
Muhamad committed
1218 1219
    return Response(respon)

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
@api_view(('GET',))
def getdetailperbaikanbangunan(request):
    id_object = request.GET.get('id')
    with conn.cursor() as data_per:
        data_per.execute("""
                         SELECT
                            gl.NAME,
                            tp.name,
                            gj.remark,
                            tp.tgl_pembuatan,
                            tp.tgl_perbaikan,
                            tp.tgl_selesai,
                            tp.anggaran,
                            tp.pj,
                            tp.pj_lapangan,
                            tp.kontraktor,
                            ST_AsGeoJSON ( tp.geom :: geometry ) :: json,
                            tp.id_object,
                            tp.id,
                            tp.proposal,
                            tp.bukti_selesai,
                            tp.jenis_perbaikan
                        FROM
                            tbl_perbaikan tp
                            JOIN geo_label gl ON gl.ID = tp.type_id
                            JOIN geo_bangunan gj ON gj.ID = tp.id_object
                        where tp.id_object = %s
                        order by tp.tgl_perbaikan desc"""% id_object)
        res_perbaikan = data_per.fetchall()
                
    res_all = []
    for j in res_perbaikan:
        data_all = {
                'name':j[1],
                'type':j[2],
                'tgl_pembuatan':j[3],
                'tgl_perbaikan':j[4],
                'tgl_selesai':j[5],
                'anggaran':j[6],
                'pj':j[7],
                'pj_lapangan':j[8],
                'kontraktor':j[9],
                'geometry':j[10],
                'id_object':j[11],
                'id':j[12],
                'proposal':j[13],
                'bukti_selesai':j[14],
                'jenis_perbaikan':j[15]
            }
        res_all.append(data_all)

    respon = res_all
    return Response(respon)

Muhamad's avatar
Muhamad committed
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
@api_view(('GET',))
def editkelurahan(request):
    kode_dagri = request.GET.get('kode_dagri')
    
    with conn.cursor() as edit_poly:
        edit_poly.execute("SELECT json_build_object('type', 'Feature', 'administrasi', json_build_array(kode_desa, desa, kecamatan, kab_kota, provinsi, jumlah_pen, jumlah_kk, luas_desa),'penduduk', json_build_array(pria, wanita, belum_kawin, kawin, cerai_hidup, cerai_mati, wajib_ktp, islam, kristen, khatolik, hindu, budha, konghucu, kepercayaan_lain, u0, u5, u10, u15, u20, u25, u30, u35, u40, u45, u50, u55, u60, u65, u70, u75),'pekerjaan_pendidikan', json_build_array(tidak_sekolah, belum_tamat, tamat_sd, sltp, slta, diploma_i, diploma_ii, diploma_iv, strata_ii, strata_iii, tidak_bekerja, aparatur_pemerintah, tenaga_pendidik, wiraswasta, pertanian, tenaga_kesehatan, pensiunan, pegawai, tentara, kepolisian, pedagang, petani, peternak, nelayan, karyawan, buruh, pembantu, tukang, pendeta, pastor, ustadz, dosen, guru, pilot, pengacara, notaris, arsitek, akuntan, konsultan, dokter, bidan, perawat, psikiater, sopir, lainnya), 'poi',json_build_array(jml_puskesma, jml_sekolah, jml_sarib),'geometry', ST_AsGeoJSON(geom :: geometry) :: json) from geo_data_kelurahan where kode_desa = '%s'"% kode_dagri)
        res_edit_poly = edit_poly.fetchall()
        
    print(res_edit_poly)
    
    la = []
    lo = []
    for i in res_edit_poly[0][0]['geometry']['coordinates'][0][0]:
        la.append(i[0])
        lo.append(i[1])
        
    avg_la = sum(la)/len(la)
    avg_lo = sum(lo)/len(lo)
    
    desa = res_edit_poly[0][0]['administrasi'][1]
    kec = res_edit_poly[0][0]['administrasi'][2]
    kab = res_edit_poly[0][0]['administrasi'][3]
    prov = res_edit_poly[0][0]['administrasi'][4]
    context = {'desa':desa, 'kec':kec, 'kab':kab, 'prov':prov,'result_poly': res_edit_poly[0][0], 'avg_la':avg_la, 'avg_lo':avg_lo}
Muhamad's avatar
Muhamad committed
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    return Response(context)

@api_view(('GET',))
def loadPointbyId(request):
    tab = request.GET.get("db")
    id = request.GET.get("idx")
    tipe = request.GET.get("type")
    print(tab, id, tipe)
    
    context = {'result':'result'}
Muhamad's avatar
Muhamad committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
    return Response(context)

@api_view(('POST',))
def sendmail(request):
    pengirimh = request.POST.get('pengirimh')
    emailh = request.POST.get('emailh')
    isih = request.POST.get('pesanh')
    pengirim = request.POST.get('pengirim')
    email = request.POST.get('email')
    isi = request.POST.get('pesan')
    
    if isi != '':
        mess = f"nama pengirim : {pengirim} \nemail pengirim : {email} \nisi pesan : \n\n{isi}"
    elif isih != '':
        mess = f"nama pengirim : {pengirimh} \nemail pengirim : {emailh} \nisi pesan : \n\n{isih}"
    try:
        send_mail(subject='help & support', message=mess, from_email=settings.EMAIL_HOST_USER, recipient_list=[settings.RECIPIENT_ADDRESS])
        data = {
                "info" : "success",
                "code" : 0,
                "data" : "Pesan anda telah terkirim"
            }
    except:
        data = {
                "code" : 4,
                "data" : "Gagal mengirim pesan, silahkan coba beberapa saat lagi",
                "info" : "failed"
                }
        
    context = data
Muhamad's avatar
Muhamad committed
1338
    return Response(context)