1 00:00:00,120 --> 00:00:03,460 It wouldn't do us much good to build this if we didn't have users that could, 2 00:00:03,460 --> 00:00:04,980 you know, use it. 3 00:00:04,980 --> 00:00:08,300 Social apps kind of fall apart when there's no one to be social with. 4 00:00:08,300 --> 00:00:11,750 One way to make it social is to use third party log ins, and let people sign up and 5 00:00:11,750 --> 00:00:13,750 log in with Twitter or Facebook. 6 00:00:13,750 --> 00:00:14,830 It's always seemed weird to me, 7 00:00:14,830 --> 00:00:18,590 to be able to log in to a social site with some other social site's credentials. 8 00:00:18,590 --> 00:00:22,770 And, that doesn't help us learn how to model these things ourselves, does it? 9 00:00:22,770 --> 00:00:25,190 That's why I'm going to show you a different way. 10 00:00:25,190 --> 00:00:27,330 All right. So the first thing we need to do, is, 11 00:00:27,330 --> 00:00:30,488 we need to make our models.py file. 12 00:00:30,488 --> 00:00:35,820 So we'll go to File > New File, and we'll call it models.py. 13 00:00:35,820 --> 00:00:39,920 Now, inside of here, just as we did the databases course, 14 00:00:39,920 --> 00:00:45,250 we want to import everything from peewee, [SOUND] so from peewee import star. 15 00:00:45,250 --> 00:00:49,580 And, our user model is gonna be pretty simple, really. 16 00:00:50,590 --> 00:00:51,260 Let's see. 17 00:00:51,260 --> 00:00:53,190 So, class user, and it's gonna be a model. 18 00:00:54,690 --> 00:00:56,890 So we're gonna give them, a username. 19 00:00:56,890 --> 00:00:59,600 We don't really have to, I mean, most of what we're gonna build, 20 00:00:59,600 --> 00:01:03,160 we could just use the ID, which is a field that's automatically created and 21 00:01:03,160 --> 00:01:05,510 it auto-increments, so the first one gets number one, 22 00:01:05,510 --> 00:01:07,660 the next one gets number two and so on. 23 00:01:07,660 --> 00:01:11,830 But I like having a username to put into URLs instead of giving them like, 24 00:01:11,830 --> 00:01:14,560 you're following user number five, but it doesn't really matter. 25 00:01:14,560 --> 00:01:16,050 It's kinda your choice. 26 00:01:16,050 --> 00:01:20,765 So we'll say username, which is a CharField, [SOUND] and 27 00:01:20,765 --> 00:01:23,821 we wanna make sure these are unique. 28 00:01:23,821 --> 00:01:26,996 We don't want them to be able to have two people with the same username 29 00:01:26,996 --> 00:01:28,990 cuz we wanna look them up by that username. 30 00:01:30,020 --> 00:01:33,580 And then we'll do email which is also a CharField, 31 00:01:33,580 --> 00:01:37,070 not a chair field, so watch out for that typo. 32 00:01:38,390 --> 00:01:41,540 Again, we wanna make this one unique because we only wanna have 33 00:01:41,540 --> 00:01:42,950 one account per email address. 34 00:01:42,950 --> 00:01:45,845 We don't want somebody to be able to have five different accounts with the same 35 00:01:45,845 --> 00:01:47,090 email address. 36 00:01:47,090 --> 00:01:49,290 They need to store a password. 37 00:01:49,290 --> 00:01:51,220 And this is also going to be a CharField. 38 00:01:51,220 --> 00:01:54,810 And we're gonna set a max length on this one of 100. 39 00:01:54,810 --> 00:01:58,710 The, hashing library that we use produces hashes of around, like, 40 00:01:58,710 --> 00:01:59,550 60 characters or so. 41 00:01:59,550 --> 00:02:03,280 But we want to have some wiggle room just in case something changes. 42 00:02:03,280 --> 00:02:07,220 Maybe we change hashing algorithms later or whatever. 43 00:02:07,220 --> 00:02:09,112 And then I wanna hold on to a couple of things. 44 00:02:09,112 --> 00:02:12,779 I wanna hold on to when they joined, [SOUND] and 45 00:02:12,779 --> 00:02:17,808 we'll set the default on this to datetime.datetime.now. 46 00:02:17,808 --> 00:02:18,900 Oops. 47 00:02:18,900 --> 00:02:22,143 No parenthesis on that, remember we don't want to put a parenthesis or 48 00:02:22,143 --> 00:02:24,314 else it'll be set to whenever the script runs. 49 00:02:24,314 --> 00:02:27,584 Not whenever the model's actually created. 50 00:02:27,584 --> 00:02:32,485 [SOUND] And then we'll hold onto a field called is_admin, 51 00:02:32,485 --> 00:02:37,295 [SOUND] and this is really more of setting you up for later. 52 00:02:37,295 --> 00:02:37,870 Oops, sorry. 53 00:02:37,870 --> 00:02:38,600 That should be false. 54 00:02:38,600 --> 00:02:43,490 This is really more of setting you up for later to where you're finished with 55 00:02:43,490 --> 00:02:46,420 this course, you wanna expand this, you wanna go a little farther. 56 00:02:46,420 --> 00:02:50,820 And maybe, if the user's an admin, they get to see some special UI abilities. 57 00:02:50,820 --> 00:02:54,700 Or maybe they get a special form where they can talk or whatever. 58 00:02:54,700 --> 00:02:57,020 Since we're using this datetime right here, 59 00:02:58,140 --> 00:03:01,050 we are going to want to import datetime, so 60 00:03:01,050 --> 00:03:05,910 we'll say import datetime, and we should probably go ahead and set up our database. 61 00:03:06,910 --> 00:03:11,300 I'm gonna use SQLite again, just as we did in the databases course. 62 00:03:11,300 --> 00:03:15,780 But if you want to use MySQL or Postgres, that's fine as well. 63 00:03:15,780 --> 00:03:19,290 And since this is going to be just the single database that we're gonna talk 64 00:03:19,290 --> 00:03:22,410 about in multiple locations, I'm gonna put it in all caps. 65 00:03:22,410 --> 00:03:26,261 Now in Python, putting it in all caps doesn't make it a constant, but 66 00:03:26,261 --> 00:03:29,546 it tells people that you're wanting this to be a constant. 67 00:03:29,546 --> 00:03:37,522 So database and let's just say social.db. 68 00:03:37,522 --> 00:03:39,260 If you want to use a different name, that's fine. 69 00:03:39,260 --> 00:03:43,140 It seems like I used just these simple little names. 70 00:03:43,140 --> 00:03:44,920 And then, let's make this a little bigger. 71 00:03:44,920 --> 00:03:47,080 There we go. 72 00:03:47,080 --> 00:03:50,340 And then down at the bottom of course, now that we've made this model, 73 00:03:50,340 --> 00:03:52,710 we have to set our class meta. 74 00:03:52,710 --> 00:03:55,240 And we have to say that database is equal to DATABASE, 75 00:03:55,240 --> 00:03:58,415 all caps, down there at the bottom. 76 00:03:58,415 --> 00:04:02,400 And then, if we were going to build a view, which we're not going to, but 77 00:04:02,400 --> 00:04:03,360 you can if you want to. 78 00:04:04,380 --> 00:04:06,370 Build a view where we list all the users. 79 00:04:06,370 --> 00:04:10,480 So like, I wanna go find, you know, my friend Jim, or my friend Ashley. 80 00:04:10,480 --> 00:04:16,387 We're gonna set how we would order them and we're gonna order them by joined at. 81 00:04:16,387 --> 00:04:20,031 [SOUND] But with a minus sign at the beginning of it. 82 00:04:20,031 --> 00:04:23,420 And so that tells order by to do this descending. 83 00:04:23,420 --> 00:04:26,771 So our newest users are going to be sorted at the top. 84 00:04:26,771 --> 00:04:30,779 So, you'll get to see people who just signed up, and then when you get to 85 00:04:30,779 --> 00:04:34,478 the bottom, you'll see the people who've been here for a while. 86 00:04:34,478 --> 00:04:36,330 All right, so I'm gonna save this. 87 00:04:36,330 --> 00:04:40,190 I'm not actually gonna create the, database file right now. 88 00:04:40,190 --> 00:04:42,790 I'm not gonna run like initialize or anything. 89 00:04:42,790 --> 00:04:45,210 We'll do that later on when we have a couple more things built. 90 00:04:46,660 --> 00:04:49,850 Our user model is nice and simple for now. 91 00:04:49,850 --> 00:04:52,170 I'm sure that it'll change before we're done with this course. 92 00:04:52,170 --> 00:04:55,332 In fact, in the next video, we're going to add some functionality to our 93 00:04:55,332 --> 00:04:57,680 user model so that we'll be able to have users sign in.