1- use crate :: apps:: Apps ;
21use crate :: gis:: { check_gis_setup, GISError } ;
3- use crate :: templates :: TemplateTags ;
4- use djls_ipc:: { JsonResponse , PythonProcess , TransportError , TransportMessage , TransportResponse } ;
2+ use djls_ipc :: v1 :: * ;
3+ use djls_ipc:: { ProcessError , PythonProcess , TransportError } ;
54use djls_python:: { ImportCheck , Python } ;
6- use serde:: Deserialize ;
75use std:: fmt;
86
97#[ derive( Debug ) ]
108pub struct DjangoProject {
119 py : Python ,
1210 python : PythonProcess ,
13- settings_module : String ,
14- installed_apps : Apps ,
15- templatetags : TemplateTags ,
16- }
17-
18- #[ derive( Debug , Deserialize ) ]
19- struct DjangoSetup {
20- installed_apps : Vec < String > ,
21- templatetags : TemplateTags ,
22- }
23-
24- impl DjangoSetup {
25- pub fn setup ( python : & mut PythonProcess ) -> Result < JsonResponse , ProjectError > {
26- let message = TransportMessage :: Json ( "django_setup" . to_string ( ) ) ;
27- let response = python. send ( message, None ) ?;
28- match response {
29- TransportResponse :: Json ( json_str) => {
30- let json_response: JsonResponse = serde_json:: from_str ( & json_str) ?;
31- Ok ( json_response)
32- }
33- _ => Err ( ProjectError :: Transport ( TransportError :: Process (
34- "Unexpected response type" . to_string ( ) ,
35- ) ) ) ,
36- }
37- }
11+ version : String ,
3812}
3913
4014impl DjangoProject {
41- fn new (
42- py : Python ,
43- python : PythonProcess ,
44- settings_module : String ,
45- installed_apps : Apps ,
46- templatetags : TemplateTags ,
47- ) -> Self {
15+ fn new ( py : Python , python : PythonProcess , version : String ) -> Self {
4816 Self {
4917 py,
5018 python,
51- settings_module,
52- installed_apps,
53- templatetags,
19+ version,
5420 }
5521 }
5622
5723 pub fn setup ( mut python : PythonProcess ) -> Result < Self , ProjectError > {
58- let settings_module =
59- std:: env:: var ( "DJANGO_SETTINGS_MODULE" ) . expect ( "DJANGO_SETTINGS_MODULE must be set" ) ;
60-
6124 let py = Python :: setup ( & mut python) ?;
6225
6326 let has_django = ImportCheck :: check ( & mut python, Some ( vec ! [ "django" . to_string( ) ] ) ) ?;
@@ -74,45 +37,52 @@ impl DjangoProject {
7437 return Ok ( Self {
7538 py,
7639 python,
77- settings_module,
78- installed_apps : Apps :: default ( ) ,
79- templatetags : TemplateTags :: default ( ) ,
40+ version : String :: new ( ) ,
8041 } ) ;
8142 }
8243
83- let response = DjangoSetup :: setup ( & mut python) ?;
84- let setup: DjangoSetup = response
85- . data ( )
86- . clone ( )
87- . ok_or_else ( || TransportError :: Process ( "No data in response" . to_string ( ) ) )
88- . and_then ( |data| serde_json:: from_value ( data) . map_err ( TransportError :: Json ) ) ?;
44+ let request = messages:: Request {
45+ command : Some ( messages:: request:: Command :: DjangoGetProjectInfo (
46+ django:: GetProjectInfoRequest { } ,
47+ ) ) ,
48+ } ;
8949
90- Ok ( Self :: new (
50+ let response = python
51+ . send ( request)
52+ . map_err ( |e| ProjectError :: Transport ( e) ) ?;
53+
54+ let version = match response. result {
55+ Some ( messages:: response:: Result :: DjangoGetProjectInfo ( response) ) => {
56+ response. project . unwrap ( ) . version
57+ }
58+ Some ( messages:: response:: Result :: Error ( e) ) => {
59+ return Err ( ProjectError :: Process ( ProcessError :: Health ( e. message ) ) ) ;
60+ }
61+ _ => {
62+ return Err ( ProjectError :: Process ( ProcessError :: Response ) ) ;
63+ }
64+ } ;
65+
66+ Ok ( Self {
9167 py,
9268 python,
93- settings_module,
94- Apps :: from_strings ( setup. installed_apps . to_vec ( ) ) ,
95- setup. templatetags ,
96- ) )
69+ version,
70+ } )
9771 }
9872
9973 pub fn py ( & self ) -> & Python {
10074 & self . py
10175 }
10276
103- fn settings_module ( & self ) -> & String {
104- & self . settings_module
77+ fn version ( & self ) -> & String {
78+ & self . version
10579 }
10680}
10781
10882impl fmt:: Display for DjangoProject {
10983 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
11084 writeln ! ( f, "Django Project" ) ?;
111- writeln ! ( f, "Settings Module: {}" , self . settings_module) ?;
112- writeln ! ( f, "Installed Apps:" ) ?;
113- write ! ( f, "{}" , self . installed_apps) ?;
114- writeln ! ( f, "Template Tags:" ) ?;
115- write ! ( f, "{}" , self . templatetags) ?;
85+ writeln ! ( f, "Version: {}" , self . version) ?;
11686 Ok ( ( ) )
11787 }
11888}
@@ -121,22 +91,18 @@ impl fmt::Display for DjangoProject {
12191pub enum ProjectError {
12292 #[ error( "Django is not installed or cannot be imported" ) ]
12393 DjangoNotFound ,
124-
12594 #[ error( "IO error: {0}" ) ]
12695 Io ( #[ from] std:: io:: Error ) ,
127-
12896 #[ error( "GIS error: {0}" ) ]
12997 Gis ( #[ from] GISError ) ,
130-
13198 #[ error( "JSON parsing error: {0}" ) ]
13299 Json ( #[ from] serde_json:: Error ) ,
133-
134100 #[ error( transparent) ]
135101 Packaging ( #[ from] djls_python:: PackagingError ) ,
136-
102+ #[ error( "Process error: {0}" ) ]
103+ Process ( #[ from] ProcessError ) ,
137104 #[ error( transparent) ]
138105 Python ( #[ from] djls_python:: PythonError ) ,
139-
140106 #[ error( "Transport error: {0}" ) ]
141107 Transport ( #[ from] TransportError ) ,
142108}
0 commit comments