Completed
Push — master ( 37bb4c...2160b9 )
by Egor
01:33
created

ManualCleanupForm   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %
Metric Value
dl 0
loc 17
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_task_kwargs() 0 6 1
A cleanup() 0 4 1
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
21
from collections import OrderedDict
22
23
from django import forms
24
from django.forms import widgets, ValidationError
25
26
from django_ace import AceWidget
27
from suit.widgets import LinkedSelect
28
from suit_redactor.widgets import RedactorWidget
29
from celery import signature
30
31
from omaha.models import Application, Version, Action, Data
32
33
34
__all__ = ['ApplicationAdminForm', 'VersionAdminForm', 'ActionAdminForm']
35
36
37
class ApplicationAdminForm(forms.ModelForm):
38
    def clean_id(self):
39
        return self.cleaned_data["id"].upper()
40
41
    class Meta:
42
        model = Application
43
        exclude = []
44
45
46
class DataAdminForm(forms.ModelForm):
47
    class Meta:
48
        model = Data
49
        exclude = []
50
        widgets = {
51
            'value': AceWidget(mode='json', theme='monokai', width='600px', height='300px'),
52
        }
53
54
55 View Code Duplication
class VersionAdminForm(forms.ModelForm):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
    class Meta:
57
        model = Version
58
        exclude = []
59
        widgets = {
60
            'app': LinkedSelect,
61
            'release_notes': RedactorWidget(editor_options={'lang': 'en',
62
                                                            'minHeight': 150}),
63
            'file_size': widgets.TextInput(attrs=dict(disabled='disabled')),
64
        }
65
66
    def clean_file_size(self):
67
        if 'file' not in self.cleaned_data:
68
            raise ValidationError('')
69
        _file = self.cleaned_data["file"]
70
        return _file.size
71
72
73
class ActionAdminForm(forms.ModelForm):
74
    SUCCESSSACTION_CHOICES = (
75
        ('default', 'default'),
76
        ('exitsilently', 'exitsilently'),
77
        ('exitsilentlyonlaunchcmd', 'exitsilentlyonlaunchcmd')
78
    )
79
    successsaction = forms.ChoiceField(choices=SUCCESSSACTION_CHOICES)
80
81
    class Meta:
82
        model = Action
83
        exclude = []
84
85
86
class ManualCleanupForm(forms.Form):
87
    limit_days = forms.IntegerField(min_value=1, required=False, label='Maximum age (days)',
88
                                    help_text=' - remove objects older than X days')
89
    limit_size = forms.IntegerField(min_value=1, required=False, label='Purge used space (Gb)',
90
                                    help_text=" - remove old objects until total size won't reach X GB")
91
92
    def cleanup(self):
93
        model = self.initial['type'].split('__')
94
        task_kwargs = self.get_task_kwargs()
95
        signature("tasks.deferred_manual_cleanup", args=(model,), kwargs=task_kwargs).apply_async(queue='limitation')
96
97
    def get_task_kwargs(self):
98
        task_kwargs = dict(
99
            limit_size=self.cleaned_data['limit_size'],
100
            limit_days=self.cleaned_data['limit_days'],
101
        )
102
        return task_kwargs
103
104
105
class CrashManualCleanupForm(ManualCleanupForm):
106
    limit_duplicated = forms.IntegerField(min_value=1, required=False, label='Maximum amount of duplicates',
107
                                          help_text=" - remove old duplicate crashes until their number won't equal X ")
108
109
    def __init__(self, *args, **kwargs):
110
        super(CrashManualCleanupForm, self).__init__(*args, **kwargs)
111
        fields = OrderedDict()
112
        for key in ("limit_duplicated", "limit_days", "limit_size"):
113
            fields[key] = self.fields.pop(key)
114
        for key, value in self.fields.items():
115
            fields[key] = value
116
        self.fields = fields
117
118
    def get_task_kwargs(self):
119
        task_kwargs = super(CrashManualCleanupForm, self).get_task_kwargs()
120
        task_kwargs.update(dict(limit_duplicated=self.cleaned_data['limit_duplicated']))
121
        return task_kwargs
122