def UserProfile(request): pkk = None unn = None if request.method == 'POST': hob = request.POST['hobby'] IA = request.POST['IA'] loc = request.POST['loc'] FA = request.POST['FA'] if request.session.has_key('pk'): pkk = request.session['pk'] unn = request.session['username'] p = User.objects.get(pk=pkk) obj = Profile(hobby=hob, interst_area=IA, Puser=p, location=loc, fav_author=FA) obj.save() return render(request, "myapp/Thanks.html", { "data": unn, "Flag": True }) else: return render(request, "myapp/Profile.html", {"data": "Login Required"}) else: return render(request, "myapp/Profile.html", { "data": unn, "Flag": True })
def profile(backend, user, response, is_new, *args, **kwargs): if is_new: profile = Profile() profile.user = user url = 'http://graph.facebook.com/{0}/picture?type=large'.format( response.get('id')) profile.photo.save(user.username + "-facebook", ContentFile(urlopen(url).read())) user.profile.token = uuid.uuid1() profile.save()
def save(self): """Store cleaned data into database.""" data = self.cleaned_data user = User(email=data['email'], first_name=data['first_name'], last_name=data['last_name'], password=data['password'], username=data['username']) user.save() profile = Profile(user=user) profile.save() return user
def signup(request): if request.method == 'POST': #submit form form = SignUpForm(request.POST) if form.is_valid(): #save user model user = form.save(commit=False) user.is_active = False user.save() #save profile model profile = Profile() profile.self_description = form.cleaned_data.get( 'self_description') profile.is_gamer = form.cleaned_data.get('is_gamer') profile.is_developer = form.cleaned_data.get('is_developer') profile.user = user profile.save() current_site = get_current_site(request) #form the link message = render_to_string( 'registration/activation.html', { 'username': user.username, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'usertoken': PasswordResetTokenGenerator().make_token(user) }) subject = 'Your email confirmation link (do NOT reply)' user.email_user(subject, message) return redirect('activation_email_sent') else: #normal access to the page form = SignUpForm() return render(request, 'registration/signup.html', {'form': form})
def post(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) user_id = user.id image = request.data.get('image') from django.core.files.storage import default_storage from django.core.files.base import ContentFile allow_type = ["image/png", "image/jpeg", "image/gif"] from PIL import Image if image.content_type in allow_type: import hashlib og_filename = str(image).split(".") types = og_filename[len(og_filename) - 1] hash_filename = hashlib.md5(str(image).encode("utf-8")).hexdigest() + "." + types path = default_storage.save(os.getcwd() + "/images/" + hash_filename, ContentFile(image.read())) url = path.replace(os.getcwd(), "") im = Image.open("." + url) x, y = im.size if x > y: new_size = x x_offset = 0 y_offset = int((x - y) / 2) elif x < y: new_size = y x_offset = int((y - x) / 2) y_offset = 0 if x != y: new_image = Image.new("RGB", (new_size, new_size), "white") new_image.paste(im, (x_offset, y_offset)) new_image.save("." + url) profile = Profile(user_id=user_id, profile_img=url) profile.save() return Response({"msg": "success"}, status.HTTP_200_OK) else: return Response({'error_code': 1, 'error_msg': 'Upload file format is incorrect', "error_file": str(image)}, \ status.HTTP_400_BAD_REQUEST)
def register(request): if request.method == "POST": last_name = request.POST.get("Last_name") first_name = request.POST.get("First_name") Username = request.POST["Username"] email = request.POST["email"] phone_number = request.POST["Phone_number"] Password1 = request.POST["Password1"] Password2 = request.POST["Password2"] Type = request.POST["type"] if Password1 != Password2 and request.method == "POST": messages.success(request, "The two passwords do not match, try again") return render(request, "register/register.html") if User.objects.filter(username=Username, email=email).exists(): messages.success( request, "Username or email is already being used by another user. Please try again with a different email/username" ) return render(request, "register/register.html") user = User.objects.create_user(username=Username, email=email, password=Password1, first_name=first_name, last_name=last_name) group = Group.objects.get(name=Type) user.groups.add(group) user = authenticate(username=Username, password=Password1) login(request, user) new_profile = Profile(user_id=User.objects.filter( username=Username).values('id').get(username=Username)['id'], Phone_number=phone_number) new_profile.save() return redirect("home") else: return render(request, "register/register.html")
def home(request): # pass in the user data for followers and following by counting how many people are in it # how to get data from db example: data = Playlist.objects.filter(users=current_user) print("-----", request.session) cur_user = request.user request.session['test'] = "work" print("IS THIS REAL RN IS THIS REAL", request.session.get('spotify_token')) # filter to see if there is already a profile profile_exists = Profile.objects.filter(user=cur_user) if profile_exists.exists(): print("exists") else: u = Profile(user=cur_user) u.save() # get profile profile = Profile.objects.get(user=cur_user) follower_count = profile.follower.all().count() following_count = profile.following.all().count() description = profile.description if description == '': description = "Edit profile to add a description" posts = Post.objects.all().filter(user_id=cur_user) data = { 'follower': follower_count, 'following': following_count, 'description': description, 'name': cur_user, 'profile': profile, 'posts': posts } return render(request, 'account/home.html', {'data': data})
def SaveProfile(request): saved = False if request.method == 'POST': MyProfileForm = ProfileForm(request.POST, request.FILES) if MyProfileForm.is_valid(): profile = Profile() profile.name = MyProfileForm.cleaned_data["name"] profile.picture = MyProfileForm.cleaned_data["picture"] profile.save() saved = True else: MyProfileForm = ProfileForm() return render(request, 'saved.html', locals())
def SaveProfile(request): saved = False if request.method == "POST": #Get the posted form MyProfileForm = ProfileForm(request.POST, request.FILES) print("In First IF ------------------------------------->") if MyProfileForm.is_valid(): print("In IF ------------------------------------->") profile = Profile() profile.name = MyProfileForm.cleaned_data["name"] profile.picture = MyProfileForm.cleaned_data["picture"] profile.save() saved = True else: MyProfileForm = ProfileForm() return render(request, 'saved.html', locals())
def handle(self, *args, **options): if User.objects.exists(): print('User data already loaded...exiting.') print(ALREADY_LOADED_ERROR_MESSAGE) return print("Creating user data") for row in DictReader(open('./users.csv')): try: user = User.objects.create_user(username=row['UserName'],password=row['Pin']) user.save() except: continue print("Creating user's profile data") for row in DictReader(open('./users.csv')): profile=Profile() a=User.objects.get(username=row['UserName']) if not a.profile.city : a.profile.date_joined = row['DateJoined'] a.profile.city = row['City'] a.profile.save() else: continue