Closed stitzelj closed 7 years ago
Yeah, I'm not sure what happened but some of the code in PR #35 got a little bit mangled. Fortunately, everything is easy to fix, so this PR is basically just a patch, when you get a chance to push it out.
I way I read it in the code, it's the maximum number of flags a user can accrue through the bitwise selection in their profiles. I thought it was 11, as well, but that was when I was getting that peculiar behavior I asked you about a while back with the toggles. The way I read the code is that as a user selects a toggle, it adds that number to the total number of flags in their record, and the total, if they select all toggles, has to be inside the total range, which is 2048. Granted, only users who have access to writing blog posts will ever hit that top number, but that needs to be the maximum in order for the toggles to function reliably and predictably.
Just for reference, it's this bit of code that's important, particularly, the i in range
line:
# E-mail preferences
eflags = 0
flag = 1
for i in range(Profile.NUM_EMAIL_FLAGS):
if 'ef_{}'.format(flag) in request.POST:
eflags = eflags | flag
flag *= 2
profile.email_flags = eflags
I spent a lot of hours on that, trying to figure out what I was doing wrong. :-P
re the e-mail flags, you need to go around the loop once for each flag. Each time around the loop, the "flag" variable is doubled. After 12 times around the loop, it should be 2048, which is the correct value. After 2048 times around the loop, "flag" is equal to 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656 which is a bit bigger than we need it to be.
I switched editors recently, and it automatically reformatted a bunch of the code. I'll go back through in the refactor files and make sure that markup is preserved as it was. I haven't loaded those files into the editor yet, so it shouldn't be difficult to restore those changes.
Thanks for that.
Yeah, I dunno on the flags, then. I read that loop as starting at a value of 1 and doubling on each loop and only adding a value to the profile.email_flags variable if the toggle on that loop is tripped. I just know that I tried using 11 as the number of email flags and couldn't get toggles to stay toggled, even after multiple saves on the profile. I also looked at the user records, and the number of email flags in their database record for a user who had all toggles selected was 2043, 4 short of the max because that user didn't have access to write blogs. I'm reasonably confident my logic is accurate on this because once I changed that parameter in the model, everything worked perfectly. Maybe I don't understand exactly how the flag variable works, but the eflags variable and subsequent assignment to profile.email_flags is spot-on. I don't think the loop actually spins 2048 times, though. That's just the max value of email_flags a user can total up.
I do know that the code isn't working right on the site right now with it being set at 10. I have one toggle in my profile (the last one) I can't even turn on and while the others are toggled on, I'm not getting the auto-subscribe to quels on stories I write.
The way I read the loop is this:
You start with eflags set to 0 and flag (your bitwise operator) set to 1. The flag variable is the value of the first toggle (AUTOSUBSCRIBE_ON_STORY). If that toggle is on, it assigns the value of the toggle -- 1 in this case -- to eflags, then flag doubles so it can check the next toggle in the series (STORY_COMMENT), and the loop repeats. Each time it finds an active toggle the value of flag gets assigned to eflags and added to its own total. Then flag doubles again for the next toggle and repeats. The loop runs exactly 11 times, and the final value eflags should be less than or equal to 2048 and the value of flag should the value of the last toggle it checked, which is CHALLENGE_ENTRY, at 1024.
That's how I read the logic loop, anyway, and it's been working for me in the dev environment, so I'm reasonable confident of my understanding. I'm open to another interpretation, though, if I'm completely wrong on this.
The
for i in range(num)
form loops i from 0 to (num-1). It looks like I counted up wrong and should have set 11 instead of 10. Setting to 2048 just means that i loops 2048 times (0 to 2047), and spends the majority of its time doing nothing. Easy fix: I'll do it when I pull in the PR.
Ok, that makes sense. I'll test it again in dev. Apparently I just misunderstood that part of the logic.
A couple of things got missed somehow in the last PR #35. Merging these in should correct a couple of anomalies on the site currently.