-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodels.go
274 lines (238 loc) · 6.98 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
81
82
83
84
85
86
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
121
122
123
124
125
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
160
161
162
163
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
198
199
200
201
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
236
237
238
239
240
241
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
package core
import (
"time"
"path/filepath"
"github.com/jinzhu/gorm"
)
/*
Migrate runs AutoMigrate on all models.
*/
func Migrate(config Config) (err error) {
db := config.DB().Debug()
db.AutoMigrate(Package{}, PackageVersion{}, PackageVersionFile{})
db.AutoMigrate(User{})
db.AutoMigrate(Classifier{})
db.AutoMigrate(License{})
db.AutoMigrate(Platform{})
db.AutoMigrate(DownloadStatsWeekly{}, DownloadStatsMonthly{}, DownloadStatsYearly{})
db.AutoMigrate(Feature{})
// create all features
if err = createFeatures(db); err != nil {
return
}
return
}
/*
Classifier model
We store just values for now, later we can do some sort of tree
*/
type Classifier struct {
ID uint `gorm:"primary_key" json:"id"`
Approved bool `json:"approved"`
Name string `gorm:"unique_index" json:"name"`
}
/*
DownloadStats is common structure for all of them
CreatedAt is aligned on every struct that embeds DownloadStats with given aggregation level.
This assures that records are groupped correctly and no need to do grouping no database level.
*/
type DownloadStats struct {
ID uint `gorm:"primary_key" json:"-"`
PackageVersion *PackageVersion `gorm:"ForeignKey:PackageVersionID" json:"version,omitempty"`
PackageVersionID uint `json:"-"`
Downloads int `json:"downloads"`
CreatedAt time.Time `json:"created_at"`
}
/*
DownloadStatsWeekly represents download stats aggregated by week
*/
type DownloadStatsWeekly struct {
DownloadStats
}
/*
BeforeSave aligns CreatedAt correctly
*/
func (s *DownloadStatsWeekly) BeforeCreate() error {
s.CreatedAt = TimeAlignWeek(time.Now())
return nil
}
/*
DownloadStatsMonthly represents download stats aggregated by month
*/
type DownloadStatsMonthly struct {
DownloadStats
}
/*
BeforeSave aligns CreatedAt correctly
*/
func (s *DownloadStatsMonthly) BeforeCreate() error {
s.CreatedAt = TimeAlignMonth(time.Now())
return nil
}
/*
DownloadStatsYearly represents download stats aggregated by year
*/
type DownloadStatsYearly struct {
DownloadStats
}
/*
BeforeSave aligns CreatedAt correctly
*/
func (s *DownloadStatsYearly) BeforeCreate() error {
s.CreatedAt = TimeAlignYear(time.Now())
return nil
}
/*
Feature is model for enabling/disabling gopypi features
*/
type Feature struct {
ID string `gorm:"primary_key" json:"id"`
Description string `json:"description"`
Value bool `json:"value"`
}
/*
License model
Holds informations about available licenses.
*/
type License struct {
ID uint `gorm:"primary_key" json:"id"`
Approved bool `json:"approved"`
Code string `json:"code"`
Content string `json:"content"`
Name string `json:"name"`
}
/*
Package model.
*/
type Package struct {
ID uint `gorm:"primary_key" json:"id"`
Name string `json:"name"`
Versions []PackageVersion `gorm:"ForeignKey:PackageID" json:"versions,omitempty"`
Maintainers []User `gorm:"many2many:package_maintainers;" json:"maintainers,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Author *User `gorm:"ForeignKey:AuthorID" json:"author,omitempty"`
AuthorID uint `json:"-"`
}
/*
BeforeCreate sets CreatedAt
*/
func (p *Package) BeforeCreate() error {
p.CreatedAt = gorm.NowFunc()
return nil
}
/*
BeforeSave sets UpdatedAt
*/
func (p *Package) BeforeSave() error {
p.UpdatedAt = gorm.NowFunc()
return nil
}
/*
PackageVersion model that holds information about given package version
*/
type PackageVersion struct {
ID uint `gorm:"primary_key" json:"id"`
PackageID uint `json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Author *User `gorm:"ForeignKey:AuthorID" json:"author,omitempty"`
AuthorID uint `json:"-"`
Comment string `json:"comment"`
Description string `json:"description"`
Summary string `json:"summary"`
HomePage string `json:"home_page"`
License *License `gorm:"ForeignKey:LicenseID" json:"license,omitempty"`
LicenseID uint `json:"-"`
Version string `gorm:"index" json:"version"`
VersionOrder int `gorm:"index" json:"version_order"`
Files []PackageVersionFile `gorm:"ForeignKey:PackageVersionID" json:"files,omitempty"`
Classifiers []Classifier `gorm:"many2many:package_version_classifiers;" json:"classifiers,omitempty"`
}
/*
BeforeCreate sets CreatedAt
*/
func (p *PackageVersion) BeforeCreate() error {
p.CreatedAt = gorm.NowFunc()
return nil
}
/*
BeforeSave sets UpdatedAt
*/
func (p *PackageVersion) BeforeSave() error {
p.UpdatedAt = gorm.NowFunc()
return nil
}
/*
PackageVersionFile model
*/
type PackageVersionFile struct {
ID uint `gorm:"primary_key" json:"id"`
PackageVersionID uint `json:"-"`
Filename string `json:"filename"`
RelativePath string `json:"relative_path"`
MD5Digest string `gorm:"column:md5_digest" json:"md5_digest"`
Author *User `gorm:"ForeignKey:AuthorID" json:"author,omitempty"`
AuthorID uint `json:"-"`
CreatedAt time.Time `json:"created_at"`
// this field is used to have pregenearated download url
DownloadURL string `gorm:"-" json:"download_url,omitempty"`
}
/*
BeforeCreate sets CreatedAt
*/
func (p *PackageVersionFile) BeforeCreate() error {
p.CreatedAt = gorm.NowFunc()
return nil
}
/*
GenerateRelativePath generates random relative path
*/
func (p PackageVersionFile) GenerateRelativePath() string {
random := GenerateSalt(32)
hash := MD5(string(random))
return filepath.Join(hash[:2], hash[:4], hash[4:])
}
/*
Platform model tracks all platforms such as: Linux, Darwin even more esoteric.
*/
type Platform struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
/*
User model
To set/verify user password please use UserManager
*/
type User struct {
ID uint `gorm:"primary_key" json:"id"`
Username string `gorm:"type:varchar(20);unique_index" json:"username"`
Email string `gorm:"type:varchar(100);index" json:"email"`
Password string `gorm:"type:varchar(256)" json:"-"`
FirstName string `gorm:"type:varchar(40)" json:"first_name"`
LastName string `gorm:"type:varchar(40)" json:"last_name"`
IsActive bool `json:"is_active"`
IsAdmin bool `json:"is_admin"`
// permissions
CanList bool `json:"can_list"`
CanCreate bool `json:"can_create"`
CanDownload bool `json:"can_download"`
CanUpdate bool `json:"can_update"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
/*
BeforeCreate sets CreatedAt
*/
func (u *User) BeforeCreate() error {
u.CreatedAt = gorm.NowFunc()
return nil
}
/*
BeforeSave sets UpdatedAt
*/
func (u *User) BeforeSave() error {
u.UpdatedAt = gorm.NowFunc()
return nil
}