13
13
from django .utils .translation import ugettext as _
14
14
import utils .s3 as s3
15
15
16
+ from ide .models .s3file import S3File
17
+ from ide .models .scriptfile import ScriptFile
16
18
from ide .models .meta import IdeModel
17
19
18
20
__author__ = 'katharine'
@@ -89,7 +91,7 @@ class Meta(IdeModel.Meta):
89
91
unique_together = (('project' , 'file_name' ),)
90
92
91
93
92
- class ResourceVariant (IdeModel ):
94
+ class ResourceVariant (S3File ):
93
95
resource_file = models .ForeignKey (ResourceFile , related_name = 'variants' )
94
96
95
97
VARIANT_DEFAULT = 0
@@ -116,6 +118,19 @@ class ResourceVariant(IdeModel):
116
118
tags = models .CommaSeparatedIntegerField (max_length = 50 , blank = True )
117
119
is_legacy = models .BooleanField (default = False ) # True for anything migrated out of ResourceFile
118
120
121
+ # The following three properties are overridden to support is_legacy
122
+ @property
123
+ def padded_id (self ):
124
+ return '%05d' % self .resource_file .id if self .is_legacy else '%09d' % self .id
125
+
126
+ @property
127
+ def s3_id (self ):
128
+ return self .resource_file .id if self .is_legacy else self .id
129
+
130
+ @property
131
+ def folder (self ):
132
+ return 'resources' if self .is_legacy else 'resources/variants'
133
+
119
134
def get_tags (self ):
120
135
return [int (tag ) for tag in self .tags .split ("," ) if tag ]
121
136
@@ -128,65 +143,6 @@ def get_tag_names(self):
128
143
def get_tags_string (self ):
129
144
return "" .join (self .get_tag_names ())
130
145
131
- def get_local_filename (self , create = False ):
132
- if self .is_legacy :
133
- padded_id = '%05d' % self .resource_file .id
134
- filename = '%sresources/%s/%s/%s' % (settings .FILE_STORAGE , padded_id [0 ], padded_id [1 ], padded_id )
135
- else :
136
- padded_id = '%09d' % self .id
137
- filename = '%sresources/variants/%s/%s/%s' % (settings .FILE_STORAGE , padded_id [0 ], padded_id [1 ], padded_id )
138
- if create :
139
- if not os .path .exists (os .path .dirname (filename )):
140
- os .makedirs (os .path .dirname (filename ))
141
- return filename
142
-
143
- def get_s3_path (self ):
144
- if self .is_legacy :
145
- return 'resources/%s' % self .resource_file .id
146
- else :
147
- return 'resources/variants/%s' % self .id
148
-
149
- local_filename = property (get_local_filename )
150
- s3_path = property (get_s3_path )
151
-
152
- def save_file (self , stream , file_size = 0 ):
153
- if file_size > 5 * 1024 * 1024 :
154
- raise Exception (_ ("Uploaded file too big." ))
155
- if not settings .AWS_ENABLED :
156
- if not os .path .exists (os .path .dirname (self .local_filename )):
157
- os .makedirs (os .path .dirname (self .local_filename ))
158
- with open (self .local_filename , 'wb' ) as out :
159
- out .write (stream .read ())
160
- else :
161
- s3 .save_file ('source' , self .s3_path , stream .read ())
162
-
163
- self .resource_file .project .last_modified = now ()
164
- self .resource_file .project .save ()
165
-
166
- def save_string (self , string ):
167
- if not settings .AWS_ENABLED :
168
- if not os .path .exists (os .path .dirname (self .local_filename )):
169
- os .makedirs (os .path .dirname (self .local_filename ))
170
- with open (self .local_filename , 'wb' ) as out :
171
- out .write (string )
172
- else :
173
- s3 .save_file ('source' , self .s3_path , string )
174
-
175
- self .resource_file .project .last_modified = now ()
176
- self .resource_file .project .save ()
177
-
178
- def copy_to_path (self , path ):
179
- if not settings .AWS_ENABLED :
180
- shutil .copy (self .local_filename , path )
181
- else :
182
- s3 .read_file_to_filesystem ('source' , self .s3_path , path )
183
-
184
- def get_contents (self ):
185
- if not settings .AWS_ENABLED :
186
- return open (self .local_filename ).read ()
187
- else :
188
- return s3 .read_file ('source' , self .s3_path )
189
-
190
146
def save (self , * args , ** kwargs ):
191
147
self .full_clean ()
192
148
self .resource_file .save ()
@@ -209,8 +165,7 @@ def get_root_path(self):
209
165
path = property (get_path )
210
166
root_path = property (get_root_path )
211
167
212
-
213
- class Meta (IdeModel .Meta ):
168
+ class Meta (S3File .Meta ):
214
169
unique_together = (('resource_file' , 'tags' ),)
215
170
216
171
@@ -271,89 +226,34 @@ def save(self, *args, **kwargs):
271
226
super (ResourceIdentifier , self ).save (* args , ** kwargs )
272
227
273
228
274
- class SourceFile (IdeModel ):
229
+ class SourceFile (ScriptFile ):
275
230
project = models .ForeignKey ('Project' , related_name = 'source_files' )
276
231
file_name = models .CharField (max_length = 100 , validators = [RegexValidator (r"^[/a-zA-Z0-9_.-]+\.(c|h|js)$" )])
277
- last_modified = models .DateTimeField (blank = True , null = True , auto_now = True )
278
- folded_lines = models .TextField (default = "[]" )
232
+ folder = 'sources'
279
233
280
234
TARGETS = (
281
235
('app' , _ ('App' )),
282
236
('worker' , _ ('Worker' )),
283
237
)
284
238
target = models .CharField (max_length = 10 , choices = TARGETS , default = 'app' )
285
239
286
- def get_local_filename (self ):
287
- padded_id = '%05d' % self .id
288
- return '%ssources/%s/%s/%s' % (settings .FILE_STORAGE , padded_id [0 ], padded_id [1 ], padded_id )
289
-
290
- def get_s3_path (self ):
291
- return 'sources/%d' % self .id
292
-
293
- def get_contents (self ):
294
- if not settings .AWS_ENABLED :
295
- try :
296
- return open (self .local_filename ).read ()
297
- except IOError :
298
- return ''
299
- else :
300
- return s3 .read_file ('source' , self .s3_path )
301
-
302
- def was_modified_since (self , expected_modification_time ):
303
- if isinstance (expected_modification_time , int ):
304
- expected_modification_time = datetime .datetime .fromtimestamp (expected_modification_time )
305
- assert isinstance (expected_modification_time , datetime .datetime )
306
- return self .last_modified .replace (tzinfo = None , microsecond = 0 ) > expected_modification_time
307
-
308
- def save_file (self , content , folded_lines = None ):
309
- if not settings .AWS_ENABLED :
310
- if not os .path .exists (os .path .dirname (self .local_filename )):
311
- os .makedirs (os .path .dirname (self .local_filename ))
312
- open (self .local_filename , 'w' ).write (content .encode ('utf-8' ))
313
- else :
314
- s3 .save_file ('source' , self .s3_path , content .encode ('utf-8' ))
315
- if folded_lines :
316
- self .folded_lines = folded_lines
317
- self .save ()
318
-
319
- def copy_to_path (self , path ):
320
- if not settings .AWS_ENABLED :
321
- try :
322
- shutil .copy (self .local_filename , path )
323
- except IOError as err :
324
- if err .errno == 2 :
325
- open (path , 'w' ).close () # create the file if it's missing.
326
- else :
327
- raise
328
- else :
329
- s3 .read_file_to_filesystem ('source' , self .s3_path , path )
330
-
331
- def save (self , * args , ** kwargs ):
332
- self .full_clean ()
333
- self .project .last_modified = now ()
334
- self .project .save ()
335
- super (SourceFile , self ).save (* args , ** kwargs )
336
-
337
240
@property
338
241
def project_path (self ):
339
242
if self .target == 'app' :
340
243
return 'src/%s' % self .file_name
341
244
else :
342
245
return 'worker_src/%s' % self .file_name
343
246
344
- local_filename = property (get_local_filename )
345
- s3_path = property (get_s3_path )
346
-
347
247
class Meta (IdeModel .Meta ):
348
248
unique_together = (('project' , 'file_name' ))
349
249
350
250
351
251
@receiver (post_delete )
352
252
def delete_file (sender , instance , ** kwargs ):
353
- if sender == SourceFile or sender == ResourceVariant :
253
+ if issubclass ( sender , S3File ) :
354
254
if settings .AWS_ENABLED :
355
255
try :
356
- s3 .delete_file ('source' , instance .s3_path )
256
+ s3 .delete_file (sender . bucket_name , instance .s3_path )
357
257
except :
358
258
traceback .print_exc ()
359
259
else :
0 commit comments